-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #621 Add missing specs to cover PersistProcessor, RemoveProcess…
…or… (loic425) This PR was merged into the poc-new-resource-metadata branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | fixes #620 | License | MIT Commits ------- ca142ac Add missing specs to cover PersistProcessor, RemoveProcessor, ClassInfoTrait & DoctrineResourceMetadataCollectionFactory
- Loading branch information
Showing
8 changed files
with
353 additions
and
1 deletion.
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
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,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reflection; | ||
|
||
use App\Entity\Book; | ||
use PHPUnit\Framework\TestCase; | ||
use Sylius\Component\Resource\Reflection\ClassInfoTrait; | ||
|
||
final class ClassInfoTraitTest extends TestCase | ||
{ | ||
private function getClassInfoTraitImplementation(): object | ||
{ | ||
return new class() { | ||
use ClassInfoTrait { | ||
ClassInfoTrait::getRealClassName as public; | ||
} | ||
}; | ||
} | ||
|
||
public function testDoctrineRealClassName(): void | ||
{ | ||
$classInfo = $this->getClassInfoTraitImplementation(); | ||
|
||
$this->assertSame(Book::class, $classInfo->getRealClassName('Proxies\__CG__\App\Entity\Book')); | ||
} | ||
|
||
public function testProxyManagerRealClassName(): void | ||
{ | ||
$classInfo = $this->getClassInfoTraitImplementation(); | ||
|
||
$this->assertSame(Book::class, $classInfo->getRealClassName('MongoDBODMProxies\__PM__\App\Entity\Book\Generated')); | ||
} | ||
|
||
public function testUnmarkedRealClassName(): void | ||
{ | ||
$classInfo = $this->getClassInfoTraitImplementation(); | ||
|
||
$this->assertSame(Book::class, $classInfo->getRealClassName(Book::class)); | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
...ctrine/Common/Metadata/Resource/Factory/DoctrineResourceMetadataCollectionFactorySpec.php
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,117 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Component\Resource\Doctrine\Common\Metadata\Resource\Factory; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Resource\Doctrine\Common\Metadata\Resource\Factory\DoctrineResourceMetadataCollectionFactory; | ||
use Sylius\Component\Resource\Doctrine\Common\State\PersistProcessor; | ||
use Sylius\Component\Resource\Doctrine\Common\State\RemoveProcessor; | ||
use Sylius\Component\Resource\Metadata\Create; | ||
use Sylius\Component\Resource\Metadata\Delete; | ||
use Sylius\Component\Resource\Metadata\MetadataInterface; | ||
use Sylius\Component\Resource\Metadata\Operations; | ||
use Sylius\Component\Resource\Metadata\RegistryInterface; | ||
use Sylius\Component\Resource\Metadata\Resource; | ||
use Sylius\Component\Resource\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
use Sylius\Component\Resource\Metadata\Resource\ResourceMetadataCollection; | ||
|
||
final class DoctrineResourceMetadataCollectionFactorySpec extends ObjectBehavior | ||
{ | ||
function let( | ||
RegistryInterface $resourceRegistry, | ||
ResourceMetadataCollectionFactoryInterface $decorated, | ||
): void { | ||
$this->beConstructedWith($resourceRegistry, $decorated); | ||
} | ||
|
||
function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(DoctrineResourceMetadataCollectionFactory::class); | ||
} | ||
|
||
function it_adds_persist_processor_to_operations_for_resource_with_doctrine_orm_driver( | ||
ResourceMetadataCollectionFactoryInterface $decorated, | ||
RegistryInterface $resourceRegistry, | ||
MetadataInterface $metadata, | ||
): void { | ||
$resource = new Resource(alias: 'app.dummy'); | ||
$operation = new Create(name: 'app_dummy_create'); | ||
$resource = $resource->withOperations(new Operations([$operation])); | ||
|
||
$resourceMetadataCollection = new ResourceMetadataCollection([$resource]); | ||
|
||
$decorated->create('App\Resource')->willReturn($resourceMetadataCollection); | ||
|
||
$resourceRegistry->get('app.dummy')->willReturn($metadata); | ||
$metadata->getDriver()->willReturn('doctrine/orm'); | ||
|
||
$result = $this->create('App\Resource'); | ||
|
||
$result | ||
->getOperation('app.dummy', 'app_dummy_create') | ||
->getProcessor() | ||
->shouldReturn(PersistProcessor::class) | ||
; | ||
} | ||
|
||
function it_adds_persist_processor_to_operations_for_resource_with_doctrine_dbal_driver( | ||
ResourceMetadataCollectionFactoryInterface $decorated, | ||
RegistryInterface $resourceRegistry, | ||
MetadataInterface $metadata, | ||
): void { | ||
$resource = new Resource(alias: 'app.dummy'); | ||
$operation = new Create(name: 'app_dummy_create'); | ||
$resource = $resource->withOperations(new Operations([$operation])); | ||
|
||
$resourceMetadataCollection = new ResourceMetadataCollection([$resource]); | ||
|
||
$decorated->create('App\Resource')->willReturn($resourceMetadataCollection); | ||
|
||
$resourceRegistry->get('app.dummy')->willReturn($metadata); | ||
$metadata->getDriver()->willReturn('doctrine/dbal'); | ||
|
||
$result = $this->create('App\Resource'); | ||
|
||
$result | ||
->getOperation('app.dummy', 'app_dummy_create') | ||
->getProcessor() | ||
->shouldReturn(PersistProcessor::class) | ||
; | ||
} | ||
|
||
function it_adds_remove_processor_to_delete_operations_for_resource_with_doctrine_driver( | ||
ResourceMetadataCollectionFactoryInterface $decorated, | ||
RegistryInterface $resourceRegistry, | ||
MetadataInterface $metadata, | ||
): void { | ||
$resource = new Resource(alias: 'app.dummy'); | ||
$operation = new Delete(name: 'app_dummy_delete'); | ||
$resource = $resource->withOperations(new Operations([$operation])); | ||
|
||
$resourceMetadataCollection = new ResourceMetadataCollection([$resource]); | ||
|
||
$decorated->create('App\Resource')->willReturn($resourceMetadataCollection); | ||
|
||
$resourceRegistry->get('app.dummy')->willReturn($metadata); | ||
$metadata->getDriver()->willReturn('doctrine/orm'); | ||
|
||
$result = $this->create('App\Resource'); | ||
|
||
$result | ||
->getOperation('app.dummy', 'app_dummy_delete') | ||
->getProcessor() | ||
->shouldReturn(RemoveProcessor::class) | ||
; | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/Component/spec/Doctrine/Common/State/PersistProcessorSpec.php
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,113 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Component\Resource\Doctrine\Common\State; | ||
|
||
use Doctrine\ORM\Mapping\ClassMetadataInfo; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Doctrine\Persistence\ObjectManager; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Resource\Context\Context; | ||
use Sylius\Component\Resource\Doctrine\Common\State\PersistProcessor; | ||
use Sylius\Component\Resource\Metadata\Operation; | ||
|
||
final class PersistProcessorSpec extends ObjectBehavior | ||
{ | ||
function let(ManagerRegistry $managerRegistry): void | ||
{ | ||
$this->beConstructedWith($managerRegistry); | ||
} | ||
|
||
function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(PersistProcessor::class); | ||
} | ||
|
||
function it_persists_data_when_manager_does_not_contains_the_resource_yet( | ||
ManagerRegistry $managerRegistry, | ||
Operation $operation, | ||
ObjectManager $manager, | ||
): void { | ||
$data = new \stdClass(); | ||
|
||
$managerRegistry->getManagerForClass(\stdClass::class)->willReturn($manager); | ||
|
||
$manager->contains($data)->willReturn(false); | ||
|
||
$manager->persist($data)->shouldBeCalled(); | ||
$manager->flush()->shouldBeCalled(); | ||
$manager->refresh($data)->shouldBeCalled(); | ||
|
||
$this->process($data, $operation, new Context()); | ||
} | ||
|
||
function it_only_flush_when_manager_contains_the_resource( | ||
ManagerRegistry $managerRegistry, | ||
Operation $operation, | ||
ObjectManager $manager, | ||
): void { | ||
$data = new \stdClass(); | ||
|
||
$managerRegistry->getManagerForClass(\stdClass::class)->willReturn($manager); | ||
|
||
$manager->contains($data)->willReturn(true); | ||
$manager->getClassMetadata(\stdClass::class)->willReturn($data); | ||
|
||
$manager->persist($data)->shouldNotBeCalled(); | ||
|
||
$manager->flush()->shouldBeCalled(); | ||
$manager->refresh($data)->shouldBeCalled(); | ||
|
||
$this->process($data, $operation, new Context()); | ||
} | ||
|
||
function it_persists_when_it_is_deferred_explicitly( | ||
ManagerRegistry $managerRegistry, | ||
Operation $operation, | ||
ObjectManager $manager, | ||
ClassMetadataInfo $classMetadataInfo, | ||
): void { | ||
$data = new \stdClass(); | ||
|
||
$managerRegistry->getManagerForClass(\stdClass::class)->willReturn($manager); | ||
|
||
$manager->contains($data)->willReturn(true); | ||
|
||
$manager->getClassMetadata(\stdClass::class)->willReturn($classMetadataInfo); | ||
|
||
$classMetadataInfo->isChangeTrackingDeferredExplicit()->willReturn(true)->shouldBeCalled(); | ||
|
||
$manager->persist($data)->shouldBeCalled(); | ||
$manager->flush()->shouldBeCalled(); | ||
$manager->refresh($data)->shouldBeCalled(); | ||
|
||
$this->process($data, $operation, new Context()); | ||
} | ||
|
||
function it_does_nothing_when_data_is_not_managed_by_doctrine( | ||
ManagerRegistry $managerRegistry, | ||
Operation $operation, | ||
): void { | ||
$data = new \stdClass(); | ||
|
||
$managerRegistry->getManagerForClass(\stdClass::class)->willReturn(null); | ||
|
||
$this->process($data, $operation, new Context())->shouldReturn($data); | ||
} | ||
|
||
function it_does_nothing_when_data_is_not_an_object( | ||
Operation $operation, | ||
): void { | ||
$this->process(1, $operation, new Context())->shouldReturn(1); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Component/spec/Doctrine/Common/State/RemoveProcessorSpec.php
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,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Component\Resource\Doctrine\Common\State; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Doctrine\Persistence\ObjectManager; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Resource\Context\Context; | ||
use Sylius\Component\Resource\Doctrine\Common\State\RemoveProcessor; | ||
use Sylius\Component\Resource\Metadata\Operation; | ||
|
||
final class RemoveProcessorSpec extends ObjectBehavior | ||
{ | ||
function let(ManagerRegistry $managerRegistry): void | ||
{ | ||
$this->beConstructedWith($managerRegistry); | ||
} | ||
|
||
function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(RemoveProcessor::class); | ||
} | ||
|
||
function it_removes_data( | ||
ManagerRegistry $managerRegistry, | ||
Operation $operation, | ||
ObjectManager $manager, | ||
): void { | ||
$data = new \stdClass(); | ||
|
||
$managerRegistry->getManagerForClass(\stdClass::class)->willReturn($manager); | ||
|
||
$manager->contains($data)->willReturn(false); | ||
|
||
$manager->remove($data)->shouldBeCalled(); | ||
$manager->flush()->shouldBeCalled(); | ||
|
||
$this->process($data, $operation, new Context()); | ||
} | ||
|
||
function it_does_nothing_when_data_is_not_managed_by_doctrine( | ||
ManagerRegistry $managerRegistry, | ||
Operation $operation, | ||
): void { | ||
$data = new \stdClass(); | ||
|
||
$managerRegistry->getManagerForClass(\stdClass::class)->willReturn(null); | ||
|
||
$this->process($data, $operation, new Context())->shouldReturn(null); | ||
} | ||
|
||
function it_does_nothing_when_data_is_not_an_object( | ||
Operation $operation, | ||
): void { | ||
$this->process(1, $operation, new Context())->shouldReturn(null); | ||
} | ||
} |