Skip to content

Commit 383f793

Browse files
authored
Merge pull request #5 from kenjis/fix-setMethods
Use onlyMethods() instead of setMethods()
2 parents b675ab2 + 73dfaf9 commit 383f793

File tree

10 files changed

+548
-432
lines changed

10 files changed

+548
-432
lines changed

composer.lock

Lines changed: 91 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpcs.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<file>src</file>
2020
<file>tests</file>
2121
<exclude-pattern>*/tmp/*</exclude-pattern>
22-
<exclude-pattern>*/Fake/*</exclude-pattern>
2322

2423
<!-- PSR12 Coding Standard -->
2524
<rule ref="PSR12"/>

src/TestDouble.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait TestDouble
2121
*
2222
* $email = $this->getMockBuilder('CI_Email')
2323
* ->disableOriginalConstructor()
24-
* ->setMethods(['send'])
24+
* ->onlyMethods(['send'])
2525
* ->getMock();
2626
* $email->method('send')->willReturn(true);
2727
*
@@ -67,7 +67,7 @@ public function getDouble(
6767
}
6868
}
6969

70-
$mock = $mockBuilder->setMethods($methods)->getMock();
70+
$mock = $mockBuilder->onlyMethods($methods)->getMock();
7171

7272
foreach ($onConsecutiveCalls as $method => $returns) {
7373
$mock->expects($this->any())->method($method)

tests/Fake/CI_Email.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/Fake/CI_Input.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/Fake/Email.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kenjis\PhpUnitHelper;
6+
7+
class Email
8+
{
9+
public function to(string $to): Email
10+
{
11+
return $this;
12+
}
13+
}

tests/Fake/ForReflectionHelper.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66

77
class ForReflectionHelper
88
{
9-
/**
10-
* @var string
11-
*/
9+
/** @var string */
1210
private $private = 'secret';
1311

14-
/**
15-
* @var string
16-
*/
12+
/** @var string */
1713
private static $staticPrivate = 'xyz';
1814

1915
public function getPrivate(): string
@@ -31,7 +27,7 @@ private function privateMethod(string $param1, string $param2): string
3127
return 'private ' . $param1 . $param2;
3228
}
3329

34-
private static function privateStaticMethod(string $param1, string$param2): string
30+
private static function privateStaticMethod(string $param1, string $param2): string
3531
{
3632
return 'private_static ' . $param1 . $param2;
3733
}

tests/Fake/Input.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kenjis\PhpUnitHelper;
6+
7+
class Input
8+
{
9+
public function method(bool $upper = false): string
10+
{
11+
return 'GET';
12+
}
13+
14+
/**
15+
* @param mixed $index
16+
*
17+
* @return mixed
18+
*/
19+
public function get($index = null, bool $xssClean = false)
20+
{
21+
return 'GET value';
22+
}
23+
}

