diff --git a/src/Factory/DefaultStubFactory.php b/src/Factory/DefaultStubFactory.php index f019f6f..c5a64b8 100644 --- a/src/Factory/DefaultStubFactory.php +++ b/src/Factory/DefaultStubFactory.php @@ -50,7 +50,7 @@ public function make(array $values, StubbingContext $context): object $values = $this->getDefaultsMerger()->merge($defaults, $values, $context); // Create the instance and apply customised values to it - $instance = $this->target_reflection->newInstance(); + $instance = $this->target_reflection->newInstanceWithoutConstructor(); foreach ($values as $prop_name => $value) { $this->castAndSetPropertyValue($prop_name, $context, $value, $instance); } diff --git a/test/integration/SkipConstructorTest.php b/test/integration/SkipConstructorTest.php new file mode 100644 index 0000000..03695cf --- /dev/null +++ b/test/integration/SkipConstructorTest.php @@ -0,0 +1,51 @@ +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, + ) { + + } +}