1
+ <?php
2
+
3
+ namespace Tests \Feature ;
4
+
5
+ use Blueprint \Blueprint ;
6
+ use Symfony \Component \Yaml \Exception \ParseException ;
7
+ use Tests \TestCase ;
8
+
9
+ class BlueprintTest extends TestCase
10
+ {
11
+ /**
12
+ * @var Blueprint
13
+ */
14
+ private $ subject ;
15
+
16
+ protected function setUp ()
17
+ {
18
+ parent ::setUp ();
19
+
20
+ $ this ->subject = new Blueprint ();
21
+ }
22
+
23
+ /**
24
+ * @test
25
+ */
26
+ public function it_parses_models ()
27
+ {
28
+ $ blueprint = $ this ->fixture ('definitions/models-only.bp ' );
29
+
30
+ $ this ->assertEquals ([
31
+ 'models ' => [
32
+ 'ModelOne ' => [
33
+ 'column ' => 'datatype modifier ' ,
34
+ ],
35
+ 'ModelTwo ' => [
36
+ 'column ' => 'datatype ' ,
37
+ 'another_column ' => 'datatype modifier ' ,
38
+ ],
39
+ ],
40
+ ], $ this ->subject ->parse ($ blueprint ));
41
+ }
42
+
43
+ /**
44
+ * @test
45
+ */
46
+ public function it_parses_controllers ()
47
+ {
48
+ $ blueprint = $ this ->fixture ('definitions/controllers-only.bp ' );
49
+
50
+ $ this ->assertEquals ([
51
+ 'controllers ' => [
52
+ 'UserController ' => [
53
+ 'index ' => [
54
+ 'action ' => 'detail '
55
+ ],
56
+ 'create ' => [
57
+ 'action ' => 'additional detail '
58
+ ],
59
+ ],
60
+ 'RoleController ' => [
61
+ 'index ' => [
62
+ 'action ' => 'detail ' ,
63
+ 'another_action ' => 'so much detail ' ,
64
+ ],
65
+ ],
66
+ ],
67
+ ], $ this ->subject ->parse ($ blueprint ));
68
+ }
69
+
70
+ /**
71
+ * @test
72
+ */
73
+ public function it_parses_shorthands ()
74
+ {
75
+ $ blueprint = $ this ->fixture ('definitions/shorthands.bp ' );
76
+
77
+ $ this ->assertEquals ([
78
+ 'models ' => [
79
+ 'Name ' => [
80
+ 'id ' => 'id ' ,
81
+ 'timestamps ' => 'timestamps ' ,
82
+ ],
83
+ ],
84
+ ], $ this ->subject ->parse ($ blueprint ));
85
+ }
86
+
87
+ /**
88
+ * @test
89
+ */
90
+ public function it_parses_the_readme_example ()
91
+ {
92
+ $ blueprint = $ this ->fixture ('definitions/readme-example.bp ' );
93
+
94
+ $ this ->assertEquals ([
95
+ 'models ' => [
96
+ 'Post ' => [
97
+ 'id ' => 'id ' ,
98
+ 'title ' => 'string ' ,
99
+ 'content ' => 'bigtext ' ,
100
+ 'published_at ' => 'nullable timestamp ' ,
101
+ 'timestamps ' => 'timestamps '
102
+ ],
103
+ ],
104
+ 'controllers ' => [
105
+ 'Post ' => [
106
+ 'index ' => [
107
+ 'query ' => 'all posts ' ,
108
+ 'render ' => 'post.index with posts ' ,
109
+ ],
110
+ 'store ' => [
111
+ 'validate ' => 'title, content ' ,
112
+ 'save ' => 'post ' ,
113
+ 'send ' => 'ReviewNotifcation to post.author ' ,
114
+ 'queue ' => 'SyncMedia ' ,
115
+ 'flash ' => 'post.title ' ,
116
+ 'redirect ' => 'post.index ' ,
117
+ ],
118
+ ],
119
+ ],
120
+ ], $ this ->subject ->parse ($ blueprint ));
121
+ }
122
+
123
+ /**
124
+ * @test
125
+ */
126
+ public function it_throws_a_custom_error_when_parsing_fails ()
127
+ {
128
+ $ this ->expectException (ParseException::class);
129
+
130
+ $ blueprint = $ this ->fixture ('definitions/invalid.bp ' );
131
+
132
+ $ this ->subject ->parse ($ blueprint );
133
+ }
134
+
135
+ /**
136
+ * @test
137
+ */
138
+ public function analyze_return_default_tree_for_empty_tokens ()
139
+ {
140
+ $ tokens = [];
141
+
142
+ $ this ->assertEquals ([
143
+ 'models ' => [],
144
+ 'controllers ' => []
145
+ ],
146
+ $ this ->subject ->analyze ($ tokens ));
147
+ }
148
+
149
+ /**
150
+ * @test
151
+ */
152
+ public function analyze_uses_register_lexers_to_analyze_tokens ()
153
+ {
154
+ $ lexer = \Mockery::mock ();
155
+ $ tokens = ['tokens ' => ['are ' , 'here ' ]];
156
+ $ lexer ->expects ('analyze ' )
157
+ ->with ($ tokens )
158
+ ->andReturn (['mock ' => 'lexer ' ]);
159
+
160
+ $ this ->subject ->registerLexer ($ lexer );
161
+
162
+ $ this ->assertEquals ([
163
+ 'models ' => [],
164
+ 'controllers ' => [],
165
+ 'mock ' => 'lexer '
166
+ ], $ this ->subject ->analyze ($ tokens ));
167
+ }
168
+
169
+ /**
170
+ * @test
171
+ */
172
+ public function generate_uses_register_generators_to_generate_code ()
173
+ {
174
+ $ generator = \Mockery::mock ();
175
+ $ tree = ['branch ' => ['code ' , 'attributes ' ]];
176
+ $ generator ->expects ('generate ' )
177
+ ->with ($ tree );
178
+
179
+ $ this ->subject ->registerGenerator ($ generator );
180
+
181
+ $ this ->subject ->generate ($ tree );
182
+ }
183
+ }
0 commit comments