Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Apr 22, 2024
1 parent 55ddee8 commit c0a4e22
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 122 deletions.
29 changes: 0 additions & 29 deletions src/Illuminate/View/AnonymousComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Illuminate\View;

use ReflectionClass;

class AnonymousComponent extends Component
{
/**
Expand All @@ -13,33 +11,6 @@ class AnonymousComponent extends Component
*/
protected $view;

/**
* The component data.
*
* @var array
*/
protected $data = [];

protected static array $ignoredParameterNames = [];

/**
* Fetch a cached set of anonymous component constructor parameter names to exclude.
*/
public static function ignoredParameterNames(): array
{
if (!isset(static::$ignoredParameterNames)) {
$constructor = (new ReflectionClass(
static::class
))->getConstructor();

static::$ignoredParameterNames = collect($constructor->getParameters())
->map->getName()
->all();
}

return static::$ignoredParameterNames;
}

/**
* Create a new anonymous component instance.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/View/Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected function componentString(string $component, array $attributes)

return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).'])
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php $attributes = $attributes->except(\\'.$class.'::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,9 @@ protected function compileEndComponentFirst()
*/
protected function compileProps($expression)
{
return "<?php
\$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag;
return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag;
\$__newAttributes = [];
\$__propNames = \Illuminate\Support\Arr::extractPropNames({$expression});
foreach (\$attributes->all() as \$__key => \$__value) {
Expand Down
31 changes: 31 additions & 0 deletions src/Illuminate/View/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,37 @@ abstract class Component
*/
protected static $constructorParametersCache = [];

/**
* The component data.
*
* @var array
*/
protected $data = [];

protected static array $ignoredParameterNames = [];

/**
* Fetch a cached set of anonymous component constructor parameter names to exclude.
*/
public static function ignoredParameterNames(): array
{
if (! isset(static::$ignoredParameterNames[static::class])) {
$constructor = (new ReflectionClass(
static::class
))->getConstructor();

if (! $constructor) {
return static::$ignoredParameterNames[static::class] = [];
}

static::$ignoredParameterNames[static::class] = collect($constructor->getParameters())
->map->getName()
->all();
}

return static::$ignoredParameterNames[static::class];
}

/**
* Get the view / view contents that represent the component.
*
Expand Down
140 changes: 70 additions & 70 deletions tests/View/Blade/BladeComponentTagCompilerTest.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/View/Blade/BladeComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testClassComponentsAreCompiled()
{
$this->assertSame('<?php if (isset($component)) { $__componentOriginal2dda3d2f2f9b76bd400bf03f0b84e87f = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal2dda3d2f2f9b76bd400bf03f0b84e87f = $attributes; } ?>
<?php $component = Illuminate\Tests\View\Blade\ComponentStub::class::resolve(["foo" => "bar"] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>
<?php $component = Illuminate\Tests\View\Blade\ComponentStub::class::resolve(["foo" => "bar"] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName(\'test\'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>', $this->compiler->compileString('@component(\'Illuminate\Tests\View\Blade\ComponentStub::class\', \'test\', ["foo" => "bar"])'));
Expand Down
38 changes: 27 additions & 11 deletions tests/View/Blade/BladePropsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@ class BladePropsTest extends AbstractBladeTestCase
{
public function testPropsAreCompiled()
{
$this->assertSame('<?php $attributes ??= new \Illuminate\View\ComponentAttributeBag; ?>
<?php foreach($attributes->onlyProps([\'one\' => true, \'two\' => \'string\']) as $__key => $__value) {
$$__key = $$__key ?? $__value;
} ?>
<?php $attributes = $attributes->exceptProps([\'one\' => true, \'two\' => \'string\']); ?>
<?php foreach (array_filter(([\'one\' => true, \'two\' => \'string\']), \'is_string\', ARRAY_FILTER_USE_KEY) as $__key => $__value) {
$this->assertSame('<?php $attributes ??= new \Illuminate\View\ComponentAttributeBag;
$__newAttributes = [];
$__propNames = \Illuminate\Support\Arr::extractPropNames(([\'one\' => true, \'two\' => \'string\']));
foreach ($attributes->all() as $__key => $__value) {
if (in_array($__key, $__propNames)) {
$$__key = $$__key ?? $__value;
} else {
$__newAttributes[$__key] = $__value;
}
}
$attributes = new \Illuminate\View\ComponentAttributeBag($__newAttributes);
unset($__propNames);
unset($__newAttributes);
foreach (array_filter(([\'one\' => true, \'two\' => \'string\']), \'is_string\', ARRAY_FILTER_USE_KEY) as $__key => $__value) {
$$__key = $$__key ?? $__value;
} ?>
<?php $__defined_vars = get_defined_vars(); ?>
<?php foreach ($attributes as $__key => $__value) {
}
$__defined_vars = get_defined_vars();
foreach ($attributes->all() as $__key => $__value) {
if (array_key_exists($__key, $__defined_vars)) unset($$__key);
} ?>
<?php unset($__defined_vars); ?>', $this->compiler->compileString('@props([\'one\' => true, \'two\' => \'string\'])'));
}
unset($__defined_vars); ?>', $this->compiler->compileString('@props([\'one\' => true, \'two\' => \'string\'])'));
}

public function testPropsAreExtractedFromParentAttributesCorrectly()
Expand Down
47 changes: 40 additions & 7 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function testEnvironmentAddsExtensionWithCustomResolver()
$factory->getEngineResolver()->shouldReceive('register')->once()->with('bar', $resolver);
$factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn('path.foo');
$factory->getEngineResolver()->shouldReceive('resolve')->once()->with('bar')->andReturn($engine = m::mock(Engine::class));
$factory->getDispatcher()->shouldReceive('dispatch');
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);

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

Expand Down Expand Up @@ -185,11 +185,14 @@ public function testPrependedExtensionOverridesExistingExtensions()
public function testCallCreatorsDoesDispatchEventsWhenIsNecessary()
{
$factory = $this->getFactory();

$factory->getDispatcher()
->shouldReceive('listen')
->with('creating: name', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('creating: name', m::type('array'))
Expand All @@ -206,11 +209,14 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessary()
public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespacedWildcards()
{
$factory = $this->getFactory();

$factory->getDispatcher()
->shouldReceive('listen')
->with('creating: namespaced::*', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('creating: namespaced::my-package-view', m::type('array'))
Expand All @@ -227,6 +233,7 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespaced
public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespacedNestedWildcards()
{
$factory = $this->getFactory();

$factory->getDispatcher()
->shouldReceive('listen')
->with('creating: namespaced::*', m::type(Closure::class))
Expand All @@ -237,6 +244,8 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespaced
->with('creating: welcome', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('creating: namespaced::my-package-view', m::type('array'))
Expand All @@ -253,11 +262,14 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespaced
public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingWildcards()
{
$factory = $this->getFactory();

$factory->getDispatcher()
->shouldReceive('listen')
->with('creating: *', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('creating: name', m::type('array'))
Expand All @@ -274,11 +286,14 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingWildcards(
public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNormalizedNames()
{
$factory = $this->getFactory();

$factory->getDispatcher()
->shouldReceive('listen')
->with('creating: components.button', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('creating: components/button', m::type('array'))
Expand All @@ -297,11 +312,14 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNormalized
public function testCallComposerDoesDispatchEventsWhenIsNecessary()
{
$factory = $this->getFactory();

$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: name', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('composing: name', m::type('array'))
Expand All @@ -318,11 +336,14 @@ public function testCallComposerDoesDispatchEventsWhenIsNecessary()
public function testCallComposerDoesDispatchEventsWhenIsNecessaryAndUsingTheArrayFormat()
{
$factory = $this->getFactory();

$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: name', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('composing: name', m::type('array'))
Expand All @@ -339,11 +360,14 @@ public function testCallComposerDoesDispatchEventsWhenIsNecessaryAndUsingTheArra
public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNamespacedWildcards()
{
$factory = $this->getFactory();

$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: namespaced::*', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('composing: namespaced::my-package-view', m::type('array'))
Expand All @@ -370,6 +394,8 @@ public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNamespace
->with('composing: welcome', m::type(Closure::class))
->once();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('composing: namespaced::my-package-view', m::type('array'))
Expand All @@ -386,6 +412,9 @@ public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNamespace
public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingWildcards()
{
$factory = $this->getFactory();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: *', m::type(Closure::class))
Expand All @@ -407,6 +436,9 @@ public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingWildcards
public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNormalizedNames()
{
$factory = $this->getFactory();

$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(true);

$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: components.button', m::type(Closure::class))
Expand Down Expand Up @@ -496,6 +528,7 @@ public function testCallComposerCallsProperEvent()

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

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

$factory->callComposer($view);
Expand Down Expand Up @@ -612,7 +645,7 @@ public function testComponentHandling()
$factory = $this->getFactory();
$factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
$factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
$factory->getDispatcher()->shouldReceive('dispatch');
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
$factory->startComponent('component', ['name' => 'Taylor']);
$factory->slot('title');
$factory->slot('website', 'laravel.com', []);
Expand All @@ -628,7 +661,7 @@ public function testComponentHandlingUsingViewObject()
$factory = $this->getFactory();
$factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
$factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
$factory->getDispatcher()->shouldReceive('dispatch');
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
$factory->startComponent($factory->make('component'), ['name' => 'Taylor']);
$factory->slot('title');
$factory->slot('website', 'laravel.com', []);
Expand All @@ -644,7 +677,7 @@ public function testComponentHandlingUsingClosure()
$factory = $this->getFactory();
$factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
$factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
$factory->getDispatcher()->shouldReceive('dispatch');
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
$factory->startComponent(function ($data) use ($factory) {
$this->assertArrayHasKey('name', $data);
$this->assertSame($data['name'], 'Taylor');
Expand Down Expand Up @@ -810,7 +843,7 @@ public function testMakeWithSlashAndDot()
$factory = $this->getFactory();
$factory->getFinder()->shouldReceive('find')->twice()->with('foo.bar')->andReturn('path.php');
$factory->getEngineResolver()->shouldReceive('resolve')->twice()->with('php')->andReturn(m::mock(Engine::class));
$factory->getDispatcher()->shouldReceive('dispatch');
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
$factory->make('foo/bar');
$factory->make('foo.bar');
}
Expand All @@ -820,7 +853,7 @@ public function testNamespacedViewNamesAreNormalizedProperly()
$factory = $this->getFactory();
$factory->getFinder()->shouldReceive('find')->twice()->with('vendor/package::foo.bar')->andReturn('path.php');
$factory->getEngineResolver()->shouldReceive('resolve')->twice()->with('php')->andReturn(m::mock(Engine::class));
$factory->getDispatcher()->shouldReceive('dispatch');
$factory->getDispatcher()->shouldReceive('hasListeners')->andReturn(false);
$factory->make('vendor/package::foo/bar');
$factory->make('vendor/package::foo.bar');
}
Expand Down Expand Up @@ -848,7 +881,7 @@ public function testExceptionsInSectionsAreThrown()
$factory->getEngineResolver()->shouldReceive('resolve')->twice()->andReturn($engine);
$factory->getFinder()->shouldReceive('find')->once()->with('layout')->andReturn(__DIR__.'/fixtures/section-exception-layout.php');
$factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn(__DIR__.'/fixtures/section-exception.php');
$factory->getDispatcher()->shouldReceive('dispatch')->times(4); // 2 "creating" + 2 "composing"...
$factory->getDispatcher()->shouldReceive('hasListeners')->times(4); // 2 "creating" + 2 "composing"...

$factory->make('view')->render();
}
Expand Down

0 comments on commit c0a4e22

Please sign in to comment.