Skip to content

Commit c38ecba

Browse files
committed
minor #185 [ci] fix flex plugin & legacy stimulus tests (jrushlow)
This PR was squashed before being merged into the main branch. Discussion ---------- [ci] fix flex plugin & legacy stimulus tests - adds `flex` to composers allowed plugins - moves Stimulus `action`, `controller`, & `target` test cases w/ array's as the first arg to legacy tests. (silence deprecation's in CI) Commits ------- 238a8ee [ci] fix flex plugin & legacy stimulus tests
2 parents 1fc3863 + 238a8ee commit c38ecba

File tree

3 files changed

+137
-71
lines changed

3 files changed

+137
-71
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ jobs:
3636
coverage: "none"
3737
php-version: "${{ matrix.php-version }}"
3838

39+
- name: Allow Flex Plugin
40+
run: |
41+
composer global config --no-plugins allow-plugins.symfony/flex true
42+
3943
- name: Install Global Dependencies
4044
run: |
4145
composer global require --no-progress --no-scripts --no-plugins symfony/flex @dev

src/Dto/AbstractStimulusDto.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function getFormattedValue($value)
3939
{
4040
if ($value instanceof \Stringable || (\is_object($value) && \is_callable([$value, '__toString']))) {
4141
$value = (string) $value;
42-
} elseif (!is_scalar($value)) {
42+
} elseif (!\is_scalar($value)) {
4343
$value = json_encode($value);
4444
} elseif (\is_bool($value)) {
4545
$value = $value ? 'true' : 'false';

tests/IntegrationTest.php

Lines changed: 132 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,68 @@ public function testAutowireDefaultBuildArgument()
203203
}
204204

205205
public function provideRenderStimulusController()
206+
{
207+
yield 'normalize-names' => [
208+
'dataOrControllerName' => '@symfony/ux-dropzone/dropzone',
209+
'controllerValues' => [
210+
'my"Key"' => true,
211+
],
212+
'expectedString' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="true"',
213+
'expectedArray' => ['data-controller' => 'symfony--ux-dropzone--dropzone', 'data-symfony--ux-dropzone--dropzone-my-key-value' => 'true'],
214+
];
215+
216+
yield 'short-single-controller-no-data' => [
217+
'dataOrControllerName' => 'my-controller',
218+
'controllerValues' => [],
219+
'expectedString' => 'data-controller="my-controller"',
220+
'expectedArray' => ['data-controller' => 'my-controller'],
221+
];
222+
223+
yield 'short-single-controller-with-data' => [
224+
'dataOrControllerName' => 'my-controller',
225+
'controllerValues' => ['myValue' => 'scalar-value'],
226+
'expectedString' => 'data-controller="my-controller" data-my-controller-my-value-value="scalar-value"',
227+
'expectedArray' => ['data-controller' => 'my-controller', 'data-my-controller-my-value-value' => 'scalar-value'],
228+
];
229+
230+
yield 'false-attribute-value-renders-false' => [
231+
'dataOrControllerName' => 'false-controller',
232+
'controllerValues' => ['isEnabled' => false],
233+
'expectedString' => 'data-controller="false-controller" data-false-controller-is-enabled-value="false"',
234+
'expectedArray' => ['data-controller' => 'false-controller', 'data-false-controller-is-enabled-value' => 'false'],
235+
];
236+
237+
yield 'true-attribute-value-renders-true' => [
238+
'dataOrControllerName' => 'true-controller',
239+
'controllerValues' => ['isEnabled' => true],
240+
'expectedString' => 'data-controller="true-controller" data-true-controller-is-enabled-value="true"',
241+
'expectedArray' => ['data-controller' => 'true-controller', 'data-true-controller-is-enabled-value' => 'true'],
242+
];
243+
244+
yield 'null-attribute-value-does-not-render' => [
245+
'dataOrControllerName' => 'null-controller',
246+
'controllerValues' => ['firstName' => null],
247+
'expectedString' => 'data-controller="null-controller"',
248+
'expectedArray' => ['data-controller' => 'null-controller'],
249+
];
250+
}
251+
252+
/**
253+
* @dataProvider provideRenderStimulusController
254+
*/
255+
public function testRenderStimulusController($dataOrControllerName, array $controllerValues, string $expectedString, array $expectedArray)
256+
{
257+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
258+
$kernel->boot();
259+
$twig = $this->getTwigEnvironmentFromBootedKernel($kernel);
260+
261+
$extension = new StimulusTwigExtension();
262+
$dto = $extension->renderStimulusController($twig, $dataOrControllerName, $controllerValues);
263+
$this->assertSame($expectedString, (string) $dto);
264+
$this->assertSame($expectedArray, $dto->toArray());
265+
}
266+
267+
public function provideLegacyRenderMultipleStimulusControllers()
206268
{
207269
yield 'empty' => [
208270
'dataOrControllerName' => [],
@@ -279,47 +341,13 @@ public function provideRenderStimulusController()
279341
'expectedString' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="true"',
280342
'expectedArray' => ['data-controller' => 'symfony--ux-dropzone--dropzone', 'data-symfony--ux-dropzone--dropzone-my-key-value' => 'true'],
281343
];
282-
283-
yield 'short-single-controller-no-data' => [
284-
'dataOrControllerName' => 'my-controller',
285-
'controllerValues' => [],
286-
'expectedString' => 'data-controller="my-controller"',
287-
'expectedArray' => ['data-controller' => 'my-controller'],
288-
];
289-
290-
yield 'short-single-controller-with-data' => [
291-
'dataOrControllerName' => 'my-controller',
292-
'controllerValues' => ['myValue' => 'scalar-value'],
293-
'expectedString' => 'data-controller="my-controller" data-my-controller-my-value-value="scalar-value"',
294-
'expectedArray' => ['data-controller' => 'my-controller', 'data-my-controller-my-value-value' => 'scalar-value'],
295-
];
296-
297-
yield 'false-attribute-value-renders-false' => [
298-
'dataOrControllerName' => 'false-controller',
299-
'controllerValues' => ['isEnabled' => false],
300-
'expectedString' => 'data-controller="false-controller" data-false-controller-is-enabled-value="false"',
301-
'expectedArray' => ['data-controller' => 'false-controller', 'data-false-controller-is-enabled-value' => 'false'],
302-
];
303-
304-
yield 'true-attribute-value-renders-true' => [
305-
'dataOrControllerName' => 'true-controller',
306-
'controllerValues' => ['isEnabled' => true],
307-
'expectedString' => 'data-controller="true-controller" data-true-controller-is-enabled-value="true"',
308-
'expectedArray' => ['data-controller' => 'true-controller', 'data-true-controller-is-enabled-value' => 'true'],
309-
];
310-
311-
yield 'null-attribute-value-does-not-render' => [
312-
'dataOrControllerName' => 'null-controller',
313-
'controllerValues' => ['firstName' => null],
314-
'expectedString' => 'data-controller="null-controller"',
315-
'expectedArray' => ['data-controller' => 'null-controller'],
316-
];
317344
}
318345

319346
/**
320-
* @dataProvider provideRenderStimulusController
347+
* @dataProvider provideLegacyRenderMultipleStimulusControllers
348+
* @legacy
321349
*/
322-
public function testRenderStimulusController($dataOrControllerName, array $controllerValues, string $expectedString, array $expectedArray)
350+
public function testLegacyRenderMultipleStimulusControllers($dataOrControllerName, array $controllerValues, string $expectedString, array $expectedArray)
323351
{
324352
$kernel = new WebpackEncoreIntegrationTestKernel(true);
325353
$kernel->boot();
@@ -374,6 +402,42 @@ public function provideRenderStimulusAction()
374402
'expectedArray' => ['data-action' => 'my-controller#onClick', 'data-my-controller-bool-param-param' => 'true', 'data-my-controller-int-param-param' => '4', 'data-my-controller-string-param-param' => 'test'],
375403
];
376404

405+
yield 'normalize-name, with default event' => [
406+
'dataOrControllerName' => '@symfony/ux-dropzone/dropzone',
407+
'actionName' => 'onClick',
408+
'eventName' => null,
409+
'parameters' => [],
410+
'expectedString' => 'data-action="symfony--ux-dropzone--dropzone#onClick"',
411+
'expectedArray' => ['data-action' => 'symfony--ux-dropzone--dropzone#onClick'],
412+
];
413+
414+
yield 'normalize-name, with custom event' => [
415+
'dataOrControllerName' => '@symfony/ux-dropzone/dropzone',
416+
'actionName' => 'onClick',
417+
'eventName' => 'click',
418+
'parameters' => [],
419+
'expectedString' => 'data-action="click->symfony--ux-dropzone--dropzone#onClick"',
420+
'expectedArray' => ['data-action' => 'click->symfony--ux-dropzone--dropzone#onClick'],
421+
];
422+
}
423+
424+
/**
425+
* @dataProvider provideRenderStimulusAction
426+
*/
427+
public function testRenderStimulusAction($dataOrControllerName, ?string $actionName, ?string $eventName, array $parameters, string $expectedString, array $expectedArray)
428+
{
429+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
430+
$kernel->boot();
431+
$twig = $this->getTwigEnvironmentFromBootedKernel($kernel);
432+
433+
$extension = new StimulusTwigExtension();
434+
$dto = $extension->renderStimulusAction($twig, $dataOrControllerName, $actionName, $eventName, $parameters);
435+
$this->assertSame($expectedString, (string) $dto);
436+
$this->assertSame($expectedArray, $dto->toArray());
437+
}
438+
439+
public function provideLegacyRenderMultipleStimulusAction(): \Generator
440+
{
377441
yield 'multiple actions, with default event' => [
378442
'dataOrControllerName' => [
379443
'my-controller' => 'onClick',
@@ -414,30 +478,13 @@ public function provideRenderStimulusAction()
414478
'expectedString' => 'data-action="click->my-controller#onClick my-second-controller#onClick click->my-second-controller#onAnotherClick change->my-second-controller#onSomethingElse resize@window->resize-controller#onWindowResize click->foo--bar-controller#onClick"',
415479
'expectedArray' => ['data-action' => 'click->my-controller#onClick my-second-controller#onClick click->my-second-controller#onAnotherClick change->my-second-controller#onSomethingElse resize@window->resize-controller#onWindowResize click->foo--bar-controller#onClick'],
416480
];
417-
418-
yield 'normalize-name, with default event' => [
419-
'dataOrControllerName' => '@symfony/ux-dropzone/dropzone',
420-
'actionName' => 'onClick',
421-
'eventName' => null,
422-
'parameters' => [],
423-
'expectedString' => 'data-action="symfony--ux-dropzone--dropzone#onClick"',
424-
'expectedArray' => ['data-action' => 'symfony--ux-dropzone--dropzone#onClick'],
425-
];
426-
427-
yield 'normalize-name, with custom event' => [
428-
'dataOrControllerName' => '@symfony/ux-dropzone/dropzone',
429-
'actionName' => 'onClick',
430-
'eventName' => 'click',
431-
'parameters' => [],
432-
'expectedString' => 'data-action="click->symfony--ux-dropzone--dropzone#onClick"',
433-
'expectedArray' => ['data-action' => 'click->symfony--ux-dropzone--dropzone#onClick'],
434-
];
435481
}
436482

437483
/**
438-
* @dataProvider provideRenderStimulusAction
484+
* @dataProvider provideLegacyRenderMultipleStimulusAction
485+
* @legacy
439486
*/
440-
public function testRenderStimulusAction($dataOrControllerName, ?string $actionName, ?string $eventName, array $parameters, string $expectedString, array $expectedArray)
487+
public function testLegacyRenderMultipleStimulusActions($dataOrControllerName, ?string $actionName, ?string $eventName, array $parameters, string $expectedString, array $expectedArray)
441488
{
442489
$kernel = new WebpackEncoreIntegrationTestKernel(true);
443490
$kernel->boot();
@@ -478,19 +525,6 @@ public function provideRenderStimulusTarget()
478525
'expectedString' => 'data-symfony--ux-dropzone--dropzone-target="myTarget"',
479526
'expectedArray' => ['data-symfony--ux-dropzone--dropzone-target' => 'myTarget'],
480527
];
481-
482-
yield 'multiple' => [
483-
'dataOrControllerName' => [
484-
'my-controller' => 'myTarget',
485-
'@symfony/ux-dropzone/dropzone' => 'anotherTarget fooTarget',
486-
],
487-
'targetName' => null,
488-
'expectedString' => 'data-my-controller-target="myTarget" data-symfony--ux-dropzone--dropzone-target="anotherTarget fooTarget"',
489-
'expectedArray' => [
490-
'data-my-controller-target' => 'myTarget',
491-
'data-symfony--ux-dropzone--dropzone-target' => 'anotherTarget fooTarget',
492-
],
493-
];
494528
}
495529

