Skip to content

Commit

Permalink
Merge pull request #2477 from hunomina/normalize-assert-calls-in-tests-2
Browse files Browse the repository at this point in the history
Normalize assert calls in tests
  • Loading branch information
malarzm authored Oct 10, 2022
2 parents 5fcca6c + 7a821c2 commit f5c1679
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
18 changes: 5 additions & 13 deletions tests/Doctrine/ODM/MongoDB/Tests/Events/LifecycleListenersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Assert;

use function get_class;

Expand Down Expand Up @@ -196,7 +196,7 @@ public function testChangeToReferenceFieldTriggersEvents(): void
public function testPostCollectionLoad(): void
{
$evm = $this->dm->getEventManager();
$evm->addEventListener([Events::postCollectionLoad], new PostCollectionLoadEventListener($this));
$evm->addEventListener([Events::postCollectionLoad], new PostCollectionLoadEventListener());

$document = new TestDocument();
$document->name = 'Maciej';
Expand Down Expand Up @@ -242,26 +242,18 @@ class PostCollectionLoadEventListener
/** @var int */
private $at = 0;

/** @var TestCase */
private $phpunit;

public function __construct(TestCase $phpunit)
{
$this->phpunit = $phpunit;
}

/**
* @param PostCollectionLoadEventArgs<int, TestEmbeddedDocument> $e
*/
public function postCollectionLoad(PostCollectionLoadEventArgs $e): void
{
switch ($this->at++) {
case 0:
$this->phpunit->assertCount(0, $e->getCollection());
Assert::assertCount(0, $e->getCollection());
break;
case 1:
$this->phpunit->assertCount(1, $e->getCollection());
$this->phpunit->assertEquals(new TestEmbeddedDocument('For mock at 1'), $e->getCollection()[0]);
Assert::assertCount(1, $e->getCollection());
Assert::assertEquals(new TestEmbeddedDocument('For mock at 1'), $e->getCollection()[0]);
break;
default:
throw new BadMethodCallException('This was not expected');
Expand Down
18 changes: 5 additions & 13 deletions tests/Doctrine/ODM/MongoDB/Tests/Events/PreUpdateEventArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Documents\Book;
use Documents\Chapter;
use Documents\Page;
use PHPUnit\Framework\Assert;

use function get_class;
use function in_array;
Expand All @@ -34,7 +35,7 @@ public function testChangeSetIsUpdated(): void

public function testCollectionsAreInChangeSet(): void
{
$listener = new CollectionsAreInChangeSetListener($this);
$listener = new CollectionsAreInChangeSetListener();
$this->dm->getEventManager()->addEventListener(Events::preUpdate, $listener);

$book = new Book();
Expand Down Expand Up @@ -74,16 +75,7 @@ public function preUpdate(PreUpdateEventArgs $e): void
class CollectionsAreInChangeSetListener
{
/** @var list<class-string> */
private $allowed;

/** @var PreUpdateEventArgsTest */
private $phpunit;

public function __construct(PreUpdateEventArgsTest $phpunit)
{
$this->allowed = [Book::class, Chapter::class];
$this->phpunit = $phpunit;
}
private $allowed = [Book::class, Chapter::class];

public function checkOnly(array $allowed): void
{
Expand All @@ -95,13 +87,13 @@ public function preUpdate(PreUpdateEventArgs $e): void
switch (get_class($e->getDocument())) {
case Book::class:
if (in_array(Book::class, $this->allowed)) {
$this->phpunit->assertTrue($e->hasChangedField('chapters'));
Assert::assertTrue($e->hasChangedField('chapters'));
}

break;
case Chapter::class:
if (in_array(Chapter::class, $this->allowed)) {
$this->phpunit->assertTrue($e->hasChangedField('pages'));
Assert::assertTrue($e->hasChangedField('pages'));
}

break;
Expand Down

0 comments on commit f5c1679

Please sign in to comment.