Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Makefile test to an auto-review namespace #777

Merged
merged 1 commit into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions tests/AutoReview/E2ECollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\AutoReview;

use Humbug\PhpScoper\NotInstantiable;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use function array_filter;
use function array_map;
use function in_array;
use function iterator_to_array;
use function sort;
use function substr;
use const SORT_STRING;

final class E2ECollector
{
use NotInstantiable;

private const E2E_DIR = __DIR__.'/../../fixtures';

private const NON_E2E_TESTS = [
'set000',
'set001',
'set002',
'set003',
'set006',
'set007',
'set008',
'set009',
'set010',
'set012',
];

/**
* @return list<string>
*/
public static function getE2ENames(): array
{
static $names;

if (!isset($names)) {
$names = self::findE2ENames();
}

return $names;
}

/**
* @return list<string>
*/
private static function findE2ENames(): array
{
$finder = Finder::create()
->directories()
->in(self::E2E_DIR)
->depth(0);

$names = array_filter(
array_map(
self::extractName(...),
iterator_to_array($finder, false),
),
);

sort($names, SORT_STRING);

return $names;
}

private static function extractName(SplFileInfo $fileInfo): ?string
{
$filename = $fileInfo->getFilename();

if (in_array($filename, self::NON_E2E_TESTS, true)) {
return null;
}

$setNumber = substr($filename, 3, 3);

return 'e2e_'.$setNumber;
}
}
45 changes: 45 additions & 0 deletions tests/AutoReview/E2ECollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\AutoReview;

use PHPUnit\Framework\TestCase;
use function count;
use function in_array;

/**
* @covers \Humbug\PhpScoper\AutoReview\E2ECollector
*
* @internal
*/
class E2ECollectorTest extends TestCase
{
public function test_it_collects_the_e2e_test_names(): void
{
$names = E2ECollector::getE2ENames();

self::assertGreaterThan(0, count($names));

foreach ($names as $name) {
self::assertMatchesRegularExpression('/^e2e_\d{3}$/', $name);
}
}

public function test_it_ignores_non_e2e_tests(): void
{
$names = E2ECollector::getE2ENames();

self::assertFalse(in_array('e2e_000', $names, true));
}
}
33 changes: 24 additions & 9 deletions tests/MakefileE2ETest.php → tests/AutoReview/MakefileE2ETest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper;
namespace Humbug\PhpScoper\AutoReview;

use PHPUnit\Framework\TestCase;
use function array_filter;
Expand All @@ -31,20 +31,35 @@
*/
class MakefileE2ETest extends TestCase
{
public function test_the_e2e_test_executes_all_the_e2e_sub_rules(): void
private const MAKEFILE_PATH = __DIR__.'/../../Makefile';

private static string $makeFileContents;

public static function setUpBeforeClass(): void
{
$contents = file_get_contents(__DIR__.'/../Makefile');
self::$makeFileContents = file_get_contents(self::MAKEFILE_PATH);
}

$mainE2ERule = $this->retrieveE2ERule($contents);
$e2eSubRules = $this->retrieveSubE2ERules($contents);
public function test_the_e2e_test_executes_all_the_e2e_sub_rules(): void
{
$mainE2ERule = self::retrieveE2ERule(self::$makeFileContents);
$e2eSubRules = self::retrieveSubE2ERules(self::$makeFileContents);

self::assertSame($e2eSubRules, $mainE2ERule);
}

public function test_it_lists_all_e2e_tests(): void
{
$expected = E2ECollector::getE2ENames();
$actual = self::retrieveE2ERule(self::$makeFileContents);

self::assertEqualsCanonicalizing($expected, $actual);
}

/**
* @return string[]
* @return list<string>
*/
private function retrieveE2ERule(string $makefileContents): array
private static function retrieveE2ERule(string $makefileContents): array
{
if (1 !== preg_match(
'/e2e:(?<steps>[\p{L}\d_ ]+)/u',
Expand All @@ -68,9 +83,9 @@ private function retrieveE2ERule(string $makefileContents): array
}

/**
* @return string[]
* @return list<string>
*/
private function retrieveSubE2ERules(string $makefileContents): array
private static function retrieveSubE2ERules(string $makefileContents): array
{
if (1 !== preg_match_all(
'/(?<step>e2e_\d+):/u',
Expand Down