Skip to content

Commit

Permalink
minor #621 Add missing specs to cover PersistProcessor, RemoveProcess…
Browse files Browse the repository at this point in the history
…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
lchrusciel authored Mar 17, 2023
2 parents 23e3d4f + ca142ac commit 1baf88c
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 1 deletion.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ parameters:
- '/Call to method getArguments\(\) on an unknown class ReflectionAttribute./'
- '/Call to method isChangeTrackingDeferredExplicit\(\) on an unknown class Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata./'
- '/Call to an undefined method ReflectionClass::getAttributes\(\)./'
- '/Call to an undefined method object::getRealClassName\(\)./'
- '/Class Doctrine\\Bundle\\MongoDBBundle/'
- '/Class Doctrine\\Bundle\\PHPCRBundle/'
- '/Class Doctrine\\Common\\Persistence\\ObjectManager not found\./'
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@

<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<file name="src/Component/Tests/Reflection/ClassInfoTraitTest.php" />
<file name="src/Component/Tests/Reflection/ClassReflectionTest.php" />
<file name="src/Bundle/Validator/Constraints/UniqueWithinCollectionConstraint.php" />
<file name="src/Bundle/Validator/Constraints/Enabled.php" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Sylius\Component\Resource\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Sylius\Component\Resource\Metadata\Resource\ResourceMetadataCollection;

class DoctrineResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
final class DoctrineResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
public function __construct(
private RegistryInterface $resourceRegistry,
Expand Down
51 changes: 51 additions & 0 deletions src/Component/Tests/Reflection/ClassInfoTraitTest.php
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));
}
}
1 change: 1 addition & 0 deletions src/Component/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"require-dev": {
"behat/transliterator": "^1.3",
"doctrine/orm": "^2.5",
"phpspec/phpspec": "^7.2",
"phpunit/phpunit": "^9.5",
"sylius/grid": "^1.7 || ^1.12",
Expand Down
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 src/Component/spec/Doctrine/Common/State/PersistProcessorSpec.php
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 src/Component/spec/Doctrine/Common/State/RemoveProcessorSpec.php
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);
}
}

0 comments on commit 1baf88c

Please sign in to comment.