Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds toHaveNonPublicConstructor expectation #1317

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,29 @@ public function toHaveAttribute(string $attribute): ArchExpectation
);
}

/**
* Asserts that the given expectation target has a non-public constructor method.
*/
public function toHaveNonPublicConstructor(): ArchExpectation
{
$callback = function (ObjectDescription $object): bool {
$constructor = $object->reflectionClass->getConstructor();

if ($constructor === null) {
return false;
}

return ! $constructor->isPublic();
};

return Targeted::make(
$this,
$callback,
'to have non-public constructor',
FileLineFinder::where(fn (string $line): bool => str_contains($line, '__construct')),
);
}

/**
* Asserts that the given expectation target has a constructor method.
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/Features/Expect/toHaveNonPublicConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

test('class has private constructor')
->expect('Tests\Fixtures\Arch\ToHaveNonPublicConstructor\HasPrivateConstructor')
->toHaveNonPublicConstructor();

test('class has protected constructor')
->expect('Tests\Fixtures\Arch\ToHaveNonPublicConstructor\HasProtectedConstructor')
->toHaveNonPublicConstructor();

test('class has public constructor')
->expect('Tests\Fixtures\Arch\ToHaveConstructor\HasConstructor\HasConstructor')
->not->toHaveNonPublicConstructor();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHaveNonPublicConstructor;

class HasPrivateConstructor
{
private function __construct() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToHaveNonPublicConstructor;

class HasProtectedConstructor
{
protected function __construct() {}
}