Skip to content

Commit 7e3a022

Browse files
Better testing. Not relying on output of Collision
1 parent f9d5a95 commit 7e3a022

15 files changed

+389
-269
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
}
3131
},
3232
"require-dev": {
33+
"dg/bypass-finals": "^1.4",
3334
"friendsofphp/php-cs-fixer": "^3.15",
3435
"phpstan/phpstan": "^1.10",
3536
"spatie/phpunit-snapshot-assertions": "^5.0"

composer.lock

Lines changed: 54 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
beStrictAboutOutputDuringTests="true"
66
colors="true"
77
defaultTestSuite="unit"
8-
cacheDirectory=".phpunit.cache">
8+
cacheDirectory=".phpunit.cache"
9+
displayDetailsOnTestsThatTriggerWarnings="true"
10+
bootstrap="tests/bootstrap.php">
911
<extensions>
1012
<bootstrap class="RobinIngelbrecht\PHPUnitPrettyPrint\PhpUnitExtension">
1113

@@ -14,6 +16,7 @@
1416
<testsuites>
1517
<testsuite name="unit">
1618
<file>tests/OutputTest.php</file>
19+
<directory>tests/Unit</directory>
1720
</testsuite>
1821
</testsuites>
1922
<source>

tests/OutputTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testPrintWithoutConfig(): void
1818
];
1919

2020
exec(implode(' ', $command), $out);
21-
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
21+
$this->assertStringContainsString('Tests: 3 failed, 1 risky, 1 incomplete, 1 skipped, 3 passed (7 assertions)', implode(PHP_EOL, $out));
2222
}
2323

2424
public function testWithProfiling(): void
@@ -29,7 +29,9 @@ public function testWithProfiling(): void
2929
];
3030

3131
exec(implode(' ', $command), $out);
32-
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
32+
$output = implode(PHP_EOL, $out);
33+
$this->assertStringContainsString('Tests: 2 passed (4 assertions)', $output);
34+
$this->assertStringContainsString('Top 10 slowest tests', $output);
3335
}
3436

3537
public function testWithProfilingAtRunTime(): void
@@ -41,7 +43,9 @@ public function testWithProfilingAtRunTime(): void
4143
'-d --profiling',
4244
];
4345
exec(implode(' ', $command), $out);
44-
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
46+
$output = implode(PHP_EOL, $out);
47+
$this->assertStringContainsString('Tests: 2 passed (4 assertions)', $output);
48+
$this->assertStringContainsString('Top 10 slowest tests', $output);
4549
}
4650

4751
public function testPrintCompactMode(): void
@@ -52,7 +56,9 @@ public function testPrintCompactMode(): void
5256
];
5357

5458
exec(implode(' ', $command), $out);
55-
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
59+
$output = implode(PHP_EOL, $out);
60+
$this->assertStringContainsString('.⨯⨯⨯!si..', $output);
61+
$this->assertStringContainsString('Tests: 3 failed, 1 risky, 1 incomplete, 1 skipped, 3 passed (7 assertions)', $output);
5662
}
5763

5864
public function testPrintCompactModeAtRunTime(): void
@@ -63,7 +69,9 @@ public function testPrintCompactModeAtRunTime(): void
6369
'-d --compact',
6470
];
6571
exec(implode(' ', $command), $out);
66-
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
72+
$output = implode(PHP_EOL, $out);
73+
$this->assertStringContainsString('.⨯⨯⨯!si..', $output);
74+
$this->assertStringContainsString('Tests: 3 failed, 1 risky, 1 incomplete, 1 skipped, 3 passed (7 assertions)', $output);
6775
}
6876

6977
public function testPrintWithQuote(): void

tests/SnapshotTextDriver.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/SpyOutput.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Symfony\Component\Console\Output\NullOutput;
6+
7+
class SpyOutput extends NullOutput implements \Stringable
8+
{
9+
private array $messages = [];
10+
11+
public function writeln(string|iterable $messages, int $options = self::OUTPUT_NORMAL): void
12+
{
13+
if (!is_iterable($messages)) {
14+
$messages = [$messages];
15+
}
16+
$this->messages = [...$this->messages, ...$messages];
17+
}
18+
19+
public function write(string|iterable $messages, bool $newline = false, int $options = self::OUTPUT_NORMAL): void
20+
{
21+
if (!is_iterable($messages)) {
22+
$messages = [$messages];
23+
}
24+
$this->messages = [...$this->messages, ...$messages];
25+
}
26+
27+
public function __toString(): string
28+
{
29+
return implode(PHP_EOL, $this->messages);
30+
}
31+
}

tests/Unit/ConfigurationTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Runner\Extension\ParameterCollection;
7+
use RobinIngelbrecht\PHPUnitPrettyPrint\Configuration;
8+
9+
class ConfigurationTest extends TestCase
10+
{
11+
private array $originalServer = [];
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->originalServer = $_SERVER;
18+
}
19+
20+
protected function tearDown(): void
21+
{
22+
parent::tearDown();
23+
24+
$_SERVER = $this->originalServer;
25+
}
26+
27+
public function testFromParameterCollection(): void
28+
{
29+
$configuration = Configuration::fromParameterCollection(ParameterCollection::fromArray([
30+
'displayProfiling' => 'true',
31+
'useCompactMode' => 'true',
32+
'displayQuote' => 'true',
33+
]));
34+
35+
$this->assertTrue($configuration->displayProfiling());
36+
$this->assertTrue($configuration->useCompactMode());
37+
$this->assertTrue($configuration->displayQuote());
38+
39+
$configuration = Configuration::fromParameterCollection(ParameterCollection::fromArray([]));
40+
41+
$this->assertFalse($configuration->displayProfiling());
42+
$this->assertFalse($configuration->useCompactMode());
43+
$this->assertFalse($configuration->displayQuote());
44+
}
45+
46+
public function testFromParameterCollectionWithServerArguments(): void
47+
{
48+
$_SERVER['argv'][] = '--profiling';
49+
$_SERVER['argv'][] = '--compact';
50+
$_SERVER['argv'][] = '--display-quote';
51+
$configuration = Configuration::fromParameterCollection(ParameterCollection::fromArray([]));
52+
53+
$this->assertTrue($configuration->displayProfiling());
54+
$this->assertTrue($configuration->useCompactMode());
55+
$this->assertTrue($configuration->displayQuote());
56+
}
57+
}

0 commit comments

Comments
 (0)