-
-
Notifications
You must be signed in to change notification settings - Fork 507
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
Restore document proxy state to uninitialized on load exception #2521
Merged
malarzm
merged 1 commit into
doctrine:2.5.x
from
notrix:fix-proxy-state-on-init-exception
Apr 13, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
100 changes: 100 additions & 0 deletions
100
tests/Doctrine/ODM/MongoDB/Tests/Proxy/Factory/StaticProxyFactoryTest.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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ODM\MongoDB\Tests\Proxy\Factory; | ||
|
||
use Closure; | ||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use Doctrine\ODM\MongoDB\Event\DocumentNotFoundEventArgs; | ||
use Doctrine\ODM\MongoDB\Events; | ||
use Doctrine\ODM\MongoDB\LockException; | ||
use Doctrine\ODM\MongoDB\Tests\BaseTestCase; | ||
use Documents\Cart; | ||
use MongoDB\Client; | ||
use MongoDB\Collection; | ||
use MongoDB\Database; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use ProxyManager\Proxy\GhostObjectInterface; | ||
|
||
class StaticProxyFactoryTest extends BaseTestCase | ||
{ | ||
/** @var Client|MockObject */ | ||
private Client $client; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->dm = $this->createMockedDocumentManager(); | ||
} | ||
|
||
public function testProxyInitializeWithException(): void | ||
{ | ||
$collection = $this->createMock(Collection::class); | ||
$database = $this->createMock(Database::class); | ||
|
||
$this->client->expects($this->once()) | ||
->method('selectDatabase') | ||
->willReturn($database); | ||
|
||
$database->expects($this->once()) | ||
->method('selectCollection') | ||
->willReturn($collection); | ||
|
||
$collection->expects($this->once()) | ||
->method('findOne') | ||
->willThrowException(LockException::lockFailed(null)); | ||
|
||
$uow = $this->dm->getUnitOfWork(); | ||
|
||
$proxy = $this->dm->getReference(Cart::class, '123'); | ||
self::assertInstanceOf(GhostObjectInterface::class, $proxy); | ||
|
||
$closure = static function (DocumentNotFoundEventArgs $eventArgs) { | ||
self::fail('DocumentNotFoundListener should not be called'); | ||
}; | ||
$this->dm->getEventManager()->addEventListener(Events::documentNotFound, new DocumentNotFoundListener($closure)); | ||
|
||
try { | ||
$proxy->initializeProxy(); | ||
malarzm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self::fail('An exception should have been thrown'); | ||
} catch (LockException $exception) { | ||
self::assertInstanceOf(LockException::class, $exception); | ||
} | ||
|
||
$uow->computeChangeSets(); | ||
|
||
self::assertFalse($proxy->isProxyInitialized(), 'Proxy should not be initialized'); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
// db connection is mocked, nothing to clean up | ||
} | ||
malarzm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private function createMockedDocumentManager(): DocumentManager | ||
{ | ||
$config = $this->getConfiguration(); | ||
|
||
$this->client = $this->createMock(Client::class); | ||
|
||
return DocumentManager::create($this->client, $config); | ||
} | ||
} | ||
|
||
class DocumentNotFoundListener | ||
{ | ||
private Closure $closure; | ||
|
||
public function __construct(Closure $closure) | ||
{ | ||
$this->closure = $closure; | ||
} | ||
|
||
public function documentNotFound(DocumentNotFoundEventArgs $eventArgs): void | ||
{ | ||
$closure = $this->closure; | ||
$closure($eventArgs); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit torn so here goes nothing: how about instead of mocking 3 levels deep we mock
DocumentPersister
, have it thrown an exception onload
? This would require manipulatingUnitOfWork::$documentPersisters
with reflection though, but the test should be easier to maintain with real database connection. For your consideration :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first I also wanted to mock DocumentPersister::load, but DocumentPersister is a final class. So my best shot was to mock Collection::findOne
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed then!