-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #2201 [TwigComponent] Optimize ComponentFactory (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Optimize ComponentFactory | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Issues | Fix #... | License | MIT Some internal refactor focused on the Component Factory / Anonymous components usage. * Optimize the hot path * Store anonymous template resolution * Avoid anonymous checks for class-based components * Add ComponentFactory unit tests * Add ComponentMetadata::isAnonymous() method * Fix loop * Reuse metadata to instanciate and mount component Other PRs will follow :) Commits ------- da0537d [TwigComponent] Optimize ComponentFactory
- Loading branch information
Showing
3 changed files
with
131 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\Tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\UX\TwigComponent\ComponentFactory; | ||
use Symfony\UX\TwigComponent\ComponentTemplateFinderInterface; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
*/ | ||
class ComponentFactoryTest extends TestCase | ||
{ | ||
public function testMetadataForConfig(): void | ||
{ | ||
$factory = new ComponentFactory( | ||
$this->createMock(ComponentTemplateFinderInterface::class), | ||
$this->createMock(ServiceLocator::class), | ||
$this->createMock(PropertyAccessorInterface::class), | ||
$this->createMock(EventDispatcherInterface::class), | ||
['foo' => ['key' => 'foo', 'template' => 'bar.html.twig']], | ||
[] | ||
); | ||
|
||
$metadata = $factory->metadataFor('foo'); | ||
|
||
$this->assertSame('foo', $metadata->getName()); | ||
$this->assertSame('bar.html.twig', $metadata->getTemplate()); | ||
} | ||
|
||
public function testMetadataForResolveAlias(): void | ||
{ | ||
$factory = new ComponentFactory( | ||
$this->createMock(ComponentTemplateFinderInterface::class), | ||
$this->createMock(ServiceLocator::class), | ||
$this->createMock(PropertyAccessorInterface::class), | ||
$this->createMock(EventDispatcherInterface::class), | ||
[ | ||
'bar' => ['key' => 'bar', 'template' => 'bar.html.twig'], | ||
'foo' => ['key' => 'foo', 'template' => 'foo.html.twig'], | ||
], | ||
['Foo\\Bar' => 'bar'], | ||
); | ||
|
||
$metadata = $factory->metadataFor('Foo\\Bar'); | ||
|
||
$this->assertSame('bar', $metadata->getName()); | ||
$this->assertSame('bar.html.twig', $metadata->getTemplate()); | ||
} | ||
|
||
public function testMetadataForReuseAnonymousConfig(): void | ||
{ | ||
$templateFinder = $this->createMock(ComponentTemplateFinderInterface::class); | ||
$templateFinder->expects($this->atLeastOnce()) | ||
->method('findAnonymousComponentTemplate') | ||
->with('foo') | ||
->willReturnOnConsecutiveCalls('foo.html.twig', 'bar.html.twig', 'bar.html.twig'); | ||
|
||
$factory = new ComponentFactory( | ||
$templateFinder, | ||
$this->createMock(ServiceLocator::class), | ||
$this->createMock(PropertyAccessorInterface::class), | ||
$this->createMock(EventDispatcherInterface::class), | ||
[], | ||
[] | ||
); | ||
|
||
$metadata = $factory->metadataFor('foo'); | ||
$this->assertSame('foo', $metadata->getName()); | ||
$this->assertSame('foo.html.twig', $metadata->getTemplate()); | ||
|
||
$metadata = $factory->metadataFor('foo'); | ||
$this->assertSame('foo', $metadata->getName()); | ||
$this->assertSame('foo.html.twig', $metadata->getTemplate()); | ||
|
||
$metadata = $factory->metadataFor('foo'); | ||
$this->assertSame('foo', $metadata->getName()); | ||
$this->assertSame('foo.html.twig', $metadata->getTemplate()); | ||
} | ||
} |