Skip to content

Commit

Permalink
Adds phpcs (#3)
Browse files Browse the repository at this point in the history
* Adds phpcs
* Fix coding style
  • Loading branch information
kimpepper authored Oct 5, 2023
1 parent 5ec16f3 commit 5b6db0b
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
show-progress: false
- name: 📦 Composer Update
run: composer update --with-all-dependencies --prefer-dist --no-progress --no-interaction ${{ matrix.prefer_lowest }}
- name: 🧹 PHPCS
run: ./bin/phpcs --report=checkstyle -q | ./bin/cs2pr
- name: 🧹 PHPStan
run: ./bin/phpstan --error-format=github analyse
- name: ⚡ Run Tests
Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
"symfony/console": "^6.3"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^9.6",
"previousnext/coding-standard": "^0.1.3",
"staabm/annotate-pull-request-from-checkstyle": "^1.8"
},
"autoload": {
"psr-4": {"PhpUnitSplitter\\": "src/"}
Expand All @@ -29,6 +32,9 @@
},
"config": {
"sort-packages": true,
"bin-dir": "bin"
"bin-dir": "bin",
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
8 changes: 8 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="PNX Standard">
<file>./src</file>
<file>./tests/src</file>
<rule ref="PreviousNextDrupal" />
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions" />
<arg name="report" value="full"/>
</ruleset>
69 changes: 57 additions & 12 deletions src/SplitterCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace PhpUnitSplitter;

Expand All @@ -19,13 +19,52 @@ class SplitterCommand extends Command {
* {@inheritdoc}
*/
protected function configure(): void {
$this->addArgument('splits', InputArgument::OPTIONAL, "The number of splits", 1);
$this->addArgument('index', InputArgument::OPTIONAL, "The index of the current split", 0);
$this->addOption('tests-file', 't', InputOption::VALUE_REQUIRED, "The xml file listing all tests.", getcwd() . '/tests.xml');
$this->addOption('results-file', 'f', InputOption::VALUE_REQUIRED, "The results cache file.", getcwd() . '/.phpunit.result.cache', );
$this->addOption('bootstrap-file', 'b', InputOption::VALUE_OPTIONAL, "The tests bootstrap file.", getcwd() . '/tests/bootstrap.php');
$this->addOption('prefix', 'p', InputOption::VALUE_OPTIONAL, "The prefix to remove from the file names.", getcwd() . '/');
$this->addOption('json', 'j', InputOption::VALUE_NONE, "Output the result as json.");
$this->addArgument(
'splits',
InputArgument::OPTIONAL,
"The number of splits",
1,
);
$this->addArgument(
'index',
InputArgument::OPTIONAL,
"The index of the current split",
0,
);
$this->addOption(
'tests-file',
't',
InputOption::VALUE_REQUIRED,
"The xml file listing all tests.",
\getcwd() . '/tests.xml',
);
$this->addOption(
'results-file',
'f',
InputOption::VALUE_REQUIRED,
"The results cache file.",
\getcwd() . '/.phpunit.result.cache',
);
$this->addOption(
'bootstrap-file',
'b',
InputOption::VALUE_OPTIONAL,
"The tests bootstrap file.",
\getcwd() . '/tests/bootstrap.php',
);
$this->addOption(
'prefix',
'p',
InputOption::VALUE_OPTIONAL,
"The prefix to remove from the file names.",
\getcwd() . '/',
);
$this->addOption(
'json',
'j',
InputOption::VALUE_NONE,
"Output the result as json.",
);
}

/**
Expand Down Expand Up @@ -60,18 +99,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

/**
* Splits the map into the given number of splits.
* Splits the tests map into the given number of splits.
*
* @param array<string,array<string,float>> $map
* The map of tests.
* @param int $splits
* The number of splits.
* @param int $index
* The index of the current split.
*
* @return array<string,array<string,float>>
* The split map.
*/
private function split(array $map, int $splits, int $index): array {
$result = [];
$keys = array_keys($map);
$values = array_values($map);
$keys = \array_keys($map);
$values = \array_values($map);

for ($i = $index; $i < count($map); $i++) {
for ($i = $index; $i < \count($map); $i++) {
if (($i - $index) % $splits === 0) {
$result[$keys[$i]] = $values[$i];
}
Expand Down
7 changes: 4 additions & 3 deletions src/TestMapper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace PhpUnitSplitter;

Expand Down Expand Up @@ -41,7 +41,8 @@ public function getMap(): array {
$className = (string) $class->attributes()['name'];
try {
$reflection = new \ReflectionClass($className);
} catch (\ReflectionException $e) {
}
catch (\ReflectionException $e) {
// Couldn't find the class.
continue;
}
Expand Down Expand Up @@ -84,7 +85,7 @@ public function getMap(): array {
* The sorted map.
*/
public function sortMap(array $map): array {
uasort($map, function ($a, $b) {
\uasort($map, function ($a, $b) {
return $a['time'] <=> $b['time'];
});
return $map;
Expand Down
40 changes: 29 additions & 11 deletions tests/fixtures/src/FastTestsTest.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace PhpUnitSplitter\Tests\Fixtures;

use PHPUnit\Framework\TestCase;

/**
* Simulates a fast test.
*/
class FastTestsTest extends TestCase {

function testOne(): void {
usleep(10000);
/**
*
*/
public function testOne(): void {
\usleep(10000);
$this->assertTrue(TRUE);
}

function testTwo(): void {
usleep(20000);
/**
*
*/
public function testTwo(): void {
\usleep(20000);
$this->assertTrue(TRUE);
}

function testThree(): void {
usleep(30000);
/**
*
*/
public function testThree(): void {
\usleep(30000);
$this->assertTrue(TRUE);
}

function testFour(): void {
usleep(40000);
/**
*
*/
public function testFour(): void {
\usleep(40000);
$this->assertTrue(TRUE);
}

function testFive(): void {
usleep(50000);
/**
*
*/
public function testFive(): void {
\usleep(50000);
$this->assertTrue(TRUE);
}

Expand Down
13 changes: 9 additions & 4 deletions tests/fixtures/src/ProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace PhpUnitSplitter\Tests\Fixtures;

use PHPUnit\Framework\TestCase;

/**
* Simulates a provider test.
*/
class ProviderTest extends TestCase {

/**
* @dataProvider provider
*/
function testProvider(int $sleep): void {
usleep($sleep);
public function testProvider(int $sleep): void {
\usleep($sleep);
$this->assertTrue(TRUE);
}

/**
*
*/
public function provider(): array {
return [
'one' => [111111],
Expand All @@ -26,5 +32,4 @@ public function provider(): array {
];
}


}
40 changes: 29 additions & 11 deletions tests/fixtures/src/SlowTestsTest.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace PhpUnitSplitter\Tests\Fixtures;

use PHPUnit\Framework\TestCase;

/**
* Simulates a slow test.
*/
class SlowTestsTest extends TestCase {

function testOne(): void {
usleep(100000);
/**
*
*/
public function testOne(): void {
\usleep(100000);
$this->assertTrue(TRUE);
}

function testTwo(): void {
usleep(200000);
/**
*
*/
public function testTwo(): void {
\usleep(200000);
$this->assertTrue(TRUE);
}

function testThree(): void {
usleep(300000);
/**
*
*/
public function testThree(): void {
\usleep(300000);
$this->assertTrue(TRUE);
}

function testFour(): void {
usleep(400000);
/**
*
*/
public function testFour(): void {
\usleep(400000);
$this->assertTrue(TRUE);
}

function testFive(): void {
usleep(500000);
/**
*
*/
public function testFive(): void {
\usleep(500000);
$this->assertTrue(TRUE);
}

Expand Down
12 changes: 6 additions & 6 deletions tests/src/PhpUnitSplitterTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace PhpUnitSplitter\Tests;

Expand All @@ -16,22 +16,22 @@ class PhpUnitSplitterTest extends TestCase {
* @covers ::getMap
*/
public function testSplitter(): void {
$fixtures = dirname(__DIR__) . '/fixtures';
$mapper = new TestMapper("$fixtures/tests.xml", "$fixtures/.phpunit.result.cache", dirname(__DIR__, 2) . '/');
$fixtures = \dirname(__DIR__) . '/fixtures';
$mapper = new TestMapper("$fixtures/tests.xml", "$fixtures/.phpunit.result.cache", \dirname(__DIR__, 2) . '/');
$map = $mapper->getMap();

$this->assertSame([
'tests/fixtures/src/FastTestsTest.php',
'tests/fixtures/src/ProviderTest.php',
'tests/fixtures/src/SlowTestsTest.php',
], array_keys($map));
], \array_keys($map));

$sorted = $mapper->sortMap($map);
$this->assertSame([
'tests/fixtures/src/FastTestsTest.php',
'tests/fixtures/src/SlowTestsTest.php',
'tests/fixtures/src/ProviderTest.php',
], array_keys($sorted));

], \array_keys($sorted));
}

}

0 comments on commit 5b6db0b

Please sign in to comment.