Skip to content

Commit

Permalink
Replacing deprecated at() calls in unit tests (#9835)
Browse files Browse the repository at this point in the history
* Replacing deprecated at() calls part 1

* Replacing deprecated at()

* Refactoring out of deprecated at()

* refactoring out of at()

* Refactoring out of at()

* Refactoring out of at()

* Refactoring out of at()

* Refactoring out of at()

* Another batch of at() refactoring

* Another batch of at() refactoring

* at() refactoring

* Removing test that actually did not test anything

* Another batch of at() refactoring

* CI fixes

* Another batch of at() refactoring

* Last batch of at() refactoring

* Test fix

* One more at()

* Refactoring deprecated assertRegExp()

* Refactoring deprecated assertFileNotExists()
  • Loading branch information
escopecz committed Mar 27, 2021
1 parent c3e2676 commit 21cc28b
Show file tree
Hide file tree
Showing 66 changed files with 2,206 additions and 2,466 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Mautic\AssetBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Mautic\AssetBundle\AssetEvents;
use Mautic\AssetBundle\Entity\Download;
use Mautic\CoreBundle\Event\DetermineWinnerEvent;
Expand All @@ -22,7 +22,7 @@
class DetermineWinnerSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManager
* @var EntityManagerInterface
*/
private $em;

Expand All @@ -31,7 +31,7 @@ class DetermineWinnerSubscriber implements EventSubscriberInterface
*/
private $translator;

public function __construct(EntityManager $em, TranslatorInterface $translator)
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->em = $em;
$this->translator = $translator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic, Inc.
Expand All @@ -11,16 +13,24 @@

namespace Mautic\AssetBundle\Tests\EventListener;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Mautic\AssetBundle\Entity\DownloadRepository;
use Mautic\AssetBundle\EventListener\DetermineWinnerSubscriber;
use Mautic\CoreBundle\Event\DetermineWinnerEvent;
use Mautic\PageBundle\Entity\Page;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Translation\TranslatorInterface;

class DetermineWinnerSubscriberTest extends \PHPUnit\Framework\TestCase
{
/**
* @var MockObject|EntityManagerInterface
*/
private $em;

/**
* @var MockObject|TranslatorInterface
*/
private $translator;

/**
Expand All @@ -32,7 +42,7 @@ protected function setUp(): void
{
parent::setUp();

$this->em = $this->createMock(EntityManager::class);
$this->em = $this->createMock(EntityManagerInterface::class);
$this->translator = $this->createMock(TranslatorInterface::class);
$this->subscriber = new DetermineWinnerSubscriber($this->em, $this->translator);
}
Expand Down Expand Up @@ -65,13 +75,8 @@ public function testOnDetermineDownloadRateWinner()
],
];

$this->translator->expects($this->at(0))
->method('trans')
->willReturn($transDownloads);

$this->translator->expects($this->at(1))
->method('trans')
->willReturn($transHits);
$this->translator->method('trans')
->willReturnOnConsecutiveCalls($transDownloads, $transHits);

$this->em->expects($this->once())
->method('getRepository')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* @copyright 2018 Mautic Contributors. All rights reserved
* @author Mautic, Inc.
Expand All @@ -26,69 +28,57 @@
use Mautic\CampaignBundle\Executioner\Helper\NotificationHelper;
use Mautic\CampaignBundle\Executioner\Scheduler\EventScheduler;
use Mautic\LeadBundle\Entity\Lead;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\NullLogger;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class ActionDispatcherTest extends \PHPUnit\Framework\TestCase
{
/**
* @var MockBuilder|EventDispatcherInterface
* @var MockObject|EventDispatcherInterface
*/
private $dispatcher;

/**
* @var MockBuilder|EventScheduler
* @var MockObject|EventScheduler
*/
private $scheduler;

/**
* @var MockBuilder|LegacyEventDispatcher
* @var MockObject|LegacyEventDispatcher
*/
private $legacyDispatcher;

/**
* @var MockBuilder|NotificationHelper
* @var MockObject|NotificationHelper
*/
private $notificationHelper;

protected function setUp(): void
{
$this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
->disableOriginalConstructor()
->getMock();

$this->scheduler = $this->getMockBuilder(EventScheduler::class)
->disableOriginalConstructor()
->getMock();

$this->notificationHelper = $this->getMockBuilder(NotificationHelper::class)
->disableOriginalConstructor()
->getMock();
parent::setUp();

$this->legacyDispatcher = $this->getMockBuilder(LegacyEventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->scheduler = $this->createMock(EventScheduler::class);
$this->notificationHelper = $this->createMock(NotificationHelper::class);
$this->legacyDispatcher = $this->createMock(LegacyEventDispatcher::class);
}

public function testActionBatchEventIsDispatchedWithSuccessAndFailedLogs()
{
$event = new Event();

$lead1 = $this->getMockBuilder(Lead::class)
->getMock();
$lead1 = $this->createMock(Lead::class);
$lead1->expects($this->exactly(2))
->method('getId')
->willReturn(1);

$lead2 = $this->getMockBuilder(Lead::class)
->getMock();
$lead2 = $this->createMock(Lead::class);
$lead2->expects($this->exactly(2))
->method('getId')
->willReturn(2);

$log1 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log1 = $this->createMock(LeadEventLog::class);
$log1->expects($this->exactly(2))
->method('getLead')
->willReturn($lead1);
Expand All @@ -97,8 +87,7 @@ public function testActionBatchEventIsDispatchedWithSuccessAndFailedLogs()
$log1->method('getEvent')
->willReturn($event);

$log2 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log2 = $this->createMock(LeadEventLog::class);
$log2->expects($this->exactly(3))
->method('getLead')
->willReturn($lead2);
Expand All @@ -114,35 +103,32 @@ public function testActionBatchEventIsDispatchedWithSuccessAndFailedLogs()
]
);

$config = $this->getMockBuilder(ActionAccessor::class)
->disableOriginalConstructor()
->getMock();

$config = $this->createMock(ActionAccessor::class);
$config->expects($this->once())
->method('getBatchEventName')
->willReturn('something');

$this->dispatcher->expects($this->at(0))
$dispatcCounter = 0;

$this->dispatcher->expects($this->exactly(4))
->method('dispatch')
->withConsecutive(
[],
[CampaignEvents::ON_EVENT_EXECUTED, $this->isInstanceOf(ExecutedEvent::class)],
[CampaignEvents::ON_EVENT_EXECUTED_BATCH, $this->isInstanceOf(ExecutedBatchEvent::class)],
[CampaignEvents::ON_EVENT_FAILED, $this->isInstanceOf(FailedEvent::class)]
)
->willReturnCallback(
function ($eventName, PendingEvent $pendingEvent) use ($logs) {
$pendingEvent->pass($logs->get(1));
$pendingEvent->fail($logs->get(2), 'just because');
function (string $eventName, $event) use ($logs, &$dispatcCounter) {
++$dispatcCounter;
if (1 === $dispatcCounter) {
Assert::assertInstanceOf(PendingEvent::class, $event);
$event->pass($logs->get(1));
$event->fail($logs->get(2), 'just because');
}
}
);

$this->dispatcher->expects($this->at(1))
->method('dispatch')
->with(CampaignEvents::ON_EVENT_EXECUTED, $this->isInstanceOf(ExecutedEvent::class));

$this->dispatcher->expects($this->at(2))
->method('dispatch')
->with(CampaignEvents::ON_EVENT_EXECUTED_BATCH, $this->isInstanceOf(ExecutedBatchEvent::class));

$this->dispatcher->expects($this->at(3))
->method('dispatch')
->with(CampaignEvents::ON_EVENT_FAILED, $this->isInstanceOf(FailedEvent::class));

$this->scheduler->expects($this->once())
->method('rescheduleFailures')
->willReturnCallback(
Expand Down Expand Up @@ -170,21 +156,17 @@ public function testActionLogNotProcessedExceptionIsThrownIfLogNotProcessedWithS
$this->expectException(LogNotProcessedException::class);

$event = new Event();

$lead1 = $this->getMockBuilder(Lead::class)
->getMock();
$lead1 = $this->createMock(Lead::class);
$lead1->expects($this->once())
->method('getId')
->willReturn(1);

$lead2 = $this->getMockBuilder(Lead::class)
->getMock();
$lead2 = $this->createMock(Lead::class);
$lead2->expects($this->once())
->method('getId')
->willReturn(2);

$log1 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log1 = $this->createMock(LeadEventLog::class);
$log1->expects($this->once())
->method('getLead')
->willReturn($lead1);
Expand All @@ -193,8 +175,7 @@ public function testActionLogNotProcessedExceptionIsThrownIfLogNotProcessedWithS
$log1->method('getEvent')
->willReturn($event);

$log2 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log2 = $this->createMock(LeadEventLog::class);
$log2->expects($this->once())
->method('getLead')
->willReturn($lead2);
Expand All @@ -210,15 +191,13 @@ public function testActionLogNotProcessedExceptionIsThrownIfLogNotProcessedWithS
]
);

$config = $this->getMockBuilder(ActionAccessor::class)
->disableOriginalConstructor()
->getMock();
$config = $this->createMock(ActionAccessor::class);

$config->expects($this->once())
->method('getBatchEventName')
->willReturn('something');

$this->dispatcher->expects($this->at(0))
$this->dispatcher->expects($this->once())
->method('dispatch')
->willReturnCallback(
function ($eventName, PendingEvent $pendingEvent) use ($logs) {
Expand All @@ -237,20 +216,17 @@ public function testActionLogNotProcessedExceptionIsThrownIfLogNotProcessedWithF

$event = new Event();

$lead1 = $this->getMockBuilder(Lead::class)
->getMock();
$lead1 = $this->createMock(Lead::class);
$lead1->expects($this->once())
->method('getId')
->willReturn(1);

$lead2 = $this->getMockBuilder(Lead::class)
->getMock();
$lead2 = $this->createMock(Lead::class);
$lead2->expects($this->once())
->method('getId')
->willReturn(2);

$log1 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log1 = $this->createMock(LeadEventLog::class);
$log1->expects($this->once())
->method('getLead')
->willReturn($lead1);
Expand All @@ -259,8 +235,7 @@ public function testActionLogNotProcessedExceptionIsThrownIfLogNotProcessedWithF
$log1->method('getEvent')
->willReturn($event);

$log2 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log2 = $this->createMock(LeadEventLog::class);
$log2->expects($this->once())
->method('getLead')
->willReturn($lead2);
Expand All @@ -276,15 +251,13 @@ public function testActionLogNotProcessedExceptionIsThrownIfLogNotProcessedWithF
]
);

$config = $this->getMockBuilder(ActionAccessor::class)
->disableOriginalConstructor()
->getMock();
$config = $this->createMock(ActionAccessor::class);

$config->expects($this->once())
->method('getBatchEventName')
->willReturn('something');

$this->dispatcher->expects($this->at(0))
$this->dispatcher->expects($this->once())
->method('dispatch')
->willReturnCallback(
function ($eventName, PendingEvent $pendingEvent) use ($logs) {
Expand All @@ -299,11 +272,8 @@ function ($eventName, PendingEvent $pendingEvent) use ($logs) {

public function testActionBatchEventIsIgnoredWithLegacy()
{
$event = new Event();

$config = $this->getMockBuilder(ActionAccessor::class)
->disableOriginalConstructor()
->getMock();
$event = new Event();
$config = $this->createMock(ActionAccessor::class);

$config->expects($this->once())
->method('getBatchEventName')
Expand Down

0 comments on commit 21cc28b

Please sign in to comment.