diff --git a/src/Expectation.php b/src/Expectation.php index 979dde28f..5885bfeb9 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -862,4 +862,20 @@ public function toHaveAttribute(string $attribute): ArchExpectation FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), ); } + + /** + * Asserts that the given expectation target has a constructor method. + */ + public function toHaveConstructor(): ArchExpectation + { + return $this->toHaveMethod('__construct'); + } + + /** + * Asserts that the given expectation target has a destructor method. + */ + public function toHaveDestructor(): ArchExpectation + { + return $this->toHaveMethod('__destruct'); + } } diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index ad2728f69..669636777 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -469,4 +469,20 @@ public function throwExpectationFailedException(string $name, array|string $argu implode(' ', array_map(fn (mixed $argument): string => $toString($argument), $arguments)), )); } + + /** + * Asserts that the given expectation target does not have a constructor method. + */ + public function toHaveConstructor(): ArchExpectation + { + return $this->toHaveMethod('__construct'); + } + + /** + * Asserts that the given expectation target does not have a destructor method. + */ + public function toHaveDestructor(): ArchExpectation + { + return $this->toHaveMethod('__destruct'); + } } diff --git a/tests/Features/Expect/toHaveConstructor.php b/tests/Features/Expect/toHaveConstructor.php new file mode 100644 index 000000000..66f07e91a --- /dev/null +++ b/tests/Features/Expect/toHaveConstructor.php @@ -0,0 +1,9 @@ +expect('Tests\Fixtures\Arch\ToHaveConstructor\HasConstructor\HasConstructor') + ->toHaveConstructor(); + +test('class has no constructor') + ->expect('Tests\Fixtures\Arch\ToHaveConstructor\HasNoConstructor\HasNoConstructor') + ->not->toHaveConstructor(); diff --git a/tests/Features/Expect/toHaveDestructor.php b/tests/Features/Expect/toHaveDestructor.php new file mode 100644 index 000000000..a5126ed2e --- /dev/null +++ b/tests/Features/Expect/toHaveDestructor.php @@ -0,0 +1,9 @@ +expect('Tests\Fixtures\Arch\ToHaveDestructor\HasDestructor\HasDestructor') + ->toHaveDestructor(); + +test('class has no destructor') + ->expect('Tests\Fixtures\Arch\ToHaveDestructor\HasNoDestructor\HasNoDestructor') + ->not->toHaveDestructor(); diff --git a/tests/Fixtures/Arch/ToHaveConstructor/HasConstructor/HasConstructor.php b/tests/Fixtures/Arch/ToHaveConstructor/HasConstructor/HasConstructor.php new file mode 100644 index 000000000..924e250da --- /dev/null +++ b/tests/Fixtures/Arch/ToHaveConstructor/HasConstructor/HasConstructor.php @@ -0,0 +1,13 @@ +