Skip to content

Commit b7c0877

Browse files
Closes #4931 and #4955
1 parent ab74dd8 commit b7c0877

14 files changed

+441
-2
lines changed

ChangeLog-9.5.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes of the PHPUnit 9.5 release series are documented in this fil
44

55
## [9.5.24] - 2022-MM-DD
66

7+
### Added
8+
9+
* [#4931](https://github.com/sebastianbergmann/phpunit/issues/4931): Support `null` and `false` as stand-alone types
10+
* [#4955](https://github.com/sebastianbergmann/phpunit/issues/4955): Support `true` as stand-alone type
11+
712
### Fixed
813

914
* [#4913](https://github.com/sebastianbergmann/phpunit/issues/4913): Failed `assert()` should show a backtrace

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"sebastian/global-state": "^5.0.1",
4747
"sebastian/object-enumerator": "^4.0.3",
4848
"sebastian/resource-operations": "^3.0.3",
49-
"sebastian/type": "^3.0",
49+
"sebastian/type": "^3.1",
5050
"sebastian/version": "^3.0.2"
5151
},
5252
"config": {

src/Framework/MockObject/Invocation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public function generateReturnValue()
144144
return null;
145145
}
146146

147+
if (in_array('true', $types, true)) {
148+
return true;
149+
}
150+
147151
if (in_array('false', $types, true) ||
148152
in_array('bool', $types, true)) {
149153
return false;

src/Framework/MockObject/MockMethod.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,11 @@ private static function getMethodParametersForDeclaration(ReflectionMethod $meth
309309
}
310310

311311
if ($type !== null) {
312-
if ($typeName !== 'mixed' && $parameter->allowsNull() && !$type instanceof ReflectionIntersectionType && !$type instanceof ReflectionUnionType) {
312+
if ($typeName !== 'mixed' &&
313+
$typeName !== 'null' &&
314+
!$type instanceof ReflectionIntersectionType &&
315+
!$type instanceof ReflectionUnionType &&
316+
$parameter->allowsNull()) {
313317
$nullable = '?';
314318
}
315319

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\MockObject;
11+
12+
interface InterfaceWithMethodReturningFalse
13+
{
14+
public function returnsFalse(): false;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\MockObject;
11+
12+
interface InterfaceWithMethodReturningNull
13+
{
14+
public function returnsNull(): null;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\MockObject;
11+
12+
interface InterfaceWithMethodReturningTrue
13+
{
14+
public function returnsTrue(): true;
15+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
\PHPUnit\Framework\MockObject\Generator::generate('Foo', [], 'MockFoo', true, true)
3+
--SKIPIF--
4+
<?php declare(strict_types=1);
5+
if (version_compare('8.2.0-dev', PHP_VERSION, '>')) {
6+
print 'skip: PHP 8.2 is required.';
7+
}
8+
--FILE--
9+
<?php declare(strict_types=1);
10+
interface Foo
11+
{
12+
public function bar(false $baz): void;
13+
}
14+
15+
require_once __DIR__ . '/../../../../vendor/autoload.php';
16+
17+
$generator = new \PHPUnit\Framework\MockObject\Generator;
18+
19+
$mock = $generator->generate(
20+
'Foo',
21+
[],
22+
'MockFoo',
23+
true,
24+
true
25+
);
26+
27+
print $mock->getClassCode();
28+
--EXPECTF--
29+
declare(strict_types=1);
30+
31+
class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo
32+
{
33+
use \PHPUnit\Framework\MockObject\Api;
34+
use \PHPUnit\Framework\MockObject\Method;
35+
use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;
36+
37+
public function bar(false $baz): void
38+
{
39+
$__phpunit_arguments = [$baz];
40+
$__phpunit_count = func_num_args();
41+
42+
if ($__phpunit_count > 1) {
43+
$__phpunit_arguments_tmp = func_get_args();
44+
45+
for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
46+
$__phpunit_arguments[] = $__phpunit_arguments_tmp[$__phpunit_i];
47+
}
48+
}
49+
50+
$this->__phpunit_getInvocationHandler()->invoke(
51+
new \PHPUnit\Framework\MockObject\Invocation(
52+
'Foo', 'bar', $__phpunit_arguments, 'void', $this, true
53+
)
54+
);
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
\PHPUnit\Framework\MockObject\Generator::generate('Foo', [], 'MockFoo', true, true)
3+
--SKIPIF--
4+
<?php declare(strict_types=1);
5+
if (version_compare('8.2.0-dev', PHP_VERSION, '>')) {
6+
print 'skip: PHP 8.2 is required.';
7+
}
8+
--FILE--
9+
<?php declare(strict_types=1);
10+
interface Foo
11+
{
12+
public function bar(null $baz): void;
13+
}
14+
15+
require_once __DIR__ . '/../../../../vendor/autoload.php';
16+
17+
$generator = new \PHPUnit\Framework\MockObject\Generator;
18+
19+
$mock = $generator->generate(
20+
'Foo',
21+
[],
22+
'MockFoo',
23+
true,
24+
true
25+
);
26+
27+
print $mock->getClassCode();
28+
--EXPECTF--
29+
declare(strict_types=1);
30+
31+
class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo
32+
{
33+
use \PHPUnit\Framework\MockObject\Api;
34+
use \PHPUnit\Framework\MockObject\Method;
35+
use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;
36+
37+
public function bar(null $baz): void
38+
{
39+
$__phpunit_arguments = [$baz];
40+
$__phpunit_count = func_num_args();
41+
42+
if ($__phpunit_count > 1) {
43+
$__phpunit_arguments_tmp = func_get_args();
44+
45+
for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
46+
$__phpunit_arguments[] = $__phpunit_arguments_tmp[$__phpunit_i];
47+
}
48+
}
49+
50+
$this->__phpunit_getInvocationHandler()->invoke(
51+
new \PHPUnit\Framework\MockObject\Invocation(
52+
'Foo', 'bar', $__phpunit_arguments, 'void', $this, true
53+
)
54+
);
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
\PHPUnit\Framework\MockObject\Generator::generate('Foo', [], 'MockFoo', true, true)
3+
--SKIPIF--
4+
<?php declare(strict_types=1);
5+
if (version_compare('8.2.0-dev', PHP_VERSION, '>')) {
6+
print 'skip: PHP 8.2 is required.';
7+
}
8+
--FILE--
9+
<?php declare(strict_types=1);
10+
interface Foo
11+
{
12+
public function bar(true $baz): void;
13+
}
14+
15+
require_once __DIR__ . '/../../../../vendor/autoload.php';
16+
17+
$generator = new \PHPUnit\Framework\MockObject\Generator;
18+
19+
$mock = $generator->generate(
20+
'Foo',
21+
[],
22+
'MockFoo',
23+
true,
24+
true
25+
);
26+
27+
print $mock->getClassCode();
28+
--EXPECTF--
29+
declare(strict_types=1);
30+
31+
class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo
32+
{
33+
use \PHPUnit\Framework\MockObject\Api;
34+
use \PHPUnit\Framework\MockObject\Method;
35+
use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;
36+
37+
public function bar(true $baz): void
38+
{
39+
$__phpunit_arguments = [$baz];
40+
$__phpunit_count = func_num_args();
41+
42+
if ($__phpunit_count > 1) {
43+
$__phpunit_arguments_tmp = func_get_args();
44+
45+
for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
46+
$__phpunit_arguments[] = $__phpunit_arguments_tmp[$__phpunit_i];
47+
}
48+
}
49+
50+
$this->__phpunit_getInvocationHandler()->invoke(
51+
new \PHPUnit\Framework\MockObject\Invocation(
52+
'Foo', 'bar', $__phpunit_arguments, 'void', $this, true
53+
)
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)