Skip to content

Commit

Permalink
[MetricsPower] Added unit and feature tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalzombie committed Mar 27, 2024
1 parent 8467a10 commit e10599b
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 46 deletions.
1 change: 1 addition & 0 deletions .docker/php/cli/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ RUN chmod a+x /usr/local/bin/start-consumer && \
# Configure PHP
COPY conf/docker-php-ext-xdebug.ini ${PHP_INI_DIR}/conf.d/docker-php-ext-xdebug.ini
COPY conf/docker-php-ext-opcache.ini ${PHP_INI_DIR}/conf.d/docker-php-ext-opcache.ini
COPY conf/docker-php-ext-opcache.ini ${PHP_INI_DIR}/conf.d/docker-php-ext-apcu.ini
COPY conf/php.production.ini ${PHP_INI_DIR}/php.ini

RUN apk add --no-cache sudo && \
Expand Down
4 changes: 4 additions & 0 deletions .docker/php/cli/conf/docker-php-ext-apcu.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[apcu]
zend_extension=apcu.so

apc.enabled=1
2 changes: 1 addition & 1 deletion .docker/php/cli/conf/docker-php-ext-opcache.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ zend_extension=opcache.so
opcache.enable=0
opcache.jit=tracing
opcache.jit_buffer_size=512M
opcache.preload_user=www-data
opcache.preload_user=www-dataa
opcache.preload=/var/www/html/config/preload.php
4 changes: 2 additions & 2 deletions Enum/StorageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ enum StorageType: string
use WithEnumValues;

case Apc = 'apc';
case ApcNg = 'apc_ng';
case ApcNg = 'apc-ng';
case Redis = 'redis';
case RedisNg = 'redis_ng';
case RedisNg = 'redis-ng';
case InMemory = 'in-memory';
public const APC = 'apc';
public const APC_NG = 'apc_ng';
Expand Down
28 changes: 0 additions & 28 deletions Exception/NotSupportedStorageException.php

This file was deleted.

2 changes: 2 additions & 0 deletions Factory/PrometheusStorageAdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace FRZB\Component\MetricsPower\Factory;

use FRZB\Component\DependencyInjection\Attribute\AsService;
use FRZB\Component\MetricsPower\DependencyInjection\Configuration;
use FRZB\Component\MetricsPower\Enum\StorageType;
use FRZB\Component\MetricsPower\Factory\Exception\NoRedisConfigurationProvidedException;
Expand All @@ -26,6 +27,7 @@
use Prometheus\Storage\Redis;
use Prometheus\Storage\RedisNg;

#[AsService]
final class PrometheusStorageAdapterFactory implements PrometheusStorageAdapterFactoryInterface
{
public static function createStorageAdapter(array $configuration): Adapter
Expand Down
2 changes: 2 additions & 0 deletions Factory/PrometheusStorageAdapterFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

namespace FRZB\Component\MetricsPower\Factory;

use FRZB\Component\DependencyInjection\Attribute\AsAlias;
use FRZB\Component\MetricsPower\Factory\Exception\StorageAdapterFactoryException;
use Prometheus\Storage\Adapter;

#[AsAlias(PrometheusStorageAdapterFactory::class)]
interface PrometheusStorageAdapterFactoryInterface
{
/** @throws StorageAdapterFactoryException */
Expand Down
4 changes: 0 additions & 4 deletions OptionsResolver/Resolver/DefaultOptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
use FRZB\Component\DependencyInjection\Attribute\AsService;
use FRZB\Component\DependencyInjection\Attribute\AsTagged;
use FRZB\Component\MetricsPower\Attribute\OptionsInterface;
use FRZB\Component\MetricsPower\Logger\MetricsPowerLoggerInterface;
use Symfony\Component\Messenger\Event\AbstractWorkerMessageEvent;
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;

#[AsService, AsTagged(OptionsResolverInterface::class)]
class DefaultOptionsResolver extends LoggerOptionsResolver implements OptionsResolverInterface
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services: &services
autoconfigure: true

FRZB\Component\MetricsPower\:
resource: '../../{Action,EventListener,Handler,TypeExtractor,Logger,OptionsResolver}/**'
resource: '../../{Action,EventListener,Handler,Factory,TypeExtractor,Logger,OptionsResolver}/**'
exclude: '../../{Attribute,Configuration,DependencyInjection,Enum,Exception,Helper,Tests,Traits}/**'

when@test:
Expand Down
19 changes: 14 additions & 5 deletions Tests/Feature/Action/MetricsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

declare(strict_types=1);

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com
*
* For the full copyright and license information, please view the LICENSE.MD
* file that was distributed with this source code.
*/

namespace FRZB\Component\MetricsPower\Tests\Feature\Action;


use FRZB\Component\MetricsPower\Action\MetricsAction;
use FRZB\Component\MetricsPower\Exception\MetricsRenderException;
use Prometheus\RegistryInterface;
Expand All @@ -18,7 +27,7 @@
$this->bootKernel();
});

test('It can generate correct prometheus metrics', function () {
test('It can generate correct prometheus metrics', function (): void {
$response = $this->getContainer()->get(MetricsAction::class)();

expect($response->getContent())
Expand All @@ -27,13 +36,13 @@
->and((bool) preg_match('/php_info{version="[\d+].+"} [\d+]+/', $response->getContent()))->toBeTrue();
});

test('It can throws when something goes wrong', function () {
$registry = \Mockery::mock( RegistryInterface::class);
test('It can throws when something goes wrong', function (): void {
$registry = \Mockery::mock(RegistryInterface::class);
$this->getContainer()->set(RegistryInterface::class, $registry);

$registry
->expects('getMetricFamilySamples')
->andThrow(new \Exception('Something goes wrong'));

$this->getContainer()->get(MetricsAction::class)();
})->expectException(MetricsRenderException::class);
})->expectException(MetricsRenderException::class);
75 changes: 75 additions & 0 deletions Tests/Feature/Factory/PrometheusStorageAdapterFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com
*
* For the full copyright and license information, please view the LICENSE.MD
* file that was distributed with this source code.
*/

namespace FRZB\Component\MetricsPower\Tests\Feature\Factory;

use FRZB\Component\MetricsPower\Enum\StorageType;
use FRZB\Component\MetricsPower\Factory\Exception\NotSupportedStorageAdapterException;
use FRZB\Component\MetricsPower\Factory\PrometheusStorageAdapterFactoryInterface;
use Prometheus\Storage\APC;
use Prometheus\Storage\APCng;
use Prometheus\Storage\InMemory;
use Prometheus\Storage\Redis;
use Prometheus\Storage\RedisNg;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

uses(KernelTestCase::class);

beforeEach(function (): void {
$this->ensureKernelShutdown();
$this->bootKernel();
});

test('It can create needed storage adapter', function (StorageType $storage): void {
$factory = $this->getContainer()->get(PrometheusStorageAdapterFactoryInterface::class);

$storageAdapter = $factory->createStorageAdapter(['storage' => $storage->value, 'redis' => []]);

expect($storageAdapter)->toBeInstanceOf(
match ($storage) {
StorageType::Redis => Redis::class,
StorageType::RedisNg => RedisNg::class,
StorageType::InMemory => InMemory::class,
default => throw new \InvalidArgumentException('Unexpected match value'),
}
);
})->with([
StorageType::Redis,
StorageType::RedisNg,
StorageType::InMemory,
]);

test('It can create apcu, apcu-ng storage adapter', function (StorageType $storage): void {
$factory = $this->getContainer()->get(PrometheusStorageAdapterFactoryInterface::class);

$storageAdapter = $factory->createStorageAdapter(['storage' => $storage->value, 'redis' => []]);

expect($storageAdapter)->toBeInstanceOf(
match ($storage) {
StorageType::Apc => APC::class,
StorageType::ApcNg => APCng::class,
default => throw new \InvalidArgumentException('Unexpected match value'),
}
);
})->with([
StorageType::Apc,
StorageType::ApcNg,
])->skip(!\extension_loaded('apcu') || !apcu_enabled());

test('It can not create unknown storage adapter', function (): void {
$factory = $this->getContainer()->get(PrometheusStorageAdapterFactoryInterface::class);

$factory->createStorageAdapter(['storage' => 'unknown-storage']);
})->throws(NotSupportedStorageAdapterException::class);
13 changes: 13 additions & 0 deletions Tests/Stub/Enum/TestEnum.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<?php

declare(strict_types=1);

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com
*
* For the full copyright and license information, please view the LICENSE.MD
* file that was distributed with this source code.
*/

namespace FRZB\Component\MetricsPower\Tests\Stub\Enum;

use FRZB\Component\MetricsPower\Traits\WithEnumValues;
Expand Down
2 changes: 0 additions & 2 deletions Tests/Stub/Message/TestMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

namespace FRZB\Component\MetricsPower\Tests\Stub\Message;

use FRZB\Component\MetricsPower\Attribute\LoggerOptions;
use FRZB\Component\MetricsPower\Attribute\Metrical;
use FRZB\Component\MetricsPower\Attribute\PrometheusOptions;
use FRZB\Component\MetricsPower\Attribute\SentryOptions;
use FRZB\Component\MetricsPower\Tests\Stub\TestConstants;

/** @internal */
Expand Down
13 changes: 11 additions & 2 deletions Tests/Stub/Message/TestMessageWithAllOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

declare(strict_types=1);

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com
*
* For the full copyright and license information, please view the LICENSE.MD
* file that was distributed with this source code.
*/

namespace FRZB\Component\MetricsPower\Tests\Stub\Message;


use FRZB\Component\MetricsPower\Attribute\LoggerOptions;
use FRZB\Component\MetricsPower\Attribute\Metrical;
use FRZB\Component\MetricsPower\Attribute\PrometheusOptions;
Expand Down Expand Up @@ -34,4 +43,4 @@ public static function getShortName(): string
{
return (new \ReflectionClass(self::class))->getShortName();
}
}
}
15 changes: 14 additions & 1 deletion Tests/Unit/Traits/WithEnumValuesTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
<?php

declare(strict_types=1);

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com
*
* For the full copyright and license information, please view the LICENSE.MD
* file that was distributed with this source code.
*/

namespace FRZB\Component\MetricsPower\Tests\Unit\Traits;

use FRZB\Component\MetricsPower\Tests\Stub\Enum\TestEnum;

test('It can map values from enum', function () {
test('It can map values from enum', function (): void {
expect(TestEnum::values())->toBe(['one', 'two']);
});

0 comments on commit e10599b

Please sign in to comment.