Skip to content

Commit 11b7547

Browse files
authored
Merge pull request #11 from jdreesen/php72-phpunit8
Upgrade to PHP ^7.2 and and PHPUnit ^8.5
2 parents cd71d4d + 58a361e commit 11b7547

File tree

4 files changed

+49
-81
lines changed

4 files changed

+49
-81
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ notifications:
77
on_success: never
88

99
php:
10-
- 7.0
11-
- 7.1
1210
- 7.2
1311
- 7.3
12+
- 7.4
1413
- nightly
1514

1615
env:
@@ -35,15 +34,14 @@ script: vendor/bin/phpunit --colors --columns 117 --no-coverage
3534

3635
jobs:
3736
allow_failures:
38-
- php: 7.3
3937
- php: nightly
4038
include:
4139
- php: nightly
4240
env: COMPOSER_FLAGS="--ignore-platform-reqs"
4341

4442
- stage: test with coverage
4543
os: linux
46-
php: 7.1
44+
php: 7.2
4745
env: COMPOSER_FLAGS=""
4846
before_install: composer self-update
4947
script: vendor/bin/phpunit --colors --coverage-text

composer.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
}
1717
},
1818
"require": {
19-
"php": "~7.0",
20-
"phpunit/phpunit-mock-objects": "~5.0|~6.0"
21-
},
22-
"require-dev": {
23-
"phpunit/phpunit": "~6.4|~7.0"
19+
"php": "^7.2",
20+
"phpunit/phpunit": "^8.5"
2421
}
2522
}

src/EasyMock.php

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace EasyMock;
44

5-
use PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount;
6-
use PHPUnit\Framework\MockObject\Matcher\Invocation as InvocationMatcher;
7-
use PHPUnit\Framework\MockObject\Matcher\InvokedAtLeastOnce;
85
use PHPUnit\Framework\MockObject\MockObject;
6+
use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount;
7+
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
8+
use PHPUnit\Framework\MockObject\Rule\InvokedAtLeastOnce;
99