tests/TestDoubleTest.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TestDoubleTest extends TestCase
1414
public function test_getDouble_method_named_method(): void
1515
{
1616
$expected = 'DELETE';
17-
$mock = $this->getDouble(CI_Input::class, ['method' => $expected]);
17+
$mock = $this->getDouble(Input::class, ['method' => $expected]);
1818

1919
$actual = $mock->method();
2020

@@ -25,7 +25,7 @@ public function test_getDouble_method_returns_consecutive(): void
2525
{
2626
$returns = ['GET', 'PUT', 'DELETE'];
2727
$mock = $this->getDouble(
28-
CI_Input::class,
28+
Input::class,
2929
[
3030
['method' => $returns],
3131
]
@@ -45,7 +45,7 @@ public function test_getDouble_returns_consecutive_and_other(): void
4545
{
4646
$returns = ['GET', 'PUT', 'DELETE'];
4747
$mock = $this->getDouble(
48-
CI_Input::class,
48+
Input::class,
4949
[
5050
'get' => [],
5151
['method' => $returns],
@@ -71,14 +71,19 @@ public function test_getDouble_returns_consecutive_and_other(): void
7171
public function test_getDouble_method_returns_execption(): void
7272
{
7373
$this->expectException(RuntimeException::class);
74-
$this->expectExceptionMessage('I can throw an exception and can use params: abcdef');
74+
$this->expectExceptionMessage(
75+
'I can throw an exception and can use params: abc1'
76+
);
7577

7678
$ret = static function ($param1, $param2): void {
77-
throw new RuntimeException('I can throw an exception and can use params: ' . $param1 . $param2);
79+
throw new RuntimeException(
80+
'I can throw an exception and can use params: '
81+
. $param1 . $param2
82+
);
7883
};
79-
$mock = $this->getDouble(CI_Input::class, ['method' => $ret]);
84+
$mock = $this->getDouble(Input::class, ['get' => $ret]);
8085

81-
$mock->method('abc', 'def');
86+
$mock->get('abc', true);
8287
}
8388

8489
public function test_getDouble_constructor_param(): void
@@ -97,7 +102,7 @@ public function test_getDouble_constructor_param(): void
97102

98103
public function test_getDobule_method_returns_phpunit_stub(): void
99104
{
100-
$mock = $this->getDouble(CI_Email::class, ['to' => $this->returnSelf()]);
105+
$mock = $this->getDouble(Email::class, ['to' => $this->returnSelf()]);
101106
$test = $mock->to('test@example.com');
102107

103108
$this->assertEquals($mock, $test);
@@ -106,7 +111,7 @@ public function test_getDobule_method_returns_phpunit_stub(): void
106111
public function test_verifyInvokedOnce_with_params(): void
107112
{
108113
$expected = 'DELETE';
109-
$mock = $this->getDouble(CI_Input::class, ['method' => $expected]);
114+
$mock = $this->getDouble(Input::class, ['method' => $expected]);
110115

111116
$this->verifyInvokedOnce($mock, 'method', [true]);
112117

@@ -116,7 +121,7 @@ public function test_verifyInvokedOnce_with_params(): void
116121
public function test_verifyInvokedOnce_without_params(): void
117122
{
118123
$expected = 'DELETE';
119-
$mock = $this->getDouble(CI_Input::class, ['method' => $expected]);
124+
$mock = $this->getDouble(Input::class, ['method' => $expected]);
120125

121126
$this->verifyInvokedOnce($mock, 'method');
122127

@@ -126,7 +131,7 @@ public function test_verifyInvokedOnce_without_params(): void
126131
public function test_verifyInvoked_once(): void
127132
{
128133
$expected = 'DELETE';
129-
$mock = $this->getDouble(CI_Input::class, ['method' => $expected]);
134+
$mock = $this->getDouble(Input::class, ['method' => $expected]);
130135

131136
$this->verifyInvoked($mock, 'method');
132137

@@ -136,7 +141,7 @@ public function test_verifyInvoked_once(): void
136141
public function test_verifyInvoked_twice(): void
137142
{
138143
$expected = 'DELETE';
139-
$mock = $this->getDouble(CI_Input::class, ['method' => $expected]);
144+
$mock = $this->getDouble(Input::class, ['method' => $expected]);
140145

141146
$this->verifyInvoked($mock, 'method');
142147

@@ -147,7 +152,7 @@ public function test_verifyInvoked_twice(): void
147152
public function test_verifyInvokedMultipleTimes(): void
148153
{
149154
$expected = 'DELETE';
150-
$mock = $this->getDouble(CI_Input::class, ['method' => $expected]);
155+
$mock = $this->getDouble(Input::class, ['method' => $expected]);
151156

152157
$this->verifyInvokedMultipleTimes($mock, 'method', 3);
153158

@@ -159,7 +164,7 @@ public function test_verifyInvokedMultipleTimes(): void
159164
public function test_verifyNeverInvoked(): void
160165
{
161166
$expected = 'DELETE';
162-
$mock = $this->getDouble(CI_Input::class, ['method' => $expected]);
167+
$mock = $this->getDouble(Input::class, ['method' => $expected]);
163168

164169
$this->verifyNeverInvoked($mock, 'method');
165170
}

0 commit comments

Comments
 (0)