Skip to content

Commit b40323f

Browse files
nikophilsebastianbergmann
authored andcommitted
fix: generate baseline with relative path
1 parent 06e51c2 commit b40323f

File tree

7 files changed

+143
-3
lines changed

7 files changed

+143
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Runner\Baseline;
11+
12+
use PHPUnit\Runner\Exception;
13+
use RuntimeException;
14+
15+
/**
16+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
17+
*
18+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
19+
*/
20+
final class CannotWriteBaselineException extends RuntimeException implements Exception
21+
{
22+
}

src/Runner/Baseline/Writer.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use function assert;
1313
use function dirname;
1414
use function file_put_contents;
15+
use function is_dir;
16+
use function realpath;
17+
use function sprintf;
1518
use XMLWriter;
1619

1720
/**
@@ -23,10 +26,18 @@
2326
{
2427
/**
2528
* @param non-empty-string $baselineFile
29+
*
30+
* @throws CannotWriteBaselineException
2631
*/
2732
public function write(string $baselineFile, Baseline $baseline): void
2833
{
29-
$pathCalculator = new RelativePathCalculator(dirname($baselineFile));
34+
$normalizedBaselineFile = realpath(dirname($baselineFile));
35+
36+
if ($normalizedBaselineFile === false || !is_dir($normalizedBaselineFile)) {
37+
throw new CannotWriteBaselineException(sprintf('Cannot write baseline to "%s".', $baselineFile));
38+
}
39+
40+
$pathCalculator = new RelativePathCalculator($normalizedBaselineFile);
3041

3142
$writer = new XMLWriter;
3243

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
baseline.xml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="../../../../../phpunit.xsd"
4+
cacheResult="false"
5+
>
6+
<testsuites>
7+
<testsuite name="default">
8+
<directory>tests</directory>
9+
</testsuite>
10+
</testsuites>
11+
12+
<source baseline="tests/baseline.xml">
13+
<include>
14+
<directory>src</directory>
15+
</include>
16+
</source>
17+
</phpunit>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
/*
4+
* This file is part of PHPUnit.
5+
*
6+
* (c) Sebastian Bergmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace PHPUnit\TestFixture\Baseline;
12+
13+
use const E_USER_DEPRECATED;
14+
use function trigger_error;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class Test extends TestCase
18+
{
19+
public function testDeprecation(): void
20+
{
21+
trigger_error('deprecation', E_USER_DEPRECATED);
22+
23+
$this->assertTrue(true);
24+
}
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
phpunit --configuration ../_files/baseline/generate-baseline-with-relative-directory/phpunit.xml --generate-baseline
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
6+
$baselineAbsolutePath = __DIR__ . '/../_files/baseline/generate-baseline-with-relative-directory/baseline.xml';
7+
$baseline = ltrim(str_replace(getcwd(), '', $baselineAbsolutePath), DIRECTORY_SEPARATOR);
8+
9+
$_SERVER['argv'][] = '--do-not-cache-result';
10+
$_SERVER['argv'][] = '--generate-baseline';
11+
$_SERVER['argv'][] = $baseline;
12+
$_SERVER['argv'][] = '--configuration';
13+
$_SERVER['argv'][] = __DIR__ . '/../_files/baseline/generate-baseline-with-relative-directory/phpunit.xml';
14+
15+
require_once __DIR__ . '/../../bootstrap.php';
16+
17+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
18+
19+
print file_get_contents($baseline);
20+
21+
@unlink($baselineAbsolutePath);
22+
--EXPECTF--
23+
PHPUnit %s by Sebastian Bergmann and contributors.
24+
25+
Runtime: %s
26+
Configuration: %s
27+
28+
D 1 / 1 (100%)
29+
30+
Time: %s, Memory: %s
31+
32+
OK, but there were issues!
33+
Tests: 1, Assertions: 1, Deprecations: 1.
34+
35+
Baseline written to %sbaseline.xml.
36+
<?xml version="1.0"?>
37+
<files version="1">
38+
<file path="tests/Test.php">
39+
<line number="21" hash="a1022fb62c4705938dd2c6df5ff35b2621f9e97d">
40+
<issue><![CDATA[deprecation]]></issue>
41+
</line>
42+
</file>
43+
</files>

tests/unit/Runner/Baseline/WriterTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
namespace PHPUnit\Runner\Baseline;
1111

1212
use const DIRECTORY_SEPARATOR;
13+
use function getcwd;
14+
use function ltrim;
1315
use function realpath;
16+
use function str_replace;
1417
use function unlink;
1518
use PHPUnit\Framework\Attributes\CoversClass;
19+
use PHPUnit\Framework\Attributes\DataProvider;
1620
use PHPUnit\Framework\Attributes\Small;
1721
use PHPUnit\Framework\TestCase;
1822

@@ -22,6 +26,15 @@ final class WriterTest extends TestCase
2226
{
2327
private string $target;
2428

29+
public static function baselinePathProvider(): iterable
30+
{
31+
$absoluteBaselinePath = __DIR__ . '/../../../_files/baseline/expected.xml';
32+
33+
yield [$absoluteBaselinePath];
34+
35+
yield [ltrim(str_replace(getcwd(), '', $absoluteBaselinePath), DIRECTORY_SEPARATOR)];
36+
}
37+
2538
protected function setUp(): void
2639
{
2740
$this->target = realpath(__DIR__ . '/../../../_files/baseline') . DIRECTORY_SEPARATOR . 'actual.xml';
@@ -32,11 +45,19 @@ protected function tearDown(): void
3245
@unlink($this->target);
3346
}
3447

35-
public function testWritesBaselineToFileInXmlFormat(): void
48+
#[DataProvider('baselinePathProvider')]
49+
public function testWritesBaselineToFileInXmlFormat(string $baselinePath): void
3650
{
3751
(new Writer)->write($this->target, $this->baseline());
3852

39-
$this->assertXmlFileEqualsXmlFile(__DIR__ . '/../../../_files/baseline/expected.xml', $this->target);
53+
$this->assertXmlFileEqualsXmlFile($baselinePath, $this->target);
54+
}
55+
56+
public function testItThrowsExceptionIfBaseLinePathDoesNotExists(): void
57+
{
58+
$this->expectException(CannotWriteBaselineException::class);
59+
60+
(new Writer)->write('/path/to/invalid', $this->baseline());
4061
}
4162

4263
private function baseline(): Baseline

0 commit comments

Comments
 (0)