Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mocking classes with
new
initializers in method and attribute p…
…arams on PHP 8.1 (#1301) Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
- Loading branch information
Showing
11 changed files
with
166 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ | |
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Fixture\\": "tests/Fixture/", | ||
"test\\": "tests/" | ||
}, | ||
"files": [ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fixture\PHP81; | ||
|
||
class A | ||
{ | ||
public function __construct( | ||
private int $x = 1 | ||
) { | ||
} | ||
|
||
public function test(): int | ||
{ | ||
return $this->x; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fixture\PHP81; | ||
|
||
class B | ||
{ | ||
public function __construct( | ||
private int $x | ||
) { | ||
} | ||
|
||
public function test(): int | ||
{ | ||
return $this->x; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fixture\PHP81; | ||
|
||
class C | ||
{ | ||
public function __construct( | ||
private int $x = 1, | ||
private B $b = new B(1) | ||
) { | ||
} | ||
|
||
public function test(): int | ||
{ | ||
return $this->x + $this->b->test(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fixture\PHP81; | ||
|
||
class HandlerClass | ||
{ | ||
public function doStuff(MockClass $mockClass): string | ||
{ | ||
return $mockClass->test('test'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fixture\PHP81; | ||
|
||
use DateTime; | ||
|
||
class MockClass | ||
{ | ||
public function __construct( | ||
public A $a = new A(), | ||
protected B $b = new B(1), | ||
private C $c = new C(b: new B(1), x: 2), | ||
public DateTime $dateTime = new DateTime(), | ||
) { | ||
} | ||
|
||
public function test( | ||
string $msg, | ||
#[SomeAttribute(param: new A())] | ||
A $a = new A(), | ||
B $b = new B(1), | ||
C $c = new C(x: 2), | ||
DateTime $dateTime = new DateTime(), | ||
): string { | ||
return $msg . ' - ' . $a->test() . ' - ' . $b->test() . ' - ' . $c->test() . ' - ' . $dateTime->format('Y-m-d'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fixture\PHP81; | ||
|
||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
class SomeAttribute | ||
{ | ||
public function __construct( | ||
public object $param = new stdClass() | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace test\Unit\PHP81; | ||
|
||
use Fixture\PHP81\HandlerClass; | ||
use Fixture\PHP81\MockClass; | ||
use Mockery\Adapter\Phpunit\MockeryTestCase; | ||
|
||
/** | ||
* @requires PHP 8.1.0-dev | ||
*/ | ||
class Php81LanguageFeaturesTest extends MockeryTestCase | ||
{ | ||
public function testNewInitializerExpression() | ||
{ | ||
$class = mock(MockClass::class) | ||
->expects('test') | ||
->with('test') | ||
->andReturn('it works') | ||
->getMock(); | ||
|
||
self::assertSame('it works', (new HandlerClass())->doStuff($class)); | ||
} | ||
} |