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

Normalize assert calls in tests #2477

Merged
merged 1 commit into from
Oct 10, 2022
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
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