Skip to content

Commit 910ef06

Browse files
committed
Add Acceptance tests
1 parent a20a0f2 commit 910ef06

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
"friendsofphp/php-cs-fixer": "^3.64"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^9.6",
29-
"vimeo/psalm": "^5.26"
28+
"phpunit/phpunit": "^10.5",
29+
"vimeo/psalm": "^5.26",
30+
"spiral/dumper": "^3.3"
3031
},
3132
"autoload": {
3233
"psr-4": {
@@ -35,7 +36,7 @@
3536
},
3637
"autoload-dev": {
3738
"psr-4": {
38-
"Spiral\\Tests\\": "tests/"
39+
"Spiral\\CodeStyle\\Tests\\": "tests/"
3940
}
4041
},
4142
"scripts": {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\CodeStyle\Tests\Acceptance\Stub\Styled;
6+
7+
final class ClassConstLines
8+
{
9+
/**
10+
* Multiline comment
11+
*/
12+
public const WITH_COMMENT = 1;
13+
14+
/**
15+
* Multiline comment
16+
*/
17+
public const WITH_COMMENT_2 = 1;
18+
19+
public const TWO = 2;
20+
public const THREE = 3;
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\CodeStyle\Tests\Acceptance\Stub\Styled;
6+
7+
final class ClassPropertiesLines
8+
{
9+
/**
10+
* Multiline comment
11+
*/
12+
public int $withComment = 1;
13+
14+
/**
15+
* Multiline comment
16+
*/
17+
public int $withComment2 = 1;
18+
19+
public int $two = 2;
20+
public int $three = 3;
21+
}

tests/Acceptance/StyleTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\CodeStyle\Tests\Acceptance;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class StyleTest extends TestCase
10+
{
11+
private const CS_STATUSES = [
12+
1 => 'General error (or PHP minimal requirement not matched).',
13+
4 => 'Some files have invalid syntax (only in dry-run mode).',
14+
8 => 'Some files need fixing (only in dry-run mode).',
15+
16 => 'Configuration error of the application.',
16+
32 => 'Configuration error of a Fixer.',
17+
64 => 'Exception raised within the application.',
18+
];
19+
20+
public function testStyled(): void
21+
{
22+
$dir = \realpath(__DIR__ . '/Stub/Styled');
23+
$command = "php vendor/bin/php-cs-fixer fix --dry-run --diff --format=json $dir";
24+
25+
\exec($command, $output, $status);
26+
27+
\in_array($status, [0, 8]) or $this->fail(\sprintf(
28+
"php-cs-fixer failed: %s. \n %s\n\n%s",
29+
$status,
30+
\implode('\n ', $this->describeFailCommand($status)),
31+
\implode("\n", $output),
32+
));
33+
34+
/**
35+
* @var array{
36+
* about: non-empty-string,
37+
* files: list<array{
38+
* name: non-empty-string,
39+
* diff: non-empty-string,
40+
* }>,
41+
* time: array{total: float},
42+
* memory: positive-int
43+
* } $result
44+
*/
45+
$result = \json_decode(\implode('', $output), true, 512, \JSON_THROW_ON_ERROR);
46+
47+
// No files to fix
48+
if ($result['files'] === []) {
49+
$this->assertTrue(true);
50+
return;
51+
}
52+
53+
$this->fail(\sprintf(
54+
"Some nominal stub files were changed: \n\n%s",
55+
\implode("\n", \array_map(
56+
static fn(array $file) => $file['diff'],
57+
$result['files'],
58+
)),
59+
));
60+
}
61+
62+
private function describeFailCommand(int $code): array
63+
{
64+
$result = [];
65+
foreach (self::CS_STATUSES as $mask => $description) {
66+
$code & $mask and $result[] = $description;
67+
}
68+
69+
return $result;
70+
}
71+
}

tests/Unit/BuilderTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
namespace Spiral\CodeStyle\Tests\Unit;
6+
57
use PhpCsFixer\Config;
68
use PHPUnit\Framework\TestCase;
79
use Spiral\CodeStyle\Builder;

0 commit comments

Comments
 (0)