Skip to content

Commit

Permalink
Merge pull request #962 from JonPurvis/construct-destruct-expectations
Browse files Browse the repository at this point in the history
add toHaveConstructor() and toHaveDestructor() expectations
  • Loading branch information
nunomaduro authored Sep 18, 2023
2 parents c08f336 + bc08f2c commit 95b65fe
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
16 changes: 16 additions & 0 deletions src/Expectations/OppositeExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
9 changes: 9 additions & 0 deletions tests/Features/Expect/toHaveConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

test('class has constructor')
->expect('Tests\Fixtures\Arch\ToHaveConstructor\HasConstructor\HasConstructor')
->toHaveConstructor();

test('class has no constructor')
->expect('Tests\Fixtures\Arch\ToHaveConstructor\HasNoConstructor\HasNoConstructor')
->not->toHaveConstructor();
9 changes: 9 additions & 0 deletions tests/Features/Expect/toHaveDestructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

test('class has destructor')
->expect('Tests\Fixtures\Arch\ToHaveDestructor\HasDestructor\HasDestructor')
->toHaveDestructor();

test('class has no destructor')
->expect('Tests\Fixtures\Arch\ToHaveDestructor\HasNoDestructor\HasNoDestructor')
->not->toHaveDestructor();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHaveConstructor\HasConstructor;

class HasConstructor
{
public function __construct()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHaveConstructor\HasNoConstructor;

class HasNoConstructor
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHaveDestructor\HasDestructor;

class HasDestructor
{
public function __destruct()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHaveDestructor\HasNoDestructor;

class HasNoDestructor
{
}

0 comments on commit 95b65fe

Please sign in to comment.