Skip to content

Commit

Permalink
Create PHPUnit Analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
radek-bruha committed Jul 14, 2019
1 parent 511a85e commit ebc4ed8
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -24,6 +24,22 @@ vendor/bin/phpcs bin src tests -pvvv --extensions=php --standard=ruleset.xml | v
001.000s (006.666%): SniffName
```

**[**PHPUnit**](https://github.com/sebastianbergmann/phpunit) Analyzer Usage**

```
vendor/bin/phpunit tests --log-junit=/tmp/log.xml && clear && cat /tmp/log.xml | vendor/bin/phpunit-analyzer
vendor/bin/paratest tests -p 8 --runner WrapperRunner --log-junit=/tmp/log.xml && clear && cat /tmp/log.xml | vendor/bin/phpunit-analyzer
```

```
015.000s (100.000%): Analyzed 100 000 rows of logs in 010.000s with 100.000MB RAM usage
005.000s (033.333%): TestClass::testMethod
004.000s (026.666%): TestClass::testMethod
003.000s (020.000%): TestClass::testMethod
002.000s (013.333%): TestClass::testMethod
001.000s (006.666%): TestClass::testMethod
```

**[**PHPCodeSniffer**](https://github.com/squizlabs/PHP_CodeSniffer) & [**PHPStan**](https://github.com/phpstan/phpstan) & [**PHPUnit**](https://github.com/sebastianbergmann/phpunit) & [**PHPParaTest**](https://github.com/paratestphp/paratest) & [**PHPInfection**](https://github.com/infection/infection) Clean Unified Output Usage**

```
Expand Down
2 changes: 1 addition & 1 deletion bin/phpcs-analyzer
Expand Up @@ -5,4 +5,4 @@ use Bruha\CodingStandard\CustomRules\Analyzer;

require __DIR__ . (is_file(__DIR__ . '/../../../autoload.php') ? '/../../../autoload.php' : '/../vendor/autoload.php');

exit(Analyzer::analyze((string) stream_get_contents(STDIN)));
exit(Analyzer::phpCodeSniffer((string) stream_get_contents(STDIN)));
8 changes: 8 additions & 0 deletions bin/phpunit-analyzer
@@ -0,0 +1,8 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

use Bruha\CodingStandard\CustomRules\Analyzer;

require __DIR__ . (is_file(__DIR__ . '/../../../autoload.php') ? '/../../../autoload.php' : '/../vendor/autoload.php');

