Skip to content

Commit

Permalink
Added test for Profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Knap committed Sep 19, 2016
1 parent bfc8cb2 commit 8222deb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
/.idea/
/vendor/
/tests/phpunit.log
/composer.lock
5 changes: 4 additions & 1 deletion makefile
@@ -1,4 +1,4 @@
.PHONY: demo demo-build demo-run
.PHONY: demo demo-build demo-run tests

demo:
composer install
Expand All @@ -11,3 +11,6 @@ demo-build:

demo-run:
docker run --rm -p 8080:80 --name tracy-profiler-demo tracy-profiler-demo

tests:
sudo docker run -v $$(pwd):/app --rm php:5.4-cli bash -c 'cd /app && php ./vendor/bin/phpunit'
10 changes: 10 additions & 0 deletions phpunit.xml
@@ -0,0 +1,10 @@
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<logging>
<log type="testdox-text" target="./tests/phpunit.log"/>
</logging>
</phpunit>
15 changes: 11 additions & 4 deletions src/Profiler/Profiler.php
Expand Up @@ -9,15 +9,22 @@ class Profiler extends AdvancedProfiler
{
private static $postProcessors;

public static function setPostProcessor(callable $postProcessor)
/**
* @inheritdoc
*/
public static function setPostProcessor(callable $postProcessor, $postProcessorId = "default")
{
$postProcessorId = func_get_arg(1);
self::$postProcessors[$postProcessorId] = $postProcessor;

$postProcessors = self::$postProcessors;
parent::setPostProcessor(function (Profile $profile) use ($postProcessors) {
foreach ($postProcessors as $postProcessor) {
$profile = call_user_func($postProcessor, $profile);
foreach ($postProcessors as $key => $postProcessor) {
if ($key !== "default") {
$profile = call_user_func($postProcessor, $profile);
}
}
if (isset($postProcessors["default"])) {
$profile = call_user_func($postProcessors["default"], $profile);
}
return $profile;
});
Expand Down
37 changes: 35 additions & 2 deletions tests/Profiler/ProfilerTest.php
@@ -1,6 +1,39 @@
<?php

class ProfilerTest
namespace Netpromotion\Profiler\Test;

use Netpromotion\Profiler\Profiler;
use PetrKnap\Php\Profiler\Profile;

class ProfilerTest extends \PHPUnit_Framework_TestCase
{
// TODO test multiple hooks support, check that postProcessor with id NULL is called at the end
public function testMultipleHooksWorks()
{
$order = [];
Profiler::setPostProcessor(function(Profile $profile) use (&$order) {
$order[] = 1;
return $profile;
});

Profiler::setPostProcessor(function(Profile $profile) use (&$order) {
$order[] = 2;
return $profile;
});

Profiler::setPostProcessor(function(Profile $profile) use (&$order) {
$order[] = 3;
return $profile;
}, "B");

Profiler::setPostProcessor(function(Profile $profile) use (&$order) {
$order[] = 4;
return $profile;
}, "A");

Profiler::enable();
Profiler::start();
Profiler::finish();

$this->assertEquals([3, 4, 2], $order);
}
}

0 comments on commit 8222deb

Please sign in to comment.