496530
/**
@@ -508,6 +542,34 @@ public function testRenderStimulusTarget($dataOrControllerName, ?string $targetN
508542
$this->assertSame($expectedArray, $dto->toArray());
509543
}
510544

545+
/**
546+
* @legacy
547+
*/
548+
public function testLegacyRenderMultipleStimulusTargets()
549+
{
550+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
551+
$kernel->boot();
552+
$twig = $this->getTwigEnvironmentFromBootedKernel($kernel);
553+
554+
$extension = new StimulusTwigExtension();
555+
$dto = $extension->renderStimulusTarget($twig, [
556+
'my-controller' => 'myTarget',
557+
'@symfony/ux-dropzone/dropzone' => 'anotherTarget fooTarget',
558+
]);
559+
560+
$this->assertSame(
561+
'data-my-controller-target="myTarget" data-symfony--ux-dropzone--dropzone-target="anotherTarget fooTarget"',
562+
(string) $dto
563+
);
564+
565+
$this->assertSame([
566+
'data-my-controller-target' => 'myTarget',
567+
'data-symfony--ux-dropzone--dropzone-target' => 'anotherTarget fooTarget',
568+
],
569+
$dto->toArray()
570+
);
571+
}
572+
511573
public function testAppendStimulusTarget()
512574
{
513575
$kernel = new WebpackEncoreIntegrationTestKernel(true);

0 commit comments

Comments
 (0)