-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse and apply baseline from configuration
This integrates the changes filter into the backward compatibility check command, parsing the configuration file prior to any process to ease the integration of new properties. Signed-off-by: Luís Cobucci <[email protected]>
- Loading branch information
Showing
5 changed files
with
192 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\BackwardCompatibility\Command; | ||
|
||
use Psl\Json; | ||
use Psl\Type; | ||
use Roave\BackwardCompatibility\Baseline; | ||
use RuntimeException; | ||
|
||
final class Configuration | ||
{ | ||
private function __construct(public readonly Baseline $baseline) | ||
{ | ||
} | ||
|
||
public static function default(): self | ||
{ | ||
return new self(Baseline::empty()); | ||
} | ||
|
||
public static function fromJson(string $jsonContents): self | ||
{ | ||
try { | ||
$configuration = Json\typed( | ||
$jsonContents, | ||
Type\shape( | ||
['baseline' => Type\optional(Type\vec(Type\string()))], | ||
), | ||
); | ||
} catch (Json\Exception\DecodeException $exception) { | ||
throw new RuntimeException( | ||
'It was not possible to parse the configuration', | ||
previous: $exception, | ||
); | ||
} | ||
|
||
$baseline = $configuration['baseline'] ?? []; | ||
|
||
return new self(Baseline::fromList(...$baseline)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RoaveTest\BackwardCompatibility\Command; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Roave\BackwardCompatibility\Baseline; | ||
use Roave\BackwardCompatibility\Command\Configuration; | ||
use RuntimeException; | ||
|
||
/** @covers \Roave\BackwardCompatibility\Command\Configuration */ | ||
final class ConfigurationTest extends TestCase | ||
{ | ||
public function testBaselineShouldBeEmptyForDefaultConfiguration(): void | ||
{ | ||
$config = Configuration::default(); | ||
|
||
self::assertEquals(Baseline::empty(), $config->baseline); | ||
} | ||
|
||
/** @dataProvider validConfigurations */ | ||
public function testBaselineShouldBeReadFromJsonContents( | ||
string $jsonContents, | ||
Baseline $expectedBaseline, | ||
): void { | ||
$config = Configuration::fromJson($jsonContents); | ||
|
||
self::assertEquals($expectedBaseline, $config->baseline); | ||
} | ||
|
||
/** @psalm-return iterable<string, array{string, Baseline}> */ | ||
public function validConfigurations(): iterable | ||
{ | ||
yield 'empty object' => ['{}', Baseline::empty()]; | ||
yield 'empty array' => ['[]', Baseline::empty()]; | ||
yield 'empty baseline property' => ['{"baseline":[]}', Baseline::empty()]; | ||
|
||
yield 'baseline with strings' => [ | ||
<<<'JSON' | ||
{"baseline": ["#\\[BC\\] CHANGED: The parameter \\$a#"]} | ||
JSON, | ||
Baseline::fromList('#\[BC\] CHANGED: The parameter \$a#'), | ||
]; | ||
|
||
yield 'random properties are ignored' => [ | ||
<<<'JSON' | ||
{ | ||
"baseline": ["#\\[BC\\] CHANGED: The parameter \\$a#"], | ||
"random": false | ||
} | ||
JSON, | ||
Baseline::fromList('#\[BC\] CHANGED: The parameter \$a#'), | ||
]; | ||
} | ||
|
||
/** @dataProvider invalidConfigurations */ | ||
public function testExceptionShouldBeTriggeredOnInvalidConfiguration( | ||
string $jsonContents, | ||
): void { | ||
$this->expectException(RuntimeException::class); | ||
|
||
Configuration::fromJson($jsonContents); | ||
} | ||
|
||
/** @psalm-return iterable<string, array{string}> */ | ||
public function invalidConfigurations(): iterable | ||
{ | ||
yield 'empty content' => ['']; | ||
yield 'empty string' => ['""']; | ||
yield 'int' => ['0']; | ||
yield 'float' => ['0.1']; | ||
yield 'boolean' => ['false']; | ||
yield 'baseline with string' => ['{"baseline": "this should be a list"}']; | ||
yield 'baseline with int' => ['{"baseline": 0}']; | ||
yield 'baseline with float' => ['{"baseline": 0.0}']; | ||
yield 'baseline with bool' => ['{"baseline": true}']; | ||
yield 'baseline with array of float' => ['{"baseline": [0.0]}']; | ||
yield 'baseline with array of bool' => ['{"baseline": [false]}']; | ||
yield 'baseline with array of object' => ['{"baseline": [{}]}']; | ||
} | ||
} |