From 6c73a3d90b2c0969dae0398a4a5dd625cb7ede9f Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Mon, 18 Sep 2023 01:00:50 +0100 Subject: [PATCH 1/2] initial commit --- src/Expectation.php | 16 ++++++++++++++++ src/Expectations/OppositeExpectation.php | 16 ++++++++++++++++ tests/Features/Expect/toHaveConstructor.php | 9 +++++++++ tests/Features/Expect/toHaveDestructor.php | 9 +++++++++ .../HasConstructor/HasConstructor.php | 13 +++++++++++++ .../HasNoConstructor/HasNoConstructor.php | 10 ++++++++++ .../HasDestructor/HasDestructor.php | 13 +++++++++++++ .../HasNoDestructor/HasNoDestructor.php | 10 ++++++++++ 8 files changed, 96 insertions(+) create mode 100644 tests/Features/Expect/toHaveConstructor.php create mode 100644 tests/Features/Expect/toHaveDestructor.php create mode 100644 tests/Fixtures/Arch/ToHaveConstructor/HasConstructor/HasConstructor.php create mode 100644 tests/Fixtures/Arch/ToHaveConstructor/HasNoConstructor/HasNoConstructor.php create mode 100644 tests/Fixtures/Arch/ToHaveDestructor/HasDestructor/HasDestructor.php create mode 100644 tests/Fixtures/Arch/ToHaveDestructor/HasNoDestructor/HasNoDestructor.php diff --git a/src/Expectation.php b/src/Expectation.php index 979dde28..5885bfeb 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 ad2728f6..66963677 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 00000000..66f07e91 --- /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 00000000..a5126ed2 --- /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 00000000..924e250d --- /dev/null +++ b/tests/Fixtures/Arch/ToHaveConstructor/HasConstructor/HasConstructor.php @@ -0,0 +1,13 @@ + Date: Mon, 18 Sep 2023 01:13:51 +0100 Subject: [PATCH 2/2] fix style issues --- .../Arch/ToHaveConstructor/HasNoConstructor/HasNoConstructor.php | 1 - .../Arch/ToHaveDestructor/HasNoDestructor/HasNoDestructor.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/Fixtures/Arch/ToHaveConstructor/HasNoConstructor/HasNoConstructor.php b/tests/Fixtures/Arch/ToHaveConstructor/HasNoConstructor/HasNoConstructor.php index ce8e0473..c798078e 100644 --- a/tests/Fixtures/Arch/ToHaveConstructor/HasNoConstructor/HasNoConstructor.php +++ b/tests/Fixtures/Arch/ToHaveConstructor/HasNoConstructor/HasNoConstructor.php @@ -6,5 +6,4 @@ class HasNoConstructor { - } diff --git a/tests/Fixtures/Arch/ToHaveDestructor/HasNoDestructor/HasNoDestructor.php b/tests/Fixtures/Arch/ToHaveDestructor/HasNoDestructor/HasNoDestructor.php index e4976283..eea037ed 100644 --- a/tests/Fixtures/Arch/ToHaveDestructor/HasNoDestructor/HasNoDestructor.php +++ b/tests/Fixtures/Arch/ToHaveDestructor/HasNoDestructor/HasNoDestructor.php @@ -6,5 +6,4 @@ class HasNoDestructor { - }