Skip to content

Commit

Permalink
Updated mocking rules for PHPUnit 9.X
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone Pasquini committed Feb 3, 2022
1 parent 46e50a5 commit e93598b
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 51 deletions.
10 changes: 6 additions & 4 deletions spec/Recruiter/JobCallCustomMethodOnWorkableTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

namespace Recruiter;

use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Exception;

class JobCallCustomMethodOnWorkableTest extends TestCase
{
Expand All @@ -24,14 +27,13 @@ public function testConfigureMethodToCallOnWorkable()
{
$this->workable->expects($this->once())->method('send');
$this->job->methodToCallOnWorkable('send');
$this->job->execute($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
$this->job->execute($this->createMock(EventDispatcherInterface::class));
}

/**
* @expectedException Exception
*/
public function testRaiseExceptionWhenConfigureMethodToCallOnWorkableThatDoNotExists()
{
$this->expectException(Exception::class);

$this->job->methodToCallOnWorkable('methodThatDoNotExists');
}

Expand Down
9 changes: 6 additions & 3 deletions spec/Recruiter/JobSendEventsToWorkableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Recruiter\Job\Event;
use Recruiter\Job\EventListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use stdClass;

class JobSendEventsToWorkableTest extends TestCase
{
Expand All @@ -15,13 +17,14 @@ public function setUp(): void
->disableOriginalConstructor()
->getMock();

$this->dispatcher = $this->getMock(
'Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
}

public function testTakeRetryPolicyFromRetriableInstance()
{
$listener = $this->getMock('StdClass', ['onEvent']);
$listener = $this->getMockBuilder(stdClass::class)
->addMethods(['onEvent'])
->getMock();
$listener
->expects($this->exactly(3))
->method('onEvent')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Timeless as T;
use Recruiter\RetryPolicy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Recruiter\RetryPolicy\BaseRetryPolicy;

class JobTakeRetryPolicyFromRetriableWorkableTest extends TestCase
{
Expand All @@ -16,12 +18,12 @@ public function setUp(): void
->getMock();

$this->eventDispatcher = $this
->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
->createMock(EventDispatcherInterface::class);
}

public function testTakeRetryPolicyFromRetriableInstance()
{
$retryPolicy = $this->getMock('Recruiter\RetryPolicy\BaseRetryPolicy');
$retryPolicy = $this->createMock(BaseRetryPolicy::class);
$retryPolicy->expects($this->once())->method('schedule');

$workable = new WorkableThatIsAlsoRetriable($retryPolicy);
Expand Down
17 changes: 9 additions & 8 deletions spec/Recruiter/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use Recruiter\Workable\AlwaysFail;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use RuntimeException;

class JobTest extends TestCase
{
Expand All @@ -18,9 +20,9 @@ public function testRetryStatisticsOnFirstExecution()
{
$job = Job::around(new AlwaysFail, $this->repository);
$retryStatistics = $job->retryStatistics();
$this->assertInternalType('array', $retryStatistics);
$this->assertIsArray($retryStatistics);
$this->assertArrayHasKey('job_id', $retryStatistics);
$this->assertInternalType('string', $retryStatistics['job_id']);
$this->assertIsString($retryStatistics['job_id']);
$this->assertArrayHasKey('retry_number', $retryStatistics);
$this->assertEquals(0, $retryStatistics['retry_number']);
$this->assertArrayHasKey('last_execution', $retryStatistics);
Expand All @@ -34,27 +36,26 @@ public function testRetryStatisticsOnSubsequentExecutions()
{
$job = Job::around(new AlwaysFail, $this->repository);
// maybe make the argument optional
$job->execute($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
$job->execute($this->createMock(EventDispatcherInterface::class));
$job = Job::import($job->export(), $this->repository);
$retryStatistics = $job->retryStatistics();
$this->assertEquals(1, $retryStatistics['retry_number']);
$this->assertArrayHasKey('last_execution', $retryStatistics);
$lastExecution = $retryStatistics['last_execution'];
$this->assertInternalType('array', $lastExecution);
$this->assertIsArray($lastExecution);
$this->assertArrayHasKey('started_at', $lastExecution);
$this->assertArrayHasKey('ended_at', $lastExecution);
$this->assertArrayHasKey('class', $lastExecution);
$this->assertArrayHasKey('message', $lastExecution);
$this->assertArrayHasKey('trace', $lastExecution);
$this->assertEquals("Sorry, I'm good for nothing", $lastExecution['message']);
$this->assertRegexp("/.*AlwaysFail->execute.*/", $lastExecution['trace']);
$this->assertMatchesRegularExpression("/.*AlwaysFail->execute.*/", $lastExecution['trace']);
}

/**
* @expectedException \RuntimeException
*/
public function testArrayAsGroupIsNotAllowed()
{
$this->expectException(RuntimeException::class);

$job = Job::around(new AlwaysFail, $this->repository);
$job->inGroup(['test']);
}
Expand Down
3 changes: 2 additions & 1 deletion spec/Recruiter/JobToBePassedRetryStatisticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Timeless as T;
use Recruiter\RetryPolicy\DoNotDoItAgain;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class JobToBePassedRetryStatisticsTest extends TestCase
{
Expand All @@ -21,7 +22,7 @@ public function testTakeRetryPolicyFromRetriableInstance()
$workable = new WorkableThatUsesRetryStatistics();

$job = Job::around($workable, $this->repository);
$job->execute($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
$job->execute($this->createMock(EventDispatcherInterface::class));
$this->assertTrue($job->done(), "Job requiring retry statistics was not executed correctly: " . var_export($job->export(), true));
}
}
Expand Down
7 changes: 4 additions & 3 deletions spec/Recruiter/JobToScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Timeless as T;
use Recruiter\RetryPolicy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class JobToScheduleTest extends TestCase
{
Expand Down Expand Up @@ -98,15 +99,15 @@ public function testShouldExecuteJobWhenNotScheduled()
{
$this->job
->expects($this->never())
->method('schedule');
->method('scheduleAt');

$this->job
->expects($this->once())
->method('execute');

(new JobToSchedule($this->job))
->execute(
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
$this->createMock(EventDispatcherInterface::class)
);
}

Expand All @@ -132,7 +133,7 @@ public function testReturnsJobId()
'42',
(new JobToSchedule($this->job))
->execute(
$this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
$this->createMock(EventDispatcherInterface::class)
)
);
}
Expand Down
6 changes: 3 additions & 3 deletions spec/Recruiter/Option/MemoryLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
namespace Recruiter\Option;

use PHPUnit\Framework\TestCase;
use Recruiter\Option\MemoryLimitExceededException;

class MemoryLimitTest extends TestCase
{
/**
* @expectedException Recruiter\Option\MemoryLimitExceededException
*/
public function testThrowsAnExceptionWhenMemoryLimitIsExceeded()
{
$this->expectException(MemoryLimitExceededException::class);

$memoryLimit = new MemoryLimit('test', 1);
$memoryLimit->ensure(2);
}
Expand Down
28 changes: 20 additions & 8 deletions spec/Recruiter/RetryPolicy/RetriableExceptionFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace Recruiter\RetryPolicy;

use InvalidArgumentException;
use Exception;
use PHPUnit\Framework\TestCase;
use Recruiter\RetryPolicy;

class RetriableExceptionFilterTest extends TestCase
{
public function setUp(): void
{
$this->filteredRetryPolicy = $this->getMock('Recruiter\RetryPolicy');
$this->filteredRetryPolicy = $this->createMock(RetryPolicy::class);
}

public function testCallScheduleOnRetriableException()
{
$exception = $this->getMock('Exception');
$exception = $this->createMock(Exception::class);
$classOfException = get_class($exception);
$filter = new RetriableExceptionFilter($this->filteredRetryPolicy, [$classOfException]);

Expand All @@ -27,7 +29,7 @@ public function testCallScheduleOnRetriableException()

public function testDoNotCallScheduleOnNonRetriableException()
{
$exception = $this->getMock('Exception');
$exception = $this->createMock(Exception::class);
$classOfException = get_class($exception);
$filter = new RetriableExceptionFilter($this->filteredRetryPolicy, [$classOfException]);

Expand All @@ -40,7 +42,7 @@ public function testDoNotCallScheduleOnNonRetriableException()

public function testWhenExceptionIsNotRetriableThenArchiveTheJob()
{
$exception = $this->getMock('Exception');
$exception = $this->createMock(Exception::class);
$classOfException = get_class($exception);
$filter = new RetriableExceptionFilter($this->filteredRetryPolicy, [$classOfException]);

Expand Down Expand Up @@ -101,14 +103,24 @@ public function testImportRetryPolicy()

$filter = RetriableExceptionFilter::import($filter->export());
$filter->schedule($this->jobFailedWithException(new Exception('Test')));

$this->assertEquals(
[
'retriable_exceptions' => ['Exception'],
'filtered_retry_policy' => [
'class' => DoNotDoItAgain::class,
'parameters' => []
]
],
$filter->export()
);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Only subclasses of Exception can be retriable exceptions, 'StdClass' is not
*/
public function testRetriableExceptionsThatAreNotExceptions()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Only subclasses of Exception can be retriable exceptions, 'StdClass' is not");

$retryPolicy = new DoNotDoItAgain();
$notAnExceptionClass = 'StdClass';
new RetriableExceptionFilter($retryPolicy, [$notAnExceptionClass]);
Expand Down
17 changes: 7 additions & 10 deletions spec/Recruiter/RetryPolicy/TimeTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Recruiter\RetryPolicy;

use Exception;
use Timeless as T;
use PHPUnit\Framework\TestCase;

class TimeTableTest extends TestCase
{
public function setUp(): void
{
$this->clock = T\clock()->stop();
$this->scheduler = new TimeTable([
'5 minutes ago' => '1 minute',
'1 hour ago' => '5 minutes',
Expand Down Expand Up @@ -69,27 +69,24 @@ public function testCanCalculateTheMaximumNumberOfRetries()
$this->assertEquals(182, $tt->maximumNumberOfRetries());
}

/**
* @expectedException Exception
*/
public function testInvalidTimeTableBecauseTimeWindow()
{
$this->expectException(Exception::class);

$tt = new TimeTable(['1 minute' => '1 second']);
}

/**
* @expectedException Exception
*/
public function testInvalidTimeTableBecauseRescheduleTime()
{
$this->expectException(Exception::class);

$tt = new TimeTable(['1 minute ago' => '1 second ago']);
}

/**
* @expectedException Exception
*/
public function testInvalidTimeTableBecauseRescheduleTimeIsGreaterThanTimeWindow()
{
$this->expectException(Exception::class);

$tt = new TimeTable(['1 minute ago' => '2 minutes']);
}

Expand Down
4 changes: 3 additions & 1 deletion spec/Recruiter/Workable/FactoryMethodCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function testPassesRetryStatisticsAsAnAdditionalArgumentToTheLastMethodTo
$workable = FactoryMethodCommand::from('Recruiter\Workable\DummyFactory::create')
->myObject()
->myNeedyMethod();
$workable->execute(['retry_number' => 0]);

$expectedValue = $workable->execute(['retry_number' => 0]);
$this->assertEquals('42', $expectedValue);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/Sink/BlackHoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public function testIsAccessibleAsAnArrayAlwaysGetItself()
public function testIsAccessibleAsAnArrayExists()
{
$instance = new BlackHole();
$this->assertFalse(array_key_exists(42, $instance));
$this->assertFalse(array_key_exists(42, (array) $instance));
}
}
13 changes: 6 additions & 7 deletions spec/Timeless/IntervalParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Timeless;

use Timeless\InvalidIntervalFormat;
use DateInterval;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -93,20 +94,18 @@ public function testFromDateInterval()
$this->assertEquals(days(2)->add(minutes(10)), Interval::fromDateInterval(new DateInterval('P2DT10M')));
}

/**
* @expectedException Timeless\InvalidIntervalFormat
* @expectedExceptionMessage Maybe you mean '5 seconds' or something like that?
*/
public function testNumberAsIntervalFormat()
{
$this->expectException(InvalidIntervalFormat::class);
$this->expectExceptionMessage("Maybe you mean '5 seconds' or something like that?");

Interval::parse(5);
}

/**
* @expectedException Timeless\InvalidIntervalFormat
*/
public function testBadString()
{
$this->expectException(InvalidIntervalFormat::class);

Interval::parse('whatever');
}
}

0 comments on commit e93598b

Please sign in to comment.