exit(Analyzer::phpUnit((string) stream_get_contents(STDIN)));
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -13,10 +13,13 @@
}
},
"bin": [
"bin/phpcs-analyzer"
"bin/phpcs-analyzer",
"bin/phpunit-analyzer"
],
"require": {
"php": "^7.1",
"ext-simplexml": "*",
"ext-tokenizer": "*",
"brianium/paratest": "^3.0",
"cweagans/composer-patches": "^1.6",
"infection/infection": "^0.13",
Expand Down
56 changes: 55 additions & 1 deletion src/CustomRules/Analyzer.php
Expand Up @@ -2,6 +2,8 @@

namespace Bruha\CodingStandard\CustomRules;

use SimpleXMLElement;

/**
* Class Analyzer
*
Expand All @@ -15,7 +17,7 @@ final class Analyzer
*
* @return int
*/
public static function analyze(string $data): int
public static function phpCodeSniffer(string $data): int
{
$timestamp = microtime(TRUE);
$results = [];
Expand Down Expand Up @@ -87,4 +89,56 @@ static function (array $one, array $two): int {
return 0;
}

/**
* @param string $data
*
* @return int
*/
public static function phpUnit(string $data): int
{
$timestamp = microtime(TRUE);
$results = [];
$total = 0;
$logs = (new SimpleXMLElement($data))->xpath('//testcase') ?: [];

/** @var SimpleXMLElement $log */
foreach ($logs as $log) {
$attributes = $log->attributes();
$time = (float) $attributes['time'];
$total += $time;

$results[] = [
sprintf('%s::%s', $attributes['class'], $attributes['name']),
$time,
];
}

usort(
$results,
static function (array $one, array $two): int {
return $two[1] <=> $one[1];
}
);

echo sprintf(
'%07.3fs (100.000%%): Analyzed %s rows of logs in %07.3fs with %07.3fMB RAM usage%s',
$total,
number_format(count($logs), 0, '.', ' '),
microtime(TRUE) - $timestamp,
memory_get_peak_usage(TRUE) / 1024 ** 2,
PHP_EOL
);

foreach ($results as $result) {
[
$name,
$time,
] = $result;

echo sprintf('%07.3fs (%07.3f%%): %s%s', $time, $time / $total * 100, $name, PHP_EOL);
}

return 0;
}

}
28 changes: 25 additions & 3 deletions tests/Integration/CustomRules/AnalyzerTest.php
Expand Up @@ -16,17 +16,17 @@ final class AnalyzerTest extends AbstractTestCase
/**
*
*/
public function testAnalyze(): void
public function testPhpCodeSniffer(): void
{
ob_start();
Analyzer::analyze((string) file_get_contents(__DIR__ . '/analyzer.log'));
Analyzer::phpCodeSniffer((string) file_get_contents(__DIR__ . '/analyzer.log'));
$output = explode(PHP_EOL, (string) ob_get_contents());
ob_end_clean();

self::assertGreaterThan(0, count($output));
self::assertEquals('', array_pop($output));
self::assertRegExp(
'/.+ \(100\.000%\): Analyzed \d+ \d+ rows of logs in \d+\.\d+s with \d+\.\d+MB RAM usage/',
'/\d+\.\d+s \(100\.000%\): Analyzed \d+ \d+ rows of logs in \d+\.\d+s with \d+\.\d+MB RAM usage/',
(string) array_shift($output)
);
self::assertRegExp(
Expand All @@ -35,4 +35,26 @@ public function testAnalyze(): void
);
}

/**
*
*/
public function testPhpUnit(): void
{
ob_start();
Analyzer::phpUnit((string) file_get_contents(__DIR__ . '/analyzer.xml'));
$output = explode(PHP_EOL, (string) ob_get_contents());
ob_end_clean();

self::assertGreaterThan(0, count($output));
self::assertEquals('', array_pop($output));
self::assertRegExp(
'/\d+\.\d+s \(100\.000%\): Analyzed \d+ rows of logs in \d+\.\d+s with \d+\.\d+MB RAM usage/',
(string) array_shift($output)
);
self::assertRegExp(
'/.+ \(\d+\.\d+%\): .+Test::test.+/',
(string) array_shift($output)
);
}

}
48 changes: 48 additions & 0 deletions tests/Integration/CustomRules/analyzer.xml
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="tests" tests="23" assertions="89" errors="0" failures="0" skipped="0" time="0.421923">
<testsuite name="Tests\Integration\CustomPatches\PatchesTest" file="/var/www/project/tests/Integration/CustomPatches/PatchesTest.php" tests="6" assertions="18" errors="0" failures="0" skipped="0" time="0.278887">
<testcase name="testPhpCodeSniffer" class="Tests\Integration\CustomPatches\PatchesTest" classname="Tests.Integration.CustomPatches.PatchesTest" file="/var/www/project/tests/Integration/CustomPatches/PatchesTest.php" line="18" assertions="2" time="0.080124"/>
<testcase name="testPhpStan" class="Tests\Integration\CustomPatches\PatchesTest" classname="Tests.Integration.CustomPatches.PatchesTest" file="/var/www/project/tests/Integration/CustomPatches/PatchesTest.php" line="27" assertions="2" time="0.022571"/>
<testcase name="testPhpUnit" class="Tests\Integration\CustomPatches\PatchesTest" classname="Tests.Integration.CustomPatches.PatchesTest" file="/var/www/project/tests/Integration/CustomPatches/PatchesTest.php" line="36" assertions="2" time="0.020889"/>
<testcase name="testPhpParaTest" class="Tests\Integration\CustomPatches\PatchesTest" classname="Tests.Integration.CustomPatches.PatchesTest" file="/var/www/project/tests/Integration/CustomPatches/PatchesTest.php" line="45" assertions="1" time="0.013604"/>
<testcase name="testPhpInfection" class="Tests\Integration\CustomPatches\PatchesTest" classname="Tests.Integration.CustomPatches.PatchesTest" file="/var/www/project/tests/Integration/CustomPatches/PatchesTest.php" line="53" assertions="9" time="0.117789"/>
<testcase name="testDoctrineAnnotation" class="Tests\Integration\CustomPatches\PatchesTest" classname="Tests.Integration.CustomPatches.PatchesTest" file="/var/www/project/tests/Integration/CustomPatches/PatchesTest.php" line="96" assertions="2" time="0.023910"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\AnalyzerTest" file="/var/www/project/tests/Integration/CustomRules/AnalyzerTest.php" tests="1" assertions="4" errors="0" failures="0" skipped="0" time="0.021894">
<testcase name="testAnalyze" class="Tests\Integration\CustomRules\AnalyzerTest" classname="Tests.Integration.CustomRules.AnalyzerTest" file="/var/www/project/tests/Integration/CustomRules/AnalyzerTest.php" line="19" assertions="4" time="0.021894"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\Sniffs\Commenting\ClassSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/ClassSniffTest.php" tests="2" assertions="8" errors="0" failures="0" skipped="0" time="0.036216">
<testcase name="testSuccess" class="Tests\Integration\CustomRules\Sniffs\Commenting\ClassSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.ClassSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/ClassSniffTest.php" line="24" assertions="2" time="0.031264"/>
<testcase name="testMissing" class="Tests\Integration\CustomRules\Sniffs\Commenting\ClassSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.ClassSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/ClassSniffTest.php" line="34" assertions="6" time="0.004952"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\Sniffs\Commenting\ConstructorSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/ConstructorSniffTest.php" tests="2" assertions="11" errors="0" failures="0" skipped="0" time="0.012302">
<testcase name="testSuccess" class="Tests\Integration\CustomRules\Sniffs\Commenting\ConstructorSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.ConstructorSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/ConstructorSniffTest.php" line="24" assertions="2" time="0.008088"/>
<testcase name="testMissing" class="Tests\Integration\CustomRules\Sniffs\Commenting\ConstructorSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.ConstructorSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/ConstructorSniffTest.php" line="34" assertions="9" time="0.004214"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\Sniffs\Commenting\FunctionSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/FunctionSniffTest.php" tests="2" assertions="11" errors="0" failures="0" skipped="0" time="0.012158">
<testcase name="testSuccess" class="Tests\Integration\CustomRules\Sniffs\Commenting\FunctionSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.FunctionSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/FunctionSniffTest.php" line="24" assertions="2" time="0.007200"/>
<testcase name="testMissing" class="Tests\Integration\CustomRules\Sniffs\Commenting\FunctionSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.FunctionSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/FunctionSniffTest.php" line="34" assertions="9" time="0.004958"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\Sniffs\Commenting\InterfaceSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/InterfaceSniffTest.php" tests="2" assertions="8" errors="0" failures="0" skipped="0" time="0.011234">
<testcase name="testSuccess" class="Tests\Integration\CustomRules\Sniffs\Commenting\InterfaceSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.InterfaceSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/InterfaceSniffTest.php" line="24" assertions="2" time="0.006650"/>
<testcase name="testMissing" class="Tests\Integration\CustomRules\Sniffs\Commenting\InterfaceSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.InterfaceSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/InterfaceSniffTest.php" line="34" assertions="6" time="0.004584"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\Sniffs\Commenting\TraitSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/TraitSniffTest.php" tests="2" assertions="8" errors="0" failures="0" skipped="0" time="0.010816">
<testcase name="testSuccess" class="Tests\Integration\CustomRules\Sniffs\Commenting\TraitSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.TraitSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/TraitSniffTest.php" line="24" assertions="2" time="0.007150"/>
<testcase name="testMissing" class="Tests\Integration\CustomRules\Sniffs\Commenting\TraitSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Commenting.TraitSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Commenting/TraitSniffTest.php" line="34" assertions="6" time="0.003666"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\Sniffs\Functions\ParentSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Functions/ParentSniffTest.php" tests="2" assertions="8" errors="0" failures="0" skipped="0" time="0.015503">
<testcase name="testSuccess" class="Tests\Integration\CustomRules\Sniffs\Functions\ParentSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Functions.ParentSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Functions/ParentSniffTest.php" line="24" assertions="2" time="0.010618"/>
<testcase name="testMissing" class="Tests\Integration\CustomRules\Sniffs\Functions\ParentSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Functions.ParentSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Functions/ParentSniffTest.php" line="34" assertions="6" time="0.004885"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\Sniffs\Functions\TestSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Functions/TestSniffTest.php" tests="2" assertions="5" errors="0" failures="0" skipped="0" time="0.010865">
<testcase name="testSuccess" class="Tests\Integration\CustomRules\Sniffs\Functions\TestSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Functions.TestSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Functions/TestSniffTest.php" line="24" assertions="2" time="0.007289"/>
<testcase name="testMissing" class="Tests\Integration\CustomRules\Sniffs\Functions\TestSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Functions.TestSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Functions/TestSniffTest.php" line="34" assertions="3" time="0.003576"/>
</testsuite>
<testsuite name="Tests\Integration\CustomRules\Sniffs\Strings\ConcatenationSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Strings/ConcatenationSniffTest.php" tests="2" assertions="8" errors="0" failures="0" skipped="0" time="0.012047">
<testcase name="testSuccess" class="Tests\Integration\CustomRules\Sniffs\Strings\ConcatenationSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Strings.ConcatenationSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Strings/ConcatenationSniffTest.php" line="24" assertions="2" time="0.008517"/>
<testcase name="testMissing" class="Tests\Integration\CustomRules\Sniffs\Strings\ConcatenationSniffTest" classname="Tests.Integration.CustomRules.Sniffs.Strings.ConcatenationSniffTest" file="/var/www/project/tests/Integration/CustomRules/Sniffs/Strings/ConcatenationSniffTest.php" line="34" assertions="6" time="0.003530"/>
</testsuite>
</testsuite>
</testsuites>

0 comments on commit ebc4ed8

Please sign in to comment.