Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: isPersisted() must work when id is predictable #774

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"brianium/paratest": "^6|^7",
"dama/doctrine-test-bundle": "^7.0|^8.0",
"doctrine/collections": "^1.7|^2.0",
"doctrine/common": "^3.2",
"doctrine/common": "^2|^3",
"doctrine/doctrine-bundle": "^2.10",
"doctrine/doctrine-migrations-bundle": "^2.2|^3.0",
"doctrine/mongodb-odm-bundle": "^4.6|^5.0",
Expand All @@ -42,6 +42,7 @@
"symfony/phpunit-bridge": "^6.4|^7.0",
"symfony/runtime": "^6.4|^7.0",
"symfony/translation-contracts": "^3.4",
"symfony/uid": "^6.4|^7.0",
"symfony/var-dumper": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0"
},
Expand Down
7 changes: 7 additions & 0 deletions src/Mongo/MongoPersistenceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ public function isEmbeddable(object $object): bool
{
return $this->objectManagerFor($object::class)->getClassMetadata($object::class)->isEmbeddedDocument;
}

final function isScheduledForInsert(object $object): bool
{
$uow = $this->objectManagerFor($object::class)->getUnitOfWork();

return $uow->isScheduledForInsert($object) || $uow->isScheduledForUpsert($object);
}
}
5 changes: 5 additions & 0 deletions src/ORM/AbstractORMPersistenceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ final public function isEmbeddable(object $object): bool
return $this->objectManagerFor($object::class)->getClassMetadata($object::class)->isEmbeddedClass;
}

final function isScheduledForInsert(object $object): bool
{
return $this->objectManagerFor($object::class)->getUnitOfWork()->isScheduledForInsert($object);
}

final public function managedNamespaces(): array
{
$namespaces = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function findPersisted(object $object): ?object

public function isPersisted(object $object): bool
{
return (bool) $this->findPersisted($object);
return !$this->strategyFor($object::class)->isScheduledForInsert($object) && $this->findPersisted($object);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Persistence/PersistenceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,6 @@ abstract public function managedNamespaces(): array;
abstract public function embeddablePropertiesFor(object $object, string $owner): ?array;

abstract public function isEmbeddable(object $object): bool;

abstract public function isScheduledForInsert(object $object): bool;
}
30 changes: 30 additions & 0 deletions tests/Fixture/Document/DocumentWithUid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\Tests\Fixture\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Uid\Uuid;

/**
* @author Nicolas PHILIPPE <[email protected]>
*/
#[MongoDB\Document]
class DocumentWithUid
{
#[MongoDB\Id(type: 'bin_uuid', strategy: 'NONE')]
public string $id;

public function __construct()
{
$this->id = Uuid::v7()->toBinary();
}
}
25 changes: 25 additions & 0 deletions tests/Fixture/Entity/EntityWithUid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Tests\Fixture\Entity;

use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;

/**
* @author Nicolas PHILIPPE <[email protected]>
*/
#[ORM\Entity]
class EntityWithUid
{
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
public Uuid $id;

public function __construct()
{
$this->id = Uuid::v7();
}
}
26 changes: 26 additions & 0 deletions tests/Integration/Mongo/PersistenceManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Tests\Integration\Mongo;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\ObjectManager;
use Zenstruck\Foundry\Tests\Fixture\Document\DocumentWithUid;
use Zenstruck\Foundry\Tests\Integration\Persistence\PersistenceManagerTestCase;
use Zenstruck\Foundry\Tests\Integration\RequiresMongo;

final class PersistenceManagerTest extends PersistenceManagerTestCase
{
use RequiresMongo;

protected static function createObject(): object
{
return new DocumentWithUid();
}

protected static function objectManager(): ObjectManager
{
return self::getContainer()->get(DocumentManager::class); // @phpstan-ignore return.type
}
}
26 changes: 26 additions & 0 deletions tests/Integration/ORM/PersistenceManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Tests\Integration\ORM;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Zenstruck\Foundry\Tests\Integration\Persistence\PersistenceManagerTestCase;
use Zenstruck\Foundry\Tests\Fixture\Entity\EntityWithUid;
use Zenstruck\Foundry\Tests\Integration\RequiresORM;

final class PersistenceManagerTest extends PersistenceManagerTestCase
{
use RequiresORM;

protected static function createObject(): object
{
return new EntityWithUid();
}

protected static function objectManager(): ObjectManager
{
return self::getContainer()->get(EntityManagerInterface::class); // @phpstan-ignore return.type
}
}
33 changes: 33 additions & 0 deletions tests/Integration/Persistence/PersistenceManagerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Tests\Integration\Persistence;

use Doctrine\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Configuration;
use Zenstruck\Foundry\Test\Factories;

abstract class PersistenceManagerTestCase extends KernelTestCase
{
use Factories;

/**
* @test
*/
public function it_can_test_if_object_with_uuid_is_persisted(): void
{
$object = $this->createObject();

$this->objectManager()->persist($object);

self::assertFalse(
Configuration::instance()->persistence()->isPersisted($object)
);
}

abstract protected static function createObject(): object;

abstract protected static function objectManager(): ObjectManager;
}
Loading