-
Notifications
You must be signed in to change notification settings - Fork 461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix mocking classes with new
initializers in method and attribute params on PHP 8.1
#1301
Merged
ghostwriter
merged 10 commits into
1.6.x
from
bugfix/1300-mock-classes-with-new-in-methods-on-php-81
Jul 18, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
051341a
Setup autoload-dev for test fixtures
ghostwriter bd52fc6
Create Php81LanguageFeaturesTest.php
ghostwriter 0fccb6c
Create test fixtures
ghostwriter fc4e7fd
Update MethodDefinitionPass.php
ghostwriter 19c13f1
Fix styleci issues
ghostwriter 47d3dfc
Update psalm-baseline.xml
ghostwriter 1c42193
Make sure the closing bracket is at the end
ghostwriter 91fdf62
Include promoted properties
ghostwriter 5881188
Include parameter attribute with parameters
ghostwriter 638b2b9
Fix cs issue
ghostwriter File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not intended to ever call
__toString()
directly. This should be a cast instead ((string)$param
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Bilge I agree,
However the reason for using
__toString()
instead was because$param
is actually an object that is proxying these calls via__call
method to theReflectionParameter
which was giving an error because the__toString
expects a string return type.mockery/src/Mockery/Generator/Parameter.php
Line 28 in b1be135