1010
/**
1111
* Generates mock objects.
@@ -20,18 +20,14 @@ trait EasyMock
2020
* Methods not specified in $methods will be mocked to return null (default PHPUnit behavior).
2121
* The class constructor will *not* be called.
2222
*
23-
* @param string $classname The class to mock. Can also be an existing mock to mock new methods.
24-
* @param array $methods Array of values to return, indexed by the method name.
23+
* @param string|MockObject $classname The class to mock. Can also be an existing mock to mock new methods.
24+
* @param array $methods Array of values to return, indexed by the method name.
2525
*
26-
* @return \PHPUnit\Framework\MockObject\MockObject
26+
* @return MockObject
2727
*/
28-
protected function easyMock($classname, array $methods = array())
28+
protected function easyMock($classname, array $methods = array()): MockObject
2929
{
30-
if ($classname instanceof MockObject) {
31-
$mock = $classname;
32-
} else {
33-
$mock = $this->doCreateMock($classname);
34-
}
30+
$mock = $classname instanceof MockObject ? $classname : $this->createMock($classname);
3531

3632
foreach ($methods as $method => $return) {
3733
$this->mockMethod($mock, $method, new AnyInvokedCount(), $return);
@@ -43,35 +39,31 @@ protected function easyMock($classname, array $methods = array())
4339
/**
4440
* Mock the given class by spying on method calls.
4541
*
46-
* This is the same as EasyMock::mock() except this assert that methods are called at
47-
* least once.
42+
* This is the same as EasyMock::mock() except this assert that methods are called at least once.
4843
*
4944
* @see easyMock()
5045
*
51-
* @param string $classname The class to mock. Can also be an existing mock to mock new methods.
52-
* @param array $methods Array of values to return, indexed by the method name.
46+
* @param string|MockObject $classname The class to mock. Can also be an existing mock to mock new methods.
47+
* @param array $methods Array of values to return, indexed by the method name.
5348
*
54-
* @return \PHPUnit\Framework\MockObject\MockObject
49+
* @return MockObject
5550
*/
56-
protected function easySpy($classname, array $methods = array())
51+
protected function easySpy($classname, array $methods = array()): MockObject
5752
{
58-
if ($classname instanceof MockObject) {
59-
$mock = $classname;
60-
} else {
61-
$mock = $this->doCreateMock($classname);
62-
}
53+
$mock = $classname instanceof MockObject ? $classname : $this->createMock($classname);
6354

6455
foreach ($methods as $method => $return) {
65-
$this->mockMethod($mock, $method, new InvokedAtLeastOnce, $return);
56+
$this->mockMethod($mock, $method, new InvokedAtLeastOnce(), $return);
6657
}
6758

6859
return $mock;
6960
}
7061

71-
private function mockMethod(MockObject $mock, $method, InvocationMatcher $invocation, $return)
62+
abstract protected function createMock($originalClassName): MockObject;
63+
64+
private function mockMethod(MockObject $mock, $method, InvocationOrder $invocation, $return): void
7265
{
73-
$methodAssertion = $mock->expects($invocation)
74-
->method($method);
66+
$methodAssertion = $mock->expects($invocation)->method($method);
7567

7668
if (is_callable($return)) {
7769
$methodAssertion->willReturnCallback($return);
@@ -81,25 +73,4 @@ private function mockMethod(MockObject $mock, $method, InvocationMatcher $invoca
8173
$methodAssertion->willReturn($return);
8274
}
8375
}
84-
85-
/**
86-
* @param string $classname
87-
* @return MockObject
88-
*/
89-
private function doCreateMock($classname)
90-
{
91-
// PHPUnit 5.4 method
92-
if (method_exists($this, 'createMock')) {
93-
return $this->createMock($classname);
94-
}
95-
96-
// Fallback on the deprecated method
97-
return $this->getMock(
98-
$classname,
99-
array(),
100-
array(),
101-
'',
102-
false
103-
);
104-
}
10576
}

tests/EasyMockTest.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,54 @@ class EasyMockTest extends TestCase
2020
/**
2121
* @test
2222
*/
23-
public function should_mock_objects()
23+
public function should_mock_objects(): void
2424
{
2525
/** @var ClassFixture $mock */
26-
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');
26+
$mock = $this->easyMock(ClassFixture::class);
2727

28-
$this->assertInstanceOf('PHPUnit\Framework\MockObject\MockObject', $mock);
2928
$this->assertNull($mock->foo());
3029
}
3130

3231
/**
3332
* @test
3433
*/
35-
public function should_skip_the_constructor()
34+
public function should_skip_the_constructor(): void
3635
{
3736
/** @var ClassWithConstructor $mock */
38-
$mock = $this->easyMock('\EasyMock\Test\Fixture\ClassWithConstructor');
37+
$mock = $this->easyMock(ClassWithConstructor::class);
38+
3939
$this->assertFalse($mock->constructorCalled);
4040
}
4141

4242
/**
4343
* @test
4444
*/
45-
public function should_mock_interfaces()
45+
public function should_mock_interfaces(): void
4646
{
4747
/** @var InterfaceFixture $mock */
48-
$mock = $this->easyMock('EasyMock\Test\Fixture\InterfaceFixture');
48+
$mock = $this->easyMock(InterfaceFixture::class);
4949

50-
$this->assertInstanceOf('PHPUnit\Framework\MockObject\MockObject', $mock);
5150
$this->assertNull($mock->foo());
5251
}
5352

5453
/**
5554
* @test
5655
*/
57-
public function not_mocked_methods_should_return_null()
56+
public function not_mocked_methods_should_return_null(): void
5857
{
5958
/** @var ClassFixture $mock */
60-
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');
59+
$mock = $this->easyMock(ClassFixture::class);
6160

6261
$this->assertNull($mock->foo());
6362
}
6463

6564
/**
6665
* @test
6766
*/
68-
public function should_mock_existing_method_with_a_value()
67+
public function should_mock_existing_method_with_a_value(): void
6968
{
7069
/** @var ClassFixture $mock */
71-
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
70+
$mock = $this->easyMock(ClassFixture::class, array(
7271
'foo' => 'bar',
7372
));
7473

@@ -78,10 +77,10 @@ public function should_mock_existing_method_with_a_value()
7877
/**
7978
* @test
8079
*/
81-
public function should_mock_existing_method_with_a_callback()
80+
public function should_mock_existing_method_with_a_callback(): void
8281
{
8382
/** @var ClassFixture $mock */
84-
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
83+
$mock = $this->easyMock(ClassFixture::class, array(
8584
'foo' => function () {
8685
return 'bar';
8786
},
@@ -92,25 +91,27 @@ public function should_mock_existing_method_with_a_callback()
9291

9392
/**
9493
* @test
95-
* @expectedException \EasyMock\Test\Fixture\CustomException
96-
* @expectedExceptionMessage My message
9794
*/
98-
public function should_mock_existing_method_to_throw_exception()
95+
public function should_mock_existing_method_to_throw_exception(): void
9996
{
10097
/** @var ClassFixture $mock */
101-
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture', array(
98+
$mock = $this->easyMock(ClassFixture::class, array(
10299
'foo' => new CustomException('My message'),
103100
));
101+
102+
$this->expectException(CustomException::class);
103+
$this->expectExceptionMessage('My message');
104+
104105
$mock->foo();
105106
}
106107

107108
/**
108109
* @test
109110
*/
110-
public function should_mock_new_methods_on_existing_mock()
111+
public function should_mock_new_methods_on_existing_mock(): void
111112
{
112113
/** @var ClassFixture $mock */
113-
$mock = $this->easyMock('EasyMock\Test\Fixture\ClassFixture');
114+
$mock = $this->easyMock(ClassFixture::class);
114115
$mock = $this->easyMock($mock, array(
115116
'foo' => 'bar',
116117
));
@@ -121,14 +122,15 @@ public function should_mock_new_methods_on_existing_mock()
121122
/**
122123
* @test
123124
*/
124-
public function should_allow_to_spy_method_calls()
125+
public function should_allow_to_spy_method_calls(): void
125126
{
126-
$mock = $this->easySpy('EasyMock\Test\Fixture\ClassFixture', array(
127+
/** @var ClassFixture $mock */
128+
$mock = $this->easySpy(ClassFixture::class, array(
127129
'foo' => 'bar',
128130
));
129131

130132
// Test PHPUnit's internals to check that the spy was registered
131-
$property = new \ReflectionProperty('\PHPUnit\Framework\TestCase', 'mockObjects');
133+
$property = new \ReflectionProperty(TestCase::class, 'mockObjects');
132134
$property->setAccessible(true);
133135
$mockObjects = $property->getValue($this);
134136

@@ -140,7 +142,7 @@ public function should_allow_to_spy_method_calls()
140142
$mock->__phpunit_verify();
141143
$this->fail('Exception not thrown');
142144
} catch (ExpectationFailedException $e) {
143-
$this->assertContains('Expected invocation at least once but it never occur', $e->getMessage());
145+
$this->assertStringContainsString('Expected invocation at least once but it never occur', $e->getMessage());
144146
}
145147

146148
// Invoke the mock: the test should now pass

0 commit comments

Comments
 (0)