Skip to content

Commit

Permalink
Merge pull request #9 from pestphp/2.x_arch_function
Browse files Browse the repository at this point in the history
[2.x] Add `arch` test function
  • Loading branch information
nunomaduro authored Dec 5, 2023
2 parents 782574d + 28519b7 commit 44f98ec
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,12 @@
"@test:types",
"@test:unit"
]
},
"extra": {
"pest": {
"plugins": [
"Pest\\Arch\\Plugin"
]
}
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ parameters:
ignoreErrors:
- "#has a nullable return type declaration.#"
- "# with a nullable type declaration.#"
- "#has parameter \\$closure with null as default value.#"
18 changes: 18 additions & 0 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
declare(strict_types=1);

use Pest\Arch\Concerns\Architectable;
use Pest\PendingCalls\TestCall;
use Pest\Plugin;
use Pest\Support\HigherOrderTapProxy;

Plugin::uses(Architectable::class);

if (! function_exists('arch')) {
/**
* Adds the given closure as an architecture test. The first
* argument is the test description; the second argument
* is a closure that contains the test expectations.
*/
function arch(string $description, Closure $closure = null): TestCall
{
$test = test($description, $closure);

assert(! $test instanceof HigherOrderTapProxy);

return $test->group('arch');
}
}
29 changes: 29 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Pest\Arch;

use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Plugins\Concerns\HandleArguments;

/**
* @internal
*/
final class Plugin implements HandlesArguments
{
use HandleArguments;

public function handleArguments(array $arguments): array
{
if ($this->hasArgument('--arch', $arguments)) {
return $this->pushArgument('--group=arch', $this->popArgument('--arch', $arguments));
}

if ($this->hasArgument('--exclude-arch', $arguments)) {
return $this->pushArgument('--exclude-group=arch', $this->popArgument('--exclude-arch', $arguments));
}

return $arguments;
}
}
14 changes: 7 additions & 7 deletions tests/Arch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Pest\Expectation;
use Whoops\Exception\Frame;

test('base')
arch('base')
->expect('Pest\Arch')
->classes->toBeFinal()
->classes->not->toBeAbstract()
Expand All @@ -34,24 +34,24 @@
'Whoops\Exception\Frame',
])->ignoring(['PHPUnit\Framework', 'Composer']);

test('contracts')
arch('contracts')
->expect('Pest\Arch')
->not->toBeInterface()
->ignoring('Pest\Arch\Contracts');

test('collections')
arch('collections')
->expect('Pest\Arch\Collections')
->toOnlyUse('Pest\Arch\ValueObjects');

test('exceptions')
arch('exceptions')
->expect('Pest\Arch\Exceptions')
->toImplement(Throwable::class)
->toOnlyUse([
Frame::class,
Violation::class,
])->ignoring('PHPUnit\Framework');

test('expectations')
arch('expectations')
->expect('Pest\Arch\Expectations')
->toOnlyUse([
'expect',
Expand All @@ -60,7 +60,7 @@
'PHPUnit\Architecture\Elements\ObjectDescription',
])->ignoring('PHPUnit\Framework');

test('repositories')->expect('Pest\Arch\Repositories')->toOnlyUse([
arch('repositories')->expect('Pest\Arch\Repositories')->toOnlyUse([
'Pest\TestSuite',
'Pest\Arch\Factories',
'Pest\Arch\Objects',
Expand All @@ -70,7 +70,7 @@
'Symfony\Component\Finder\Finder',
]);

test('value objects')
arch('value objects')
->expect('Pest\Arch\ValueObjects')
->toUseNothing()
->ignoring([Targets::class, Dependency::class, 'PHPUnit\Framework', Expectation::class])
Expand Down
5 changes: 5 additions & 0 deletions tests/ArchFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

arch('this should have been added to an arch group', function () {
expect(test()->groups()[0])->toBe('arch');
});

0 comments on commit 44f98ec

Please sign in to comment.