Skip to content

Commit c0a4e22

Browse files
committed
Fix tests
1 parent 55ddee8 commit c0a4e22

File tree

8 files changed

+171
-122
lines changed

8 files changed

+171
-122
lines changed

src/Illuminate/View/AnonymousComponent.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Illuminate\View;
44

5-
use ReflectionClass;
6-
75
class AnonymousComponent extends Component
86
{
97
/**
@@ -13,33 +11,6 @@ class AnonymousComponent extends Component
1311
*/
1412
protected $view;
1513

16-
/**
17-
* The component data.
18-
*
19-
* @var array
20-
*/
21-
protected $data = [];
22-
23-
protected static array $ignoredParameterNames = [];
24-
25-
/**
26-
* Fetch a cached set of anonymous component constructor parameter names to exclude.
27-
*/
28-
public static function ignoredParameterNames(): array
29-
{
30-
if (!isset(static::$ignoredParameterNames)) {
31-
$constructor = (new ReflectionClass(
32-
static::class
33-
))->getConstructor();
34-
35-
static::$ignoredParameterNames = collect($constructor->getParameters())
36-
->map->getName()
37-
->all();
38-
}
39-
40-
return static::$ignoredParameterNames;
41-
}
42-
4314
/**
4415
* Create a new anonymous component instance.
4516
*

src/Illuminate/View/Compilers/ComponentTagCompiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ protected function componentString(string $component, array $attributes)
260260

261261
return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).'])
262262
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
263-
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
263+
<?php $attributes = $attributes->except(\\'.$class.'::ignoredParameterNames()); ?>
264264
<?php endif; ?>
265265
<?php $component->withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>';
266266
}

src/Illuminate/View/Compilers/Concerns/CompilesComponents.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,9 @@ protected function compileEndComponentFirst()
157157
*/
158158
protected function compileProps($expression)
159159
{
160-
return "<?php
161-
\$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag;
160+
return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag;
162161
163162
\$__newAttributes = [];
164-
165163
\$__propNames = \Illuminate\Support\Arr::extractPropNames({$expression});
166164
167165
foreach (\$attributes->all() as \$__key => \$__value) {

src/Illuminate/View/Component.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,37 @@ abstract class Component
7575
*/
7676
protected static $constructorParametersCache = [];
7777

78+
/**
79+
* The component data.
80+
*
81+
* @var array
82+
*/
83+
protected $data = [];
84+
85+
protected static array $ignoredParameterNames = [];
86+
87+
/**
88+
* Fetch a cached set of anonymous component constructor parameter names to exclude.
89+
*/
90+
public static function ignoredParameterNames(): array
91+
{
92+
if (! isset(static::$ignoredParameterNames[static::class])) {
93+
$constructor = (new ReflectionClass(
94+
static::class
95+
))->getConstructor();
96+
97+
if (! $constructor) {
98+
return static::$ignoredParameterNames[static::class] = [];
99+
}
100+
101+
static::$ignoredParameterNames[static::class] = collect($constructor->getParameters())
102+
->map->getName()
103+
->all();
104+
}
105+
106+
return static::$ignoredParameterNames[static::class];
107+
}
108+
78109
/**
79110
* Get the view / view contents that represent the component.
80111
*

tests/View/Blade/BladeComponentTagCompilerTest.php

Lines changed: 70 additions & 70 deletions
Large diffs are not rendered by default.

tests/View/Blade/BladeComponentsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testClassComponentsAreCompiled()
1818
{
1919
$this->assertSame('<?php if (isset($component)) { $__componentOriginal2dda3d2f2f9b76bd400bf03f0b84e87f = $component; } ?>
2020
<?php if (isset($attributes)) { $__attributesOriginal2dda3d2f2f9b76bd400bf03f0b84e87f = $attributes; } ?>
21-
<?php $component = Illuminate\Tests\View\Blade\ComponentStub::class::resolve(["foo" => "bar"] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>
21+
<?php $component = Illuminate\Tests\View\Blade\ComponentStub::class::resolve(["foo" => "bar"] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
2222
<?php $component->withName(\'test\'); ?>
2323
<?php if ($component->shouldRender()): ?>
2424
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>', $this->compiler->compileString('@component(\'Illuminate\Tests\View\Blade\ComponentStub::class\', \'test\', ["foo" => "bar"])'));

tests/View/Blade/BladePropsTest.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,35 @@ class BladePropsTest extends AbstractBladeTestCase
88
{
99
public function testPropsAreCompiled()
1010
{
11-
$this->assertSame('<?php $attributes ??= new \Illuminate\View\ComponentAttributeBag; ?>
12-
<?php foreach($attributes->onlyProps([\'one\' => true, \'two\' => \'string\']) as $__key => $__value) {
13-
$$__key = $$__key ?? $__value;
14-
} ?>
15-
<?php $attributes = $attributes->exceptProps([\'one\' => true, \'two\' => \'string\']); ?>
16-
<?php foreach (array_filter(([\'one\' => true, \'two\' => \'string\']), \'is_string\', ARRAY_FILTER_USE_KEY) as $__key => $__value) {
11+
$this->assertSame('<?php $attributes ??= new \Illuminate\View\ComponentAttributeBag;
12+
13+
$__newAttributes = [];
14+
$__propNames = \Illuminate\Support\Arr::extractPropNames(([\'one\' => true, \'two\' => \'string\']));
15+
16+
foreach ($attributes->all() as $__key => $__value) {
17+
if (in_array($__key, $__propNames)) {
18+
$$__key = $$__key ?? $__value;
19+
} else {
20+
$__newAttributes[$__key] = $__value;
21+
}
22+
}
23+
24+
$attributes = new \Illuminate\View\ComponentAttributeBag($__newAttributes);
25+
26+
unset($__propNames);
27+
unset($__newAttributes);
28+
29+
foreach (array_filter(([\'one\' => true, \'two\' => \'string\']), \'is_string\', ARRAY_FILTER_USE_KEY) as $__key => $__value) {
1730
$$__key = $$__key ?? $__value;
18-
} ?>
19-
<?php $__defined_vars = get_defined_vars(); ?>
20-
<?php foreach ($attributes as $__key => $__value) {
31+
}
32+
33+
$__defined_vars = get_defined_vars();
34+
35+
foreach ($attributes->all() as $__key => $__value) {
2136
if (array_key_exists($__key, $__defined_vars)) unset($$__key);
22-
} ?>
23-
<?php unset($__defined_vars); ?>', $this->compiler->compileString('@props([\'one\' => true, \'two\' => \'string\'])'));
37+
}
38+
39+
unset($__defined_vars); ?>', $this->compiler->compileString('@props([\'one\' => true, \'two\' => \'string\'])'));
2440
}
2541

2642
public function testPropsAreExtractedFromParentAttributesCorrectly()

tests/View/ViewFactoryTest.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testEnvironmentAddsExtensionWithCustomResolver()
148148
$factory->getEngineResolver()->shouldReceive('register')->once()->with('bar', $resolver);
149149
$factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn('path.foo');
150150
$factory->getEngineResolver()->shouldReceive('resolve')->once()->with('bar')->andReturn($engine = m::mock(Engine::class));
151-
$factory->getDispatcher()->shouldReceive('dispatch');
151+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
152152

153153
$factory->addExtension('foo', 'bar', $resolver);
154154

@@ -185,11 +185,14 @@ public function testPrependedExtensionOverridesExistingExtensions()
185185
public function testCallCreatorsDoesDispatchEventsWhenIsNecessary()
186186
{
187187
$factory = $this->getFactory();
188+
188189
$factory->getDispatcher()
189190
->shouldReceive('listen')
190191
->with('creating: name', m::type(Closure::class))
191192
->once();
192193

194+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
195+
193196
$factory->getDispatcher()
194197
->shouldReceive('dispatch')
195198
->with('creating: name', m::type('array'))
@@ -206,11 +209,14 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessary()
206209
public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespacedWildcards()
207210
{
208211
$factory = $this->getFactory();
212+
209213
$factory->getDispatcher()
210214
->shouldReceive('listen')
211215
->with('creating: namespaced::*', m::type(Closure::class))
212216
->once();
213217

218+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
219+
214220
$factory->getDispatcher()
215221
->shouldReceive('dispatch')
216222
->with('creating: namespaced::my-package-view', m::type('array'))
@@ -227,6 +233,7 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespaced
227233
public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespacedNestedWildcards()
228234
{
229235
$factory = $this->getFactory();
236+
230237
$factory->getDispatcher()
231238
->shouldReceive('listen')
232239
->with('creating: namespaced::*', m::type(Closure::class))
@@ -237,6 +244,8 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespaced
237244
->with('creating: welcome', m::type(Closure::class))
238245
->once();
239246

247+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
248+
240249
$factory->getDispatcher()
241250
->shouldReceive('dispatch')
242251
->with('creating: namespaced::my-package-view', m::type('array'))
@@ -253,11 +262,14 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespaced
253262
public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingWildcards()
254263
{
255264
$factory = $this->getFactory();
265+
256266
$factory->getDispatcher()
257267
->shouldReceive('listen')
258268
->with('creating: *', m::type(Closure::class))
259269
->once();
260270

271+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
272+
261273
$factory->getDispatcher()
262274
->shouldReceive('dispatch')
263275
->with('creating: name', m::type('array'))
@@ -274,11 +286,14 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingWildcards(
274286
public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNormalizedNames()
275287
{
276288
$factory = $this->getFactory();
289+
277290
$factory->getDispatcher()
278291
->shouldReceive('listen')
279292
->with('creating: components.button', m::type(Closure::class))
280293
->once();
281294

295+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
296+
282297
$factory->getDispatcher()
283298
->shouldReceive('dispatch')
284299
->with('creating: components/button', m::type('array'))
@@ -297,11 +312,14 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNormalized
297312
public function testCallComposerDoesDispatchEventsWhenIsNecessary()
298313
{
299314
$factory = $this->getFactory();
315+
300316
$factory->getDispatcher()
301317
->shouldReceive('listen')
302318
->with('composing: name', m::type(Closure::class))
303319
->once();
304320

321+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
322+
305323
$factory->getDispatcher()
306324
->shouldReceive('dispatch')
307325
->with('composing: name', m::type('array'))
@@ -318,11 +336,14 @@ public function testCallComposerDoesDispatchEventsWhenIsNecessary()
318336
public function testCallComposerDoesDispatchEventsWhenIsNecessaryAndUsingTheArrayFormat()
319337
{
320338
$factory = $this->getFactory();
339+
321340
$factory->getDispatcher()
322341
->shouldReceive('listen')
323342
->with('composing: name', m::type(Closure::class))
324343
->once();
325344

345+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
346+
326347
$factory->getDispatcher()
327348
->shouldReceive('dispatch')
328349
->with('composing: name', m::type('array'))
@@ -339,11 +360,14 @@ public function testCallComposerDoesDispatchEventsWhenIsNecessaryAndUsingTheArra
339360
public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNamespacedWildcards()
340361
{
341362
$factory = $this->getFactory();
363+
342364
$factory->getDispatcher()
343365
->shouldReceive('listen')
344366
->with('composing: namespaced::*', m::type(Closure::class))
345367
->once();
346368

369+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
370+
347371
$factory->getDispatcher()
348372
->shouldReceive('dispatch')
349373
->with('composing: namespaced::my-package-view', m::type('array'))
@@ -370,6 +394,8 @@ public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNamespace
370394
->with('composing: welcome', m::type(Closure::class))
371395
->once();
372396

397+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
398+
373399
$factory->getDispatcher()
374400
->shouldReceive('dispatch')
375401
->with('composing: namespaced::my-package-view', m::type('array'))
@@ -386,6 +412,9 @@ public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNamespace
386412
public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingWildcards()
387413
{
388414
$factory = $this->getFactory();
415+
416+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
417+
389418
$factory->getDispatcher()
390419
->shouldReceive('listen')
391420
->with('composing: *', m::type(Closure::class))
@@ -407,6 +436,9 @@ public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingWildcards
407436
public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNormalizedNames()
408437
{
409438
$factory = $this->getFactory();
439+
440+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
441+
410442
$factory->getDispatcher()
411443
->shouldReceive('listen')
412444
->with('composing: components.button', m::type(Closure::class))
@@ -496,6 +528,7 @@ public function testCallComposerCallsProperEvent()
496528

497529
$factory->composer('name', fn () => true);
498530

531+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);
499532
$factory->getDispatcher()->shouldReceive('dispatch')->once()->with('composing: name', [$view]);
500533

501534
$factory->callComposer($view);
@@ -612,7 +645,7 @@ public function testComponentHandling()
612645
$factory = $this->getFactory();
613646
$factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
614647
$factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
615-
$factory->getDispatcher()->shouldReceive('dispatch');
648+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
616649
$factory->startComponent('component', ['name' => 'Taylor']);
617650
$factory->slot('title');
618651
$factory->slot('website', 'laravel.com', []);
@@ -628,7 +661,7 @@ public function testComponentHandlingUsingViewObject()
628661
$factory = $this->getFactory();
629662
$factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
630663
$factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
631-
$factory->getDispatcher()->shouldReceive('dispatch');
664+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
632665
$factory->startComponent($factory->make('component'), ['name' => 'Taylor']);
633666
$factory->slot('title');
634667
$factory->slot('website', 'laravel.com', []);
@@ -644,7 +677,7 @@ public function testComponentHandlingUsingClosure()
644677
$factory = $this->getFactory();
645678
$factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
646679
$factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
647-
$factory->getDispatcher()->shouldReceive('dispatch');
680+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
648681
$factory->startComponent(function ($data) use ($factory) {
649682
$this->assertArrayHasKey('name', $data);
650683
$this->assertSame($data['name'], 'Taylor');
@@ -810,7 +843,7 @@ public function testMakeWithSlashAndDot()
810843
$factory = $this->getFactory();
811844
$factory->getFinder()->shouldReceive('find')->twice()->with('foo.bar')->andReturn('path.php');
812845
$factory->getEngineResolver()->shouldReceive('resolve')->twice()->with('php')->andReturn(m::mock(Engine::class));
813-
$factory->getDispatcher()->shouldReceive('dispatch');
846+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
814847
$factory->make('foo/bar');
815848
$factory->make('foo.bar');
816849
}
@@ -820,7 +853,7 @@ public function testNamespacedViewNamesAreNormalizedProperly()
820853
$factory = $this->getFactory();
821854
$factory->getFinder()->shouldReceive('find')->twice()->with('vendor/package::foo.bar')->andReturn('path.php');
822855
$factory->getEngineResolver()->shouldReceive('resolve')->twice()->with('php')->andReturn(m::mock(Engine::class));
823-
$factory->getDispatcher()->shouldReceive('dispatch');
856+
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
824857
$factory->make('vendor/package::foo/bar');
825858
$factory->make('vendor/package::foo.bar');
826859
}
@@ -848,7 +881,7 @@ public function testExceptionsInSectionsAreThrown()
848881
$factory->getEngineResolver()->shouldReceive('resolve')->twice()->andReturn($engine);
849882
$factory->getFinder()->shouldReceive('find')->once()->with('layout')->andReturn(__DIR__.'/fixtures/section-exception-layout.php');
850883
$factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn(__DIR__.'/fixtures/section-exception.php');
851-
$factory->getDispatcher()->shouldReceive('dispatch')->times(4); // 2 "creating" + 2 "composing"...
884+
$factory->getDispatcher()->shouldReceive('hasListeners')->times(4); // 2 "creating" + 2 "composing"...
852885

853886
$factory->make('view')->render();
854887
}

0 commit comments

Comments
 (0)