Skip to content

Commit

Permalink
feat: add arch test function
Browse files Browse the repository at this point in the history
Adds a new `arch` function that is a synonym for `test()->group('arch')`. This allows users to easily isolate arch tests for inclusion/exclusion when running their test suite.
  • Loading branch information
lukeraymonddowning committed Nov 28, 2023
1 parent 59698f0 commit f8ad66d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
declare(strict_types=1);

use Pest\Arch\Concerns\Architectable;
use Pest\Concerns\Expectable;
use Pest\PendingCalls\TestCall;
use Pest\Plugin;
use PHPUnit\Framework\TestCase;

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.
*
* @return Expectable|TestCall|TestCase|mixed
*/
function arch(string $description, Closure $closure = null): TestCall
{
return test($description, $closure)->group('arch');
}
}
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 f8ad66d

Please sign in to comment.