-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't call constructors when stubbing objects
We are/should already be specifying defaults for all the properties, so it doesn't matter what the constructor does as we will be immediately resetting the object state to our expected values anyway. This is equivalent to e.g. Doctrine hydrating from a database record.
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
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
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,51 @@ | ||
<?php | ||
|
||
namespace test\integration; | ||
|
||
use DateTimeImmutable; | ||
use Ingenerator\StubObjects\Attribute\StubDefault\StubDefaultValue; | ||
use Ingenerator\StubObjects\StubObjects; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SkipConstructorTest extends TestCase | ||
{ | ||
|
||
public function test_it_can_stub_classes_that_have_constructor_args() | ||
{ | ||
$result = $this->newSubject()->stub(MyStubbableClassWithConstructor::class); | ||
$this->assertEquals( | ||
[ | ||
'something_upper' => 'UPPER', | ||
'nullable' => NULL, | ||
'whenever' => new DateTimeImmutable('2024-02-02 10:30:04'), | ||
'a_boolean' => FALSE, | ||
], | ||
(array) $result | ||
); | ||
} | ||
|
||
private function newSubject(array $stubbable_class_patterns = ['*']): StubObjects | ||
{ | ||
return new StubObjects(...get_defined_vars()); | ||
} | ||
} | ||
|
||
|
||
class MyStubbableClassWithConstructor | ||
{ | ||
#[StubDefaultValue('UPPER')] | ||
public string $something_upper; | ||
|
||
public function __construct( | ||
public ?string $nullable, | ||
#[StubDefaultValue('2024-02-02 10:30:04')] | ||
public DateTimeImmutable $whenever, | ||
string $something_else, | ||
// Note, we have to specify a default stub value because the =FALSE in the code is the *constructor param* | ||
// default, technically it is not a default property value. | ||
#[StubDefaultValue(FALSE)] | ||
public bool $a_boolean = FALSE, | ||
) { | ||
|
||
} | ||
} |