-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeDumperTest.php
113 lines (98 loc) · 4.53 KB
/
CodeDumperTest.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
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace EventSauce\EventSourcing\CodeGeneration;
use PHPUnit\Framework\TestCase;
use function file_get_contents;
class CodeDumperTest extends TestCase
{
/**
* @test
* @dataProvider definitionProvider
*/
public function dumping_a_definition(DefinitionGroup $definitionGroup, string $fixtureFile): void
{
$dumper = new CodeDumper();
$actual = $dumper->dump($definitionGroup);
// file_put_contents(__DIR__ . '/Fixtures/' . $fixtureFile . 'Fixture.php', $actual);
$expected = file_get_contents(__DIR__ . '/Fixtures/' . $fixtureFile . 'Fixture.php');
$this->assertEquals($expected, $actual, "Expect {$fixtureFile} to match generated code.");
}
/**
* @test
* @dataProvider definitionProvider
*/
public function dumping_a_definition_with_types_properties(DefinitionGroup $definitionGroup, string $fixtureFile): void
{
$dumper = new CodeDumper();
$actual = $dumper->dump($definitionGroup);
// file_put_contents(__DIR__ . '/Fixtures/' . $fixtureFile . 'TypePropertiesFixture.php', $actual);
$expected = file_get_contents(__DIR__ . '/Fixtures/' . $fixtureFile . 'TypePropertiesFixture.php');
$this->assertEquals($expected, $actual, "Expect {$fixtureFile} to match generated code.");
}
public static function definitionProvider(): iterable
{
/* test case 1 */
$simpleDefinitionGroup = DefinitionGroup::create('Simple\\Definition\\Group');
$simpleDefinitionGroup->event('SomethingHappened')
->field('what', 'string', 'Example Event')
->field('yolo', 'bool', 'true');
/* test case 2 */
$multipleEventsDefinitionGroup = DefinitionGroup::create('Multiple\\Events\\DefinitionGroup');
$multipleEventsDefinitionGroup->event('FirstEvent')
->field('firstField', 'string', 'FIRST');
$multipleEventsDefinitionGroup->event('SecondEvent')
->field('secondField', 'string', 'SECOND');
/* test case 3 */
$definitionGroupWithDefaults = DefinitionGroup::create('Group\\With\\Defaults');
$definitionGroupWithDefaults->fieldDefault('description', 'string', 'This is a description.');
$definitionGroupWithDefaults->event('EventWithDescription')
->field('description', 'string');
/* test case 4 */
$groupWithFieldSerialization = DefinitionGroup::create('Group\\With\\FieldDeserialization');
$groupWithFieldSerialization->fieldSerializer('items', <<<EOF
array_map(function (\$item) {
return \$item['property'];
}, {param})
EOF
);
$groupWithFieldSerialization->fieldDeserializer('items', <<<EOF
array_map(function (\$property) {
return ['property' => \$property];
}, {param})
EOF
);
$groupWithFieldSerialization->event('WithFieldSerializers')
->field('items', 'array');
/* test case 5 */
$definitionGroupWithCommand = DefinitionGroup::create('With\Commands');
$definitionGroupWithCommand->command('DoSomething')
->field('reason', 'string', 'Because reasons.');
/* test case 6 */
$groupWithFieldSerializationFromEvent = DefinitionGroup::create('With\\EventFieldSerialization');
$groupWithFieldSerializationFromEvent->event('EventName')
->field('title', 'string', 'Title')
->fieldSerializer('title', <<<EOF
strtoupper({param})
EOF
)->fieldDeserializer('title', <<<EOF
strtolower({param})
EOF
);
$groupWithEventWithTwoRequiredFields = DefinitionGroup::create('With\\ManyRequiredFields');
$groupWithEventWithTwoRequiredFields->event('ThisOne')
->field('title', 'string')
->field('description', 'string');
$groupWithEventWithNoFields = DefinitionGroup::create('Without\Fields');
$groupWithEventWithNoFields->event('WithoutFields');
return [
[$simpleDefinitionGroup, 'simpleDefinitionGroup'],
[$multipleEventsDefinitionGroup, 'multipleEventsDefinitionGroup'],
[$definitionGroupWithDefaults, 'definitionGroupWithDefaults'],
[$groupWithFieldSerialization, 'groupWithFieldSerialization'],
[$definitionGroupWithCommand, 'definitionGroupWithCommand'],
[$groupWithFieldSerializationFromEvent, 'groupWithFieldSerializationFromEvent'],
[$groupWithEventWithTwoRequiredFields, 'groupWithEventWithTwoRequiredFields'],
[$groupWithEventWithNoFields, 'groupWithEventWithNoFields'],
];
}
}