@@ -148,4 +148,91 @@ public function testAddAttribute()
148
148
$ this ->assertSame ('foo="bar baz" ' , (string ) $ this ->stimulusAttributes );
149
149
$ this ->assertSame (['foo ' => 'bar baz ' ], $ this ->stimulusAttributes ->toArray ());
150
150
}
151
+
152
+ /**
153
+ * @dataProvider provideAddComplexActionData
154
+ */
155
+ public function testAddComplexAction (string $ controllerName , string $ actionName , ?string $ eventName , string $ expectedAction ): void
156
+ {
157
+ $ this ->stimulusAttributes ->addAction ($ controllerName , $ actionName , $ eventName );
158
+ $ attributesHtml = (string ) $ this ->stimulusAttributes ;
159
+ self ::assertSame (\sprintf ('data-action="%s" ' , $ expectedAction ), $ attributesHtml );
160
+ }
161
+
162
+ /**
163
+ * @return iterable<array{
164
+ * controllerName: string,
165
+ * actionName: string,
166
+ * eventName: ?string,
167
+ * expectedAction: string,
168
+ * }>
169
+ */
170
+ public static function provideAddComplexActionData (): iterable
171
+ {
172
+ // basic datasets
173
+ yield 'foo#bar ' => [
174
+ 'controllerName ' => 'foo ' ,
175
+ 'actionName ' => 'bar ' ,
176
+ 'eventName ' => null ,
177
+ 'expectedAction ' => 'foo#bar ' ,
178
+ ];
179
+ yield 'baz->foo#bar ' => [
180
+ 'controllerName ' => 'foo ' ,
181
+ 'actionName ' => 'bar ' ,
182
+ 'eventName ' => 'baz ' ,
183
+ 'expectedAction ' => 'baz->foo#bar ' ,
184
+ ];
185
+
186
+ // datasets from https://github.com/hotwired/stimulus
187
+ yield 'keydown.esc@document->a#log ' => [
188
+ 'controllerName ' => 'a ' ,
189
+ 'actionName ' => 'log ' ,
190
+ 'eventName ' => 'keydown.esc@document ' ,
191
+ 'expectedAction ' => 'keydown.esc@document->a#log ' ,
192
+ ];
193
+ yield 'keydown.enter->a#log ' => [
194
+ 'controllerName ' => 'a ' ,
195
+ 'actionName ' => 'log ' ,
196
+ 'eventName ' => 'keydown.enter ' ,
197
+ 'expectedAction ' => 'keydown.enter->a#log ' ,
198
+ ];
199
+ yield 'keydown.shift+a->a#log ' => [
200
+ 'controllerName ' => 'a ' ,
201
+ 'actionName ' => 'log ' ,
202
+ 'eventName ' => 'keydown.shift+a ' ,
203
+ 'expectedAction ' => 'keydown.shift+a->a#log ' ,
204
+ ];
205
+ yield 'keydown@window->c#log ' => [
206
+ 'controllerName ' => 'c ' ,
207
+ 'actionName ' => 'log ' ,
208
+ 'eventName ' => 'keydown@window ' ,
209
+ 'expectedAction ' => 'keydown@window->c#log ' ,
210
+ ];
211
+ yield 'click->c#log:once ' => [
212
+ 'controllerName ' => 'c ' ,
213
+ 'actionName ' => 'log:once ' ,
214
+ 'eventName ' => 'click ' ,
215
+ 'expectedAction ' => 'click->c#log:once ' ,
216
+ ];
217
+
218
+ // extended datasets
219
+ yield 'vue:mount->foo#bar:passive ' => [
220
+ 'controllerName ' => 'foo ' ,
221
+ 'actionName ' => 'bar:passive ' ,
222
+ 'eventName ' => 'vue:mount ' ,
223
+ 'expectedAction ' => 'vue:mount->foo#bar:passive ' ,
224
+ ];
225
+ yield 'foo--controller-1:baz->bar--controller-2#log ' => [
226
+ 'controllerName ' => '@bar/controller_2 ' ,
227
+ 'actionName ' => 'log ' ,
228
+ 'eventName ' => '@foo/controller_1:baz ' ,
229
+ 'expectedAction ' => 'foo--controller-1:baz->bar--controller-2#log ' ,
230
+ ];
231
+ yield 'foo--controller-1:baz@document->bar--controller-2#log:capture ' => [
232
+ 'controllerName ' => '@bar/controller_2 ' ,
233
+ 'actionName ' => 'log:capture ' ,
234
+ 'eventName ' => '@foo/controller_1:baz@document ' ,
235
+ 'expectedAction ' => 'foo--controller-1:baz@document->bar--controller-2#log:capture ' ,
236
+ ];
237
+ }
151
238
}
0 commit comments