-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
YamlDefinitionLoaderTest.php
105 lines (92 loc) · 3.38 KB
/
YamlDefinitionLoaderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace EventSauce\EventSourcing\CodeGeneration;
use const false;
use InvalidArgumentException;
use LogicException;
use OutOfBoundsException;
use PHPUnit\Framework\TestCase;
use function file_get_contents;
class YamlDefinitionLoaderTest extends TestCase
{
/**
* @test
*/
public function it_can_load_yaml_files(): void
{
$loader = new YamlDefinitionLoader();
$this->assertTrue($loader->canLoad('a_yaml_file.yaml'));
$this->assertTrue($loader->canLoad('a_yaml_file.yml'));
$this->assertFalse($loader->canLoad('not_a_yaml_file.php'));
}
/**
* @test
* @dataProvider definitionProvider
*/
public function generating_code_from_yaml(string $source, string $output, bool $withHelpers = true, bool $withSerializers = true): void
{
$loader = new YamlDefinitionLoader();
$definitionGroup = $loader->load($source);
$dumper = new CodeDumper();
$code = $dumper->dump($definitionGroup, $withHelpers, $withSerializers);
// file_put_contents($output, $code);
$expected = file_get_contents($output);
$this->assertEquals($expected, $code);
}
public static function definitionProvider(): iterable
{
yield [__DIR__ . '/Fixtures/exampleDefinition.yaml', __DIR__ . '/Fixtures/definedWithYamlFixture.php'];
yield [__DIR__ . '/Fixtures/exampleDefinitionWithoutHelpers.yaml', __DIR__ . '/Fixtures/definedWithoutHelpersInYamlFixture.php', false];
yield [__DIR__ . '/Fixtures/definitionWithFieldsFromOtherDefinitions.yaml', __DIR__ . '/Fixtures/definitionWithFieldsFromOtherDefinitionsFixture.php', false];
yield [__DIR__ . '/Fixtures/commands-with-interfaces.yaml', __DIR__ . '/Fixtures/commandsWithInterfaces.php', false];
}
/**
* @test
*/
public function trying_to_inherit_fields_from_unknown_type(): void
{
$this->expectException(LogicException::class);
$loader = new YamlDefinitionLoader();
$definitionGroup = $loader->load(__DIR__ . '/Fixtures/inheritFieldsFromUnknownType.yaml');
$dumper = new CodeDumper();
$dumper->dump($definitionGroup, false);
}
/**
* @test
*/
public function trying_to_use_non_defined_interfaces(): void
{
$this->expectException(OutOfBoundsException::class);
$loader = new YamlDefinitionLoader();
$definitionGroup = $loader->load(__DIR__ . '/Fixtures/commands-with-non-existing-interfaces.yaml');
$dumper = new CodeDumper();
$dumper->dump($definitionGroup, false);
}
/**
* @test
*/
public function trying_to_use_non_existing_interfaces(): void
{
$this->expectException(LogicException::class);
$loader = new YamlDefinitionLoader();
$loader->load(__DIR__ . '/Fixtures/non-existing-interface.yaml');
}
/**
* @test
*/
public function loading_a_yaml_thats_not_an_array(): void
{
$this->expectException(InvalidArgumentException::class);
$loader = new YamlDefinitionLoader();
$loader->load(__DIR__ . '/Fixtures/no-array.yml');
}
/**
* @test
*/
public function loading_a_yaml_that_does_not_exist(): void
{
$this->expectException(InvalidArgumentException::class);
$loader = new YamlDefinitionLoader();
$loader->load(__DIR__ . '/Fixtures/empty.yml');
}
}