From 581af1aefbc6f15bea18a4cfd78d67b789a4b9af Mon Sep 17 00:00:00 2001 From: Daniel Bannert Date: Thu, 5 Oct 2017 09:35:36 +0200 Subject: [PATCH] Issue 490 flush method (#632) #490 adding flush to profiler --- appveyor.yml | 39 ++++---- composer.json | 2 +- .../Twig/DataCollector/TwigDataCollector.php | 15 ++- src/Viserio/Bridge/Twig/composer.json | 2 +- .../Contract/Profiler/DataCollector.php | 7 ++ .../Component/Contract/Profiler/Profiler.php | 5 + .../DataCollector/TraceableEventManager.php | 10 ++ .../ViserioEventsDataCollector.php | 10 ++ .../TraceableEventManagerTest.php | 15 +++ .../ViserioHttpDataCollector.php | 2 +- .../DataCollector/AbstractDataCollector.php | 8 ++ .../AjaxRequestsDataCollector.php | 8 ++ .../Cache/Psr6Psr16CacheDataCollector.php | 13 +++ .../{Log => Monolog}/DebugProcessor.php | 13 ++- .../MonologLoggerDataCollector.php | 22 +++-- .../DataCollector/MemoryDataCollector.php | 20 ++-- src/Viserio/Component/Profiler/Profiler.php | 10 ++ ...lerMonologDataCollectorServiceProvider.php | 4 +- .../Provider/ProfilerServiceProvider.php | 4 +- .../Log/MonologLoggerDataCollectorTest.php | 0 .../Bridge/Monolog/DebugProcessorTest.php | 96 +++++++++++++++++++ .../MonologLoggerDataCollectorTest.php | 3 + .../Component/Profiler/Tests/ProfilerTest.php | 60 +++++++----- src/Viserio/Provider/Twig/composer.json | 1 + 24 files changed, 300 insertions(+), 69 deletions(-) rename src/Viserio/Component/Profiler/DataCollector/Bridge/{Log => Monolog}/DebugProcessor.php (85%) rename src/Viserio/Component/Profiler/DataCollector/Bridge/{Log => Monolog}/MonologLoggerDataCollector.php (96%) delete mode 100644 src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Log/MonologLoggerDataCollectorTest.php create mode 100644 src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Monolog/DebugProcessorTest.php create mode 100644 src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Monolog/MonologLoggerDataCollectorTest.php diff --git a/appveyor.yml b/appveyor.yml index 49438b09e..b0e0e2654 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -22,17 +22,16 @@ matrix: services: - memcached -cache: # cache is cleared when linked file is modified - - '%LOCALAPPDATA%\Composer\files -> composer.lock' - - composer.phar +# cache is cleared when linked file is modified +cache: # Cache chocolatey packages - C:\ProgramData\chocolatey\bin -> .appveyor.yml - C:\ProgramData\chocolatey\lib -> .appveyor.yml # Cache php install - C:\tools\php -> .appveyor.yml - - .phpunit -> phpunit init: + - SET PHP=1 - SET PATH=C:\Program Files\OpenSSL;C:\tools\php;%PATH% - SET ANSICON=121x90 (121x90) - SET COMPOSER_NO_INTERACTION=1 @@ -41,24 +40,24 @@ init: install: # Install PHP - - mkdir C:\tools\php - - appveyor DownloadFile "https://raw.githubusercontent.com/prisis/ps-install-php/master/Install-PHP.ps1" - - ps: .\Install-PHP.ps1 -InstallPath C:\tools\php -Version $Env:php_ver_target -Highest -Arch x64 -Extensions mbstring,intl,openssl,memcache,fileinfo,pdo_sqlite,curl,sodium + - IF EXIST C:\tools\php (SET PHP=0) ELSE (mkdir C:\tools\php) + - IF %PHP%==1 appveyor DownloadFile "https://raw.githubusercontent.com/prisis/ps-install-php/master/Install-PHP.ps1" + - ps: if ($env.PHP) {.\Install-PHP.ps1 -InstallPath C:\tools\php -Version $Env:php_ver_target -Highest -Arch x64 -Extensions mbstring,intl,openssl,memcache,fileinfo,pdo_sqlite,curl,sodium} # Install redis and start - - nuget install redis-64 -excludeversion -Verbosity quiet - - redis-64\tools\redis-server.exe --service-install - - redis-64\tools\redis-server.exe --service-start + - IF %PHP%==1 nuget install redis-64 -excludeversion -Verbosity quiet + - IF %PHP%==1 redis-64\tools\redis-server.exe --service-install + - IF %PHP%==1 redis-64\tools\redis-server.exe --service-start # PHP settings and extensions - - cd C:\tools\php - - cd ext - - appveyor DownloadFile https://github.com/nono303/PHP7-memcache-dll/blob/master/vc15/x64/nts/php-7.2.x_memcache.dll?raw=true - - rename php-7.2.x_memcache.dll php_memcache.dll - - cd .. - - echo zend_extension=php_opcache.dll >> php.ini - - echo max_execution_time=1200 >> php.ini - - echo date.timezone="America/Los_Angeles" >> php.ini - - echo opcache.enable_cli=1 >> php.ini - - echo apc.enable_cli=1 >> php.ini + - IF %PHP%==1 cd C:\tools\php + - IF %PHP%==1 cd ext + - IF %PHP%==1 appveyor DownloadFile https://github.com/nono303/PHP7-memcache-dll/blob/master/vc15/x64/nts/php-7.2.x_memcache.dll?raw=true + - IF %PHP%==1 rename php-7.2.x_memcache.dll php_memcache.dll + - IF %PHP%==1 cd .. + - IF %PHP%==1 echo zend_extension=php_opcache.dll >> php.ini + - IF %PHP%==1 echo max_execution_time=1200 >> php.ini + - IF %PHP%==1 echo date.timezone="America/Los_Angeles" >> php.ini + - IF %PHP%==1 echo opcache.enable_cli=1 >> php.ini + - IF %PHP%==1 echo apc.enable_cli=1 >> php.ini - IF %APPVEYOR_REPO_BRANCH%==master (SET COMPOSER_ROOT_VERSION=dev-master) ELSE (SET COMPOSER_ROOT_VERSION=%APPVEYOR_REPO_BRANCH%.x-dev) - cd C:\projects\narrowspark - php -r "readfile('https://getcomposer.org/installer');" | php diff --git a/composer.json b/composer.json index 6390f7578..6fb620218 100644 --- a/composer.json +++ b/composer.json @@ -149,7 +149,7 @@ "php-amqplib/php-amqplib" : "^2.6", "pda/pheanstalk" : "^3.1", "spatie/array-to-xml" : "^2.1", - "twig/twig" : "~2.4", + "twig/twig" : "^2.4.4", "sepia/po-parser" : "^4.2", "spatie/flysystem-dropbox" : "^1.0", "symfony/routing" : "~3.3", diff --git a/src/Viserio/Bridge/Twig/DataCollector/TwigDataCollector.php b/src/Viserio/Bridge/Twig/DataCollector/TwigDataCollector.php index dd88b6634..25cfd270a 100644 --- a/src/Viserio/Bridge/Twig/DataCollector/TwigDataCollector.php +++ b/src/Viserio/Bridge/Twig/DataCollector/TwigDataCollector.php @@ -37,7 +37,7 @@ class TwigDataCollector extends AbstractDataCollector implements * * @var array */ - private $computed; + private $computed = []; /** * Create new twig collector instance. @@ -218,6 +218,17 @@ public function getTooltip(): string ]); } + /** + * {@inheritdoc} + */ + public function reset() + { + $this->profile->reset(); + + $this->computed = []; + $this->data = []; + } + /** * Get computed data. * @@ -227,7 +238,7 @@ public function getTooltip(): string */ private function getComputedData(string $index) { - if ($this->computed === null) { + if (count($this->computed) === 0) { $this->computed = $this->generateComputeData($this->getProfile()); } diff --git a/src/Viserio/Bridge/Twig/composer.json b/src/Viserio/Bridge/Twig/composer.json index fa9908272..dcb8e8eee 100644 --- a/src/Viserio/Bridge/Twig/composer.json +++ b/src/Viserio/Bridge/Twig/composer.json @@ -19,7 +19,7 @@ ], "require": { "php" : "^7.2", - "twig/twig" : "~2.4" + "twig/twig" : "^2.4.4" }, "require-dev": { "container-interop/service-provider" : "^0.4", diff --git a/src/Viserio/Component/Contract/Profiler/DataCollector.php b/src/Viserio/Component/Contract/Profiler/DataCollector.php index 035c82b84..265bc3d26 100644 --- a/src/Viserio/Component/Contract/Profiler/DataCollector.php +++ b/src/Viserio/Component/Contract/Profiler/DataCollector.php @@ -42,4 +42,11 @@ public function getMenu(): array; * @return string */ public function getMenuPosition(): string; + + /** + * Resets this data collector to its initial state. + * + * @return void + */ + public function flush(): void; } diff --git a/src/Viserio/Component/Contract/Profiler/Profiler.php b/src/Viserio/Component/Contract/Profiler/Profiler.php index 45656ea07..e26a5f028 100644 --- a/src/Viserio/Component/Contract/Profiler/Profiler.php +++ b/src/Viserio/Component/Contract/Profiler/Profiler.php @@ -102,4 +102,9 @@ public function setUrlGenerator(UrlGeneratorContract $urlGenerator): Profiler; * @return null|\Viserio\Component\Contract\Routing\UrlGenerator */ public function getUrlGenerator(): ?UrlGeneratorContract; + + /** + * @return void + */ + public function flush(): void; } diff --git a/src/Viserio/Component/Events/DataCollector/TraceableEventManager.php b/src/Viserio/Component/Events/DataCollector/TraceableEventManager.php index 9ed3a6027..6ddfe8d9c 100644 --- a/src/Viserio/Component/Events/DataCollector/TraceableEventManager.php +++ b/src/Viserio/Component/Events/DataCollector/TraceableEventManager.php @@ -224,6 +224,16 @@ public function getOrphanedEvents(): array return $this->orphanedEvents; } + /** + * Resets this to its initial state. + * + * @return void + */ + public function flush(): void + { + $this->called = []; + } + /** * @param string $eventName * @param $listener diff --git a/src/Viserio/Component/Events/DataCollector/ViserioEventsDataCollector.php b/src/Viserio/Component/Events/DataCollector/ViserioEventsDataCollector.php index 9f757a474..b6d97b9de 100644 --- a/src/Viserio/Component/Events/DataCollector/ViserioEventsDataCollector.php +++ b/src/Viserio/Component/Events/DataCollector/ViserioEventsDataCollector.php @@ -116,4 +116,14 @@ public function getPanel(): string ], ]); } + + /** + * {@inheritdoc} + */ + public function flush(): void + { + $this->data = []; + + $this->eventManager->flush(); + } } diff --git a/src/Viserio/Component/Events/Tests/DataCollector/TraceableEventManagerTest.php b/src/Viserio/Component/Events/Tests/DataCollector/TraceableEventManagerTest.php index 4e04d284c..099b60de8 100644 --- a/src/Viserio/Component/Events/Tests/DataCollector/TraceableEventManagerTest.php +++ b/src/Viserio/Component/Events/Tests/DataCollector/TraceableEventManagerTest.php @@ -247,4 +247,19 @@ public function testListenerCanRemoveItselfWhenExecuted() self::assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed'); } + + public function testClearCalledListeners() + { + $this->wrapperDispatcher->attach('foo', function () { + }, 5); + + $this->wrapperDispatcher->trigger('foo'); + $this->wrapperDispatcher->flush(); + + $listeners = $this->wrapperDispatcher->getNotCalledListeners(); + + self::assertArrayHasKey('stub', $listeners['foo'][0]); + + self::assertEquals([], $this->wrapperDispatcher->getCalledListeners()); + } } diff --git a/src/Viserio/Component/Foundation/DataCollector/ViserioHttpDataCollector.php b/src/Viserio/Component/Foundation/DataCollector/ViserioHttpDataCollector.php index fb3782d51..d173dbd3f 100644 --- a/src/Viserio/Component/Foundation/DataCollector/ViserioHttpDataCollector.php +++ b/src/Viserio/Component/Foundation/DataCollector/ViserioHttpDataCollector.php @@ -311,7 +311,7 @@ protected function getRouteInformation(RouteContract $route): array $result = [ 'uri' => $route->getUri() ?: '-', - 'methods' => \count((array) $route->getMethods()) > 1 ? + 'methods' => \count($route->getMethods()) > 1 ? \implode(' | ', $route->getMethods()) : $route->getMethods(), ]; diff --git a/src/Viserio/Component/Profiler/DataCollector/AbstractDataCollector.php b/src/Viserio/Component/Profiler/DataCollector/AbstractDataCollector.php index 68e0f4e3d..b341b5307 100644 --- a/src/Viserio/Component/Profiler/DataCollector/AbstractDataCollector.php +++ b/src/Viserio/Component/Profiler/DataCollector/AbstractDataCollector.php @@ -82,6 +82,14 @@ public function getData(): array return $this->data; } + /** + * + * {@inheritdoc} + * + */ + public function flush(): void + { + $this->data = []; + } + /** * Add measurement to float time. * diff --git a/src/Viserio/Component/Profiler/DataCollector/AjaxRequestsDataCollector.php b/src/Viserio/Component/Profiler/DataCollector/AjaxRequestsDataCollector.php index 026a63498..5ae4f019d 100644 --- a/src/Viserio/Component/Profiler/DataCollector/AjaxRequestsDataCollector.php +++ b/src/Viserio/Component/Profiler/DataCollector/AjaxRequestsDataCollector.php @@ -64,4 +64,12 @@ public function getAssets(): array 'js' => __DIR__ . '/../Resources/js/ajaxHandler.js', ]; } + + /** + * {@inheritdoc} + */ + public function flush(): void + { + // all collecting is done client side + } } diff --git a/src/Viserio/Component/Profiler/DataCollector/Bridge/Cache/Psr6Psr16CacheDataCollector.php b/src/Viserio/Component/Profiler/DataCollector/Bridge/Cache/Psr6Psr16CacheDataCollector.php index c9568c2b6..7f2852bcf 100644 --- a/src/Viserio/Component/Profiler/DataCollector/Bridge/Cache/Psr6Psr16CacheDataCollector.php +++ b/src/Viserio/Component/Profiler/DataCollector/Bridge/Cache/Psr6Psr16CacheDataCollector.php @@ -178,6 +178,19 @@ public function getCalls(): int return $this->data['pools']['calls']; } + /** + * {@inheritdoc} + */ + public function flush(): void + { + parent::flush(); + + foreach ($this->pools as $instance) { + // Calling getCalls() will clear the calls. + $instance->getCalls(); + } + } + /** * @return array */ diff --git a/src/Viserio/Component/Profiler/DataCollector/Bridge/Log/DebugProcessor.php b/src/Viserio/Component/Profiler/DataCollector/Bridge/Monolog/DebugProcessor.php similarity index 85% rename from src/Viserio/Component/Profiler/DataCollector/Bridge/Log/DebugProcessor.php rename to src/Viserio/Component/Profiler/DataCollector/Bridge/Monolog/DebugProcessor.php index fd81d93bb..f3fb7919e 100644 --- a/src/Viserio/Component/Profiler/DataCollector/Bridge/Log/DebugProcessor.php +++ b/src/Viserio/Component/Profiler/DataCollector/Bridge/Monolog/DebugProcessor.php @@ -1,6 +1,6 @@ errorCount; } + + /** + * Removes all log records. + * + * @return void + */ + public function flush(): void + { + $this->records = []; + $this->errorCount = 0; + } } diff --git a/src/Viserio/Component/Profiler/DataCollector/Bridge/Log/MonologLoggerDataCollector.php b/src/Viserio/Component/Profiler/DataCollector/Bridge/Monolog/MonologLoggerDataCollector.php similarity index 96% rename from src/Viserio/Component/Profiler/DataCollector/Bridge/Log/MonologLoggerDataCollector.php rename to src/Viserio/Component/Profiler/DataCollector/Bridge/Monolog/MonologLoggerDataCollector.php index d2ac45bdc..900c5ab37 100644 --- a/src/Viserio/Component/Profiler/DataCollector/Bridge/Log/MonologLoggerDataCollector.php +++ b/src/Viserio/Component/Profiler/DataCollector/Bridge/Monolog/MonologLoggerDataCollector.php @@ -1,6 +1,6 @@ groupLogLevels(); + $tableHeaders = ['Level', 'Channel', 'Message']; + $logs = $this->groupLogLevels(); $html = $this->createTabs([ [ 'name' => 'Info. & Errors ' . \count($logs['info_error']) . '', @@ -230,10 +226,20 @@ public function getLogs(): array return []; } + /** + * {@inheritdoc} + */ + public function flush(): void + { + if (($logger = $this->getDebugLogger()) && method_exists($logger, 'flush')) { + $logger->flush(); + } + } + /** * Returns a DebugProcessor instance if one is registered with this logger. * - * @return null|\Viserio\Component\Profiler\DataCollector\Bridge\Log\DebugProcessor + * @return null|\Viserio\Component\Profiler\DataCollector\Bridge\Monolog\DebugProcessor */ private function getDebugLogger(): ?DebugProcessor { diff --git a/src/Viserio/Component/Profiler/DataCollector/MemoryDataCollector.php b/src/Viserio/Component/Profiler/DataCollector/MemoryDataCollector.php index db5c99c40..b90010fe0 100644 --- a/src/Viserio/Component/Profiler/DataCollector/MemoryDataCollector.php +++ b/src/Viserio/Component/Profiler/DataCollector/MemoryDataCollector.php @@ -13,12 +13,7 @@ class MemoryDataCollector extends AbstractDataCollector implements TooltipAwareC */ public function __construct() { - $memoryLimit = \ini_get('memory_limit'); - - $this->data = [ - 'memory' => 0, - 'memory_limit' => $memoryLimit == '-1' ? -1 : self::convertToBytes($memoryLimit), - ]; + $this->flush(); } /** @@ -68,4 +63,17 @@ public function updateMemoryUsage(): void { $this->data['memory'] = \memory_get_peak_usage(true); } + + /** + * {@inheritdoc} + */ + public function flush(): void + { + $memoryLimit = \ini_get('memory_limit'); + + $this->data = [ + 'memory' => 0, + 'memory_limit' => $memoryLimit == '-1' ? -1 : self::convertToBytes($memoryLimit), + ]; + } } diff --git a/src/Viserio/Component/Profiler/Profiler.php b/src/Viserio/Component/Profiler/Profiler.php index 2058b8a06..792025017 100644 --- a/src/Viserio/Component/Profiler/Profiler.php +++ b/src/Viserio/Component/Profiler/Profiler.php @@ -210,6 +210,16 @@ public function getAssetsRenderer(): AssetsRendererContract return $this->assetsRenderer; } + /** + * {@inheritdoc} + */ + public function flush(): void + { + foreach ($this->collectors as $data) { + $data['collector']->flush(); + } + } + /** * Injects the web debug toolbar into the given Response. * diff --git a/src/Viserio/Component/Profiler/Provider/ProfilerMonologDataCollectorServiceProvider.php b/src/Viserio/Component/Profiler/Provider/ProfilerMonologDataCollectorServiceProvider.php index 1bab5e8aa..17fac9c0f 100644 --- a/src/Viserio/Component/Profiler/Provider/ProfilerMonologDataCollectorServiceProvider.php +++ b/src/Viserio/Component/Profiler/Provider/ProfilerMonologDataCollectorServiceProvider.php @@ -9,8 +9,8 @@ use Viserio\Component\Contract\OptionsResolver\RequiresComponentConfig as RequiresComponentConfigContract; use Viserio\Component\Contract\Profiler\Profiler as ProfilerContract; use Viserio\Component\OptionsResolver\Traits\OptionsResolverTrait; -use Viserio\Component\Profiler\DataCollector\Bridge\Log\DebugProcessor; -use Viserio\Component\Profiler\DataCollector\Bridge\Log\MonologLoggerDataCollector; +use Viserio\Component\Profiler\DataCollector\Bridge\Monolog\DebugProcessor; +use Viserio\Component\Profiler\DataCollector\Bridge\Monolog\MonologLoggerDataCollector; class ProfilerMonologDataCollectorServiceProvider implements ServiceProviderInterface, diff --git a/src/Viserio/Component/Profiler/Provider/ProfilerServiceProvider.php b/src/Viserio/Component/Profiler/Provider/ProfilerServiceProvider.php index 3afb02105..f026abe90 100644 --- a/src/Viserio/Component/Profiler/Provider/ProfilerServiceProvider.php +++ b/src/Viserio/Component/Profiler/Provider/ProfilerServiceProvider.php @@ -111,9 +111,7 @@ public static function extendEventManager( ): ?EventManagerContract { if ($eventManager !== null) { $eventManager->attach(TerminableContract::TERMINATE, function () use ($container) { - foreach ($container->get(ProfilerContract::class)->getCollectors() as $collector) { - // @todo clear collector - } + $container->get(ProfilerContract::class)->flush(); }); } diff --git a/src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Log/MonologLoggerDataCollectorTest.php b/src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Log/MonologLoggerDataCollectorTest.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Monolog/DebugProcessorTest.php b/src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Monolog/DebugProcessorTest.php new file mode 100644 index 000000000..145190a0a --- /dev/null +++ b/src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Monolog/DebugProcessorTest.php @@ -0,0 +1,96 @@ +logger = new Logger(__METHOD__, [$handler], [$processor]); + } + + public function testGetLogsWithDebugProcessor() + { + self::assertTrue($this->logger->error('error message')); + self::assertCount(1, $this->getDebugLogger()->getLogs()); + } + + public function testCountErrorsWithDebugProcessor() + { + self::assertTrue($this->logger->debug('test message')); + self::assertTrue($this->logger->info('test message')); + self::assertTrue($this->logger->notice('test message')); + self::assertTrue($this->logger->warning('test message')); + self::assertTrue($this->logger->error('test message')); + self::assertTrue($this->logger->critical('test message')); + self::assertTrue($this->logger->alert('test message')); + self::assertTrue($this->logger->emergency('test message')); + + self::assertSame(4, $this->getDebugLogger()->countErrors()); + } + + public function testGetLogsWithDebugProcessor2() + { + $handler = new TestHandler(); + $logger = new Logger('test', [$handler]); + $logger->pushProcessor(new DebugProcessor()); + $logger->addInfo('test'); + + self::assertCount(1, $this->getDebugLogger($logger)->getLogs()); + + [$record] = $this->getDebugLogger($logger)->getLogs(); + + self::assertEquals('test', $record['message']); + self::assertEquals(Logger::INFO, $record['priority']); + } + + public function testFlush() + { + $handler = new TestHandler(); + $logger = new Logger('test', [$handler]); + $logger->pushProcessor(new DebugProcessor()); + $logger->addInfo('test'); + + $this->getDebugLogger($logger)->flush(); + + self::assertEmpty($this->getDebugLogger($logger)->getLogs()); + self::assertSame(0, $this->getDebugLogger($logger)->countErrors()); + } + + /** + * Returns a DebugProcessor instance if one is registered with this logger. + * + * @param null|\Monolog\Logger $logger + * + * @return null|\Viserio\Component\Profiler\DataCollector\Bridge\Monolog\DebugProcessor + */ + private function getDebugLogger(Logger $logger = null): ?DebugProcessor + { + if ($logger === null) { + $logger = $this->logger; + } + + foreach ($logger->getProcessors() as $processor) { + if ($processor instanceof DebugProcessor) { + return $processor; + } + } + + return null; + } +} diff --git a/src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Monolog/MonologLoggerDataCollectorTest.php b/src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Monolog/MonologLoggerDataCollectorTest.php new file mode 100644 index 000000000..fa9c6009e --- /dev/null +++ b/src/Viserio/Component/Profiler/Tests/DataCollector/Bridge/Monolog/MonologLoggerDataCollectorTest.php @@ -0,0 +1,3 @@ +getProfiler(); + parent::setUp(); - $profiler->setUrlGenerator($this->mock(UrlGeneratorContract::class)); + $this->profiler = new Profiler(new AssetsRenderer()); + } - self::assertInstanceOf(UrlGeneratorContract::class, $profiler->getUrlGenerator()); + public function testSetAndGetUrlGenerator(): void + { + $this->profiler->setUrlGenerator($this->mock(UrlGeneratorContract::class)); + + self::assertInstanceOf(UrlGeneratorContract::class, $this->profiler->getUrlGenerator()); } public function testSetAndGetTemplate(): void { - $profiler = $this->getProfiler(); - - $profiler->setTemplate(__DIR__); + $this->profiler->setTemplate(__DIR__); - self::assertSame(__DIR__, $profiler->getTemplate()); + self::assertSame(__DIR__, $this->profiler->getTemplate()); } public function testAddHasAndGetCollectors(): void { - $profiler = $this->getProfiler(); $collector = new PhpInfoDataCollector(); - $profiler->addCollector($collector); + $this->profiler->addCollector($collector); - self::assertTrue($profiler->hasCollector('php-info-data-collector')); + self::assertTrue($this->profiler->hasCollector('php-info-data-collector')); self::assertSame( [ @@ -48,7 +56,7 @@ public function testAddHasAndGetCollectors(): void 'priority' => 100, ], ], - $profiler->getCollectors() + $this->profiler->getCollectors() ); } @@ -58,11 +66,10 @@ public function testAddHasAndGetCollectors(): void */ public function testAddCollectorThrowsException(): void { - $profiler = $this->getProfiler(); $collector = new PhpInfoDataCollector(); - $profiler->addCollector($collector); - $profiler->addCollector($collector); + $this->profiler->addCollector($collector); + $this->profiler->addCollector($collector); } public function testModifyResponse(): void @@ -136,17 +143,14 @@ public function testModifyResponseWithOldContent(): void public function testDontModifyResponse(): void { - $assets = new AssetsRenderer(); - $profiler = new Profiler($assets); - $server = $_SERVER; $server['SERVER_ADDR'] = '127.0.0.1'; unset($server['PHP_SELF']); - $profiler->disable(); + $this->profiler->disable(); $orginalResponse = (new ResponseFactory())->createResponse(); - $response = $profiler->modifyResponse( + $response = $this->profiler->modifyResponse( (new ServerRequestFactory())->createServerRequestFromArray($server), $orginalResponse ); @@ -154,13 +158,21 @@ public function testDontModifyResponse(): void self::assertEquals($response, $orginalResponse); } - private function removeId(string $html): string + public function testFlush() { - return \trim(\preg_replace('/="profiler-(.*?)"/', '', $html)); + $collector = $this->mock(DataCollector::class); + $collector->shouldReceive('getName') + ->twice() + ->andReturn('mock'); + $collector->shouldReceive('flush') + ->once(); + + $this->profiler->addCollector($collector); + $this->profiler->flush(); } - private function getProfiler() + private function removeId(string $html): string { - return new Profiler(new AssetsRenderer()); + return \trim(\preg_replace('/="profiler-(.*?)"/', '', $html)); } } diff --git a/src/Viserio/Provider/Twig/composer.json b/src/Viserio/Provider/Twig/composer.json index 914d43d42..9fa0a84c0 100644 --- a/src/Viserio/Provider/Twig/composer.json +++ b/src/Viserio/Provider/Twig/composer.json @@ -19,6 +19,7 @@ ], "require": { "php" : "^7.2", + "twig/twig" : "^2.4.4", "viserio/contract" : "self.version", "viserio/view" : "self.version" },