mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ebba1298a | |||
| 2e0d1bb5a0 | |||
| 09d2b16767 | |||
| f56556eb73 | |||
| f387ca8624 | |||
| ca9d783cf9 | |||
| 863ddea50b | |||
| d217503a6a | |||
| b6012862c4 | |||
| 60c0ad006f | |||
| 79ff332afe | |||
| 595bbe32a4 | |||
| 328427bfdb | |||
| 51f556799c | |||
| 5e0a0855ea | |||
| 7ec3460d73 | |||
| 4c8c42cd20 | |||
| 09682dd393 | |||
| 82c18d3848 | |||
| 3bdba9210d | |||
| 371620d161 | |||
| 47ceb2419b | |||
| 771b5b2e53 | |||
| 027e69e48f | |||
| 33e01e3805 | |||
| 13781dcd14 | |||
| 164bad437a | |||
| 7ddcc03ad9 | |||
| bbe4445257 | |||
| d90ddf889c | |||
| 5907164749 | |||
| eb6de433b7 | |||
| 32f72cdf55 | |||
| c160d97428 | |||
| 2a8de0565f | |||
| 5ab3c6e2d7 | |||
| 50ece576a7 | |||
| 9c202fa2d7 | |||
| e4e9cb09e4 | |||
| 6a7597c01a | |||
| dd05452edd | |||
| 99ea9f42e5 |
30
CHANGELOG.md
30
CHANGELOG.md
@ -4,6 +4,36 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
## [v1.12.0 (2021-07-26)](https://github.com/pestphp/pest/compare/v1.11.0...v1.12.0)
|
||||
### Added
|
||||
- `--force` option to override tests in `pest:test` artisan command ([#353](https://github.com/pestphp/pest/pull/353))
|
||||
- Support for PHPUnit `^9.3.7` ([ca9d783](https://github.com/pestphp/pest/commit/ca9d783cf942a2caabc85ff7a728c7f28350c67a))
|
||||
|
||||
### Fixed
|
||||
- `beforeAll` and `afterAll` behind called multiple times per test ([#357](https://github.com/pestphp/pest/pull/357))
|
||||
|
||||
## [v1.11.0 (2021-07-21)](https://github.com/pestphp/pest/compare/v1.10.0...v1.11.0)
|
||||
### Added
|
||||
- Support for interacting with datasets in higher order tests ([#352](https://github.com/pestphp/pest/pull/352))
|
||||
|
||||
### Changed
|
||||
- The unit test stub now uses the expectation API ([#348](https://github.com/pestphp/pest/pull/348))
|
||||
|
||||
### Fixed
|
||||
- PhpStorm will no longer show 0 assertions in the output ([#349](https://github.com/pestphp/pest/pull/349))
|
||||
|
||||
## [v1.10.0 (2021-07-12)](https://github.com/pestphp/pest/compare/v1.9.1...v1.10.0)
|
||||
### Added
|
||||
- The ability to use higher order expectations inside higher order tests ([#341](https://github.com/pestphp/pest/pull/341))
|
||||
|
||||
## [v1.9.1 (2021-07-11)](https://github.com/pestphp/pest/compare/v1.9.0...v1.9.1)
|
||||
### Fixed
|
||||
- Callable `expect` values in higher order tests failing if the value was an existing method name ([#334](https://github.com/pestphp/pest/pull/344))
|
||||
|
||||
## [v1.9.0 (2021-07-09)](https://github.com/pestphp/pest/compare/v1.8.0...v1.9.0)
|
||||
### Changed
|
||||
- You may now pass just an exception message when using the `throws` method ([#339](https://github.com/pestphp/pest/pull/339))
|
||||
|
||||
## [v1.8.0 (2021-07-08)](https://github.com/pestphp/pest/compare/v1.7.1...v1.8.0)
|
||||
### Added
|
||||
- A new `tap` and test case aware `expect` methods for higher order tests ([#331](https://github.com/pestphp/pest/pull/331))
|
||||
|
||||
@ -21,8 +21,9 @@ We would like to extend our thanks to the following sponsors for funding Pest de
|
||||
|
||||
### Premium Sponsors
|
||||
|
||||
- **[Scout APM](https://scoutapm.com)**
|
||||
- **[Akaunting](https://akaunting.com)**
|
||||
- **[Scout APM](https://scoutapm.com)**
|
||||
- **[Meema](https://meema.io/)**
|
||||
- **[Spatie](https://spatie.be/)**
|
||||
|
||||
Pest was created by **[Nuno Maduro](https://twitter.com/enunomaduro)** under the **[Sponsorware license](https://github.com/sponsorware/docs)**. It got open-sourced and is now licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
"php": "^7.3 || ^8.0",
|
||||
"nunomaduro/collision": "^5.4.0",
|
||||
"pestphp/pest-plugin": "^1.0.0",
|
||||
"phpunit/phpunit": ">= 9.3.7 <= 9.5.6"
|
||||
"phpunit/phpunit": "^9.3.7"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
@ -73,6 +73,8 @@ trait Testable
|
||||
{
|
||||
$this->__test = $test;
|
||||
$this->__description = $description;
|
||||
self::$beforeAll = null;
|
||||
self::$afterAll = null;
|
||||
|
||||
parent::__construct('__test', $data);
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ final class PestTestCommand extends Command
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'pest:test {name : The name of the file} {--unit : Create a unit test} {--dusk : Create a Dusk test} {--test-directory=tests : The name of the tests directory}';
|
||||
protected $signature = 'pest:test {name : The name of the file} {--unit : Create a unit test} {--dusk : Create a Dusk test} {--test-directory=tests : The name of the tests directory} {--force : Overwrite the existing test file with the same name}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
@ -56,7 +56,7 @@ final class PestTestCommand extends Command
|
||||
File::makeDirectory(dirname($target), 0777, true, true);
|
||||
}
|
||||
|
||||
if (File::exists($target)) {
|
||||
if (File::exists($target) && !(bool) $this->option('force')) {
|
||||
throw new InvalidConsoleArgument(sprintf('%s already exist', $target));
|
||||
}
|
||||
|
||||
|
||||
@ -8,9 +8,11 @@ use function getmypid;
|
||||
use Pest\Concerns\Testable;
|
||||
use PHPUnit\Framework\AssertionFailedError;
|
||||
use PHPUnit\Framework\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Framework\TestResult;
|
||||
use PHPUnit\Framework\TestSuite;
|
||||
use PHPUnit\Framework\Warning;
|
||||
use PHPUnit\Runner\PhptTestCase;
|
||||
use PHPUnit\TextUI\DefaultResultPrinter;
|
||||
use function round;
|
||||
use function str_replace;
|
||||
@ -137,6 +139,12 @@ final class TeamCity extends DefaultResultPrinter
|
||||
return;
|
||||
}
|
||||
|
||||
if ($test instanceof TestCase) {
|
||||
$this->numAssertions += $test->getNumAssertions();
|
||||
} elseif ($test instanceof PhptTestCase) {
|
||||
$this->numAssertions++;
|
||||
}
|
||||
|
||||
$this->printEvent('testFinished', [
|
||||
self::NAME => $test->getName(),
|
||||
self::DURATION => self::toMilliseconds($time),
|
||||
|
||||
@ -59,11 +59,15 @@ final class TestCall
|
||||
/**
|
||||
* Asserts that the test throws the given `$exceptionClass` when called.
|
||||
*/
|
||||
public function throws(string $exceptionClass, string $exceptionMessage = null): TestCall
|
||||
public function throws(string $exception, string $exceptionMessage = null): TestCall
|
||||
{
|
||||
$this->testCaseFactory
|
||||
->proxies
|
||||
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]);
|
||||
if (class_exists($exception)) {
|
||||
$this->testCaseFactory
|
||||
->proxies
|
||||
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]);
|
||||
} else {
|
||||
$exceptionMessage = $exception;
|
||||
}
|
||||
|
||||
if (is_string($exceptionMessage)) {
|
||||
$this->testCaseFactory
|
||||
@ -151,12 +155,30 @@ final class TestCall
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the property accessors to be used on the target.
|
||||
*/
|
||||
public function __get(string $name): self
|
||||
{
|
||||
return $this->addChain($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the calls to be used on the target.
|
||||
*
|
||||
* @param array<int, mixed> $arguments
|
||||
*/
|
||||
public function __call(string $name, array $arguments): self
|
||||
{
|
||||
return $this->addChain($name, $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a chain to the test case factory. Omitting the arguments will treat it as a property accessor.
|
||||
*
|
||||
* @param array<int, mixed>|null $arguments
|
||||
*/
|
||||
private function addChain(string $name, array $arguments = null): self
|
||||
{
|
||||
$this->testCaseFactory
|
||||
->chains
|
||||
@ -167,7 +189,9 @@ final class TestCall
|
||||
if ($this->testCaseFactory->description !== null) {
|
||||
$this->testCaseFactory->description .= ' → ';
|
||||
}
|
||||
$this->testCaseFactory->description .= sprintf('%s %s', $name, $exporter->shortenedRecursiveExport($arguments));
|
||||
$this->testCaseFactory->description .= $arguments === null
|
||||
? $name
|
||||
: sprintf('%s %s', $name, $exporter->shortenedRecursiveExport($arguments));
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
||||
@ -6,7 +6,7 @@ namespace Pest;
|
||||
|
||||
function version(): string
|
||||
{
|
||||
return '1.8.0';
|
||||
return '1.12.0';
|
||||
}
|
||||
|
||||
function testDirectory(string $file = ''): string
|
||||
|
||||
@ -4,7 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\Support;
|
||||
|
||||
use Closure;
|
||||
use Pest\Expectation;
|
||||
use Pest\PendingObjects\TestCall;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -32,7 +35,7 @@ final class HigherOrderCallables
|
||||
*/
|
||||
public function expect($value)
|
||||
{
|
||||
return new Expectation(is_callable($value) ? Reflection::bindCallable($value) : $value);
|
||||
return new Expectation($value instanceof Closure ? Reflection::bindCallableWithData($value) : $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -50,15 +53,13 @@ final class HigherOrderCallables
|
||||
}
|
||||
|
||||
/**
|
||||
* @template TValue
|
||||
* Tap into the test case to perform an action and return the test case.
|
||||
*
|
||||
* @param callable(): TValue $callable
|
||||
*
|
||||
* @return TValue|object
|
||||
* @return TestCall|TestCase|object
|
||||
*/
|
||||
public function tap(callable $callable)
|
||||
{
|
||||
Reflection::bindCallable($callable);
|
||||
Reflection::bindCallableWithData($callable);
|
||||
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
@ -34,18 +34,18 @@ final class HigherOrderMessage
|
||||
public $line;
|
||||
|
||||
/**
|
||||
* The method name.
|
||||
* The method or property name to access.
|
||||
*
|
||||
* @readonly
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $methodName;
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* The arguments.
|
||||
*
|
||||
* @var array<int, mixed>
|
||||
* @var array<int, mixed>|null
|
||||
*
|
||||
* @readonly
|
||||
*/
|
||||
@ -61,13 +61,13 @@ final class HigherOrderMessage
|
||||
/**
|
||||
* Creates a new higher order message.
|
||||
*
|
||||
* @param array<int, mixed> $arguments
|
||||
* @param array<int, mixed>|null $arguments
|
||||
*/
|
||||
public function __construct(string $filename, int $line, string $methodName, array $arguments)
|
||||
public function __construct(string $filename, int $line, string $methodName, $arguments)
|
||||
{
|
||||
$this->filename = $filename;
|
||||
$this->line = $line;
|
||||
$this->methodName = $methodName;
|
||||
$this->name = $methodName;
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
@ -85,21 +85,23 @@ final class HigherOrderMessage
|
||||
|
||||
if ($this->hasHigherOrderCallable()) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
return (new HigherOrderCallables($target))->{$this->methodName}(...$this->arguments);
|
||||
return (new HigherOrderCallables($target))->{$this->name}(...$this->arguments);
|
||||
}
|
||||
|
||||
try {
|
||||
return Reflection::call($target, $this->methodName, $this->arguments);
|
||||
return is_array($this->arguments)
|
||||
? Reflection::call($target, $this->name, $this->arguments)
|
||||
: $target->{$this->name}; /* @phpstan-ignore-line */
|
||||
} catch (Throwable $throwable) {
|
||||
Reflection::setPropertyValue($throwable, 'file', $this->filename);
|
||||
Reflection::setPropertyValue($throwable, 'line', $this->line);
|
||||
|
||||
if ($throwable->getMessage() === self::getUndefinedMethodMessage($target, $this->methodName)) {
|
||||
if ($throwable->getMessage() === self::getUndefinedMethodMessage($target, $this->name)) {
|
||||
/** @var ReflectionClass $reflection */
|
||||
$reflection = new ReflectionClass($target);
|
||||
/* @phpstan-ignore-next-line */
|
||||
$reflection = $reflection->getParentClass() ?: $reflection;
|
||||
Reflection::setPropertyValue($throwable, 'message', sprintf('Call to undefined method %s::%s()', $reflection->getName(), $this->methodName));
|
||||
Reflection::setPropertyValue($throwable, 'message', sprintf('Call to undefined method %s::%s()', $reflection->getName(), $this->name));
|
||||
}
|
||||
|
||||
throw $throwable;
|
||||
@ -125,7 +127,7 @@ final class HigherOrderMessage
|
||||
*/
|
||||
private function hasHigherOrderCallable()
|
||||
{
|
||||
return in_array($this->methodName, get_class_methods(HigherOrderCallables::class), true);
|
||||
return in_array($this->name, get_class_methods(HigherOrderCallables::class), true);
|
||||
}
|
||||
|
||||
private static function getUndefinedMethodMessage(object $target, string $methodName): string
|
||||
|
||||
@ -17,21 +17,21 @@ final class HigherOrderMessageCollection
|
||||
/**
|
||||
* Adds a new higher order message to the collection.
|
||||
*
|
||||
* @param array<int, mixed> $arguments
|
||||
* @param array<int, mixed>|null $arguments
|
||||
*/
|
||||
public function add(string $filename, int $line, string $methodName, array $arguments): void
|
||||
public function add(string $filename, int $line, string $name, array $arguments = null): void
|
||||
{
|
||||
$this->messages[] = new HigherOrderMessage($filename, $line, $methodName, $arguments);
|
||||
$this->messages[] = new HigherOrderMessage($filename, $line, $name, $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new higher order message to the collection if the callable condition is does not return false.
|
||||
*
|
||||
* @param array<int, mixed> $arguments
|
||||
* @param array<int, mixed>|null $arguments
|
||||
*/
|
||||
public function addWhen(callable $condition, string $filename, int $line, string $methodName, array $arguments): void
|
||||
public function addWhen(callable $condition, string $filename, int $line, string $name, array $arguments = null): void
|
||||
{
|
||||
$this->messages[] = (new HigherOrderMessage($filename, $line, $methodName, $arguments))->when($condition);
|
||||
$this->messages[] = (new HigherOrderMessage($filename, $line, $name, $arguments))->when($condition);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\Support;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
use Throwable;
|
||||
|
||||
@ -17,16 +18,14 @@ final class HigherOrderTapProxy
|
||||
/**
|
||||
* The target being tapped.
|
||||
*
|
||||
* @var mixed
|
||||
* @var TestCase
|
||||
*/
|
||||
public $target;
|
||||
|
||||
/**
|
||||
* Create a new tap proxy instance.
|
||||
*
|
||||
* @param mixed $target
|
||||
*/
|
||||
public function __construct($target)
|
||||
public function __construct(TestCase $target)
|
||||
{
|
||||
$this->target = $target;
|
||||
}
|
||||
|
||||
@ -60,6 +60,21 @@ final class Reflection
|
||||
return Closure::fromCallable($callable)->bindTo(TestSuite::getInstance()->test)(...$args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind a callable to the TestCase and return the result,
|
||||
* passing in the current dataset values as arguments.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function bindCallableWithData(callable $callable)
|
||||
{
|
||||
$test = TestSuite::getInstance()->test;
|
||||
|
||||
return $test === null
|
||||
? static::bindCallable($callable)
|
||||
: Closure::fromCallable($callable)->bindTo($test)(...$test->getProvidedData());
|
||||
}
|
||||
|
||||
/**
|
||||
* Infers the file name from the given closure.
|
||||
*/
|
||||
@ -94,10 +109,6 @@ final class Reflection
|
||||
}
|
||||
}
|
||||
|
||||
if ($reflectionProperty === null) {
|
||||
throw ShouldNotHappen::fromMessage('Reflection property not found.');
|
||||
}
|
||||
|
||||
$reflectionProperty->setAccessible(true);
|
||||
|
||||
return $reflectionProperty->getValue($object);
|
||||
@ -128,10 +139,6 @@ final class Reflection
|
||||
}
|
||||
}
|
||||
|
||||
if ($reflectionProperty === null) {
|
||||
throw ShouldNotHappen::fromMessage('Reflection property not found.');
|
||||
}
|
||||
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$reflectionProperty->setValue($object, $value);
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
|
||||
test('{name}', function () {
|
||||
assertTrue(true);
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
@ -13,3 +13,7 @@ it('catch exceptions', function () {
|
||||
it('catch exceptions and messages', function () {
|
||||
throw new Exception('Something bad happened');
|
||||
})->throws(Exception::class, 'Something bad happened');
|
||||
|
||||
it('can just define the message', function () {
|
||||
throw new Exception('Something bad happened');
|
||||
})->throws('Something bad happened');
|
||||
|
||||
@ -67,6 +67,13 @@ it('can handle nested method calls', function () {
|
||||
->books()->each->toBeArray();
|
||||
});
|
||||
|
||||
it('works with higher order tests')
|
||||
->expect(new HasMethods())
|
||||
->newInstance()->newInstance()->name()->toEqual('Has Methods')->toBeString()
|
||||
->newInstance()->name()->toEqual('Has Methods')->not->toBeArray
|
||||
->name()->toEqual('Has Methods')
|
||||
->books()->each->toBeArray;
|
||||
|
||||
class HasMethods
|
||||
{
|
||||
public function name()
|
||||
|
||||
@ -22,6 +22,13 @@ it('can handle nested methods and properties', function () {
|
||||
->newInstance()->books()->toBeArray();
|
||||
});
|
||||
|
||||
it('works with higher order tests')
|
||||
->expect(new HasMethodsAndProperties())
|
||||
->meta->foo->bar->toBeString()->toEqual('baz')->not->toBeInt
|
||||
->newInstance()->meta->foo->toBeArray
|
||||
->newInstance()->multiply(2, 2)->toEqual(4)->not->toEqual(5)
|
||||
->newInstance()->books()->toBeArray();
|
||||
|
||||
it('can start a new higher order expectation using the and syntax', function () {
|
||||
expect(new HasMethodsAndProperties())
|
||||
->toBeInstanceOf(HasMethodsAndProperties::class)
|
||||
@ -33,6 +40,14 @@ it('can start a new higher order expectation using the and syntax', function ()
|
||||
expect(static::getCount())->toEqual(4);
|
||||
});
|
||||
|
||||
it('can start a new higher order expectation using the and syntax in higher order tests')
|
||||
->expect(new HasMethodsAndProperties())
|
||||
->toBeInstanceOf(HasMethodsAndProperties::class)
|
||||
->meta->toBeArray
|
||||
->and(['foo' => 'bar'])
|
||||
->toBeArray()
|
||||
->foo->toEqual('bar');
|
||||
|
||||
class HasMethodsAndProperties
|
||||
{
|
||||
public $name = 'Has Methods and Properties';
|
||||
|
||||
@ -64,6 +64,11 @@ it('works with nested properties', function () {
|
||||
->posts->toBeArray()->toHaveCount(2);
|
||||
});
|
||||
|
||||
it('works with higher order tests')
|
||||
->expect(new HasProperties())
|
||||
->nested->foo->bar->toBeString()->toEqual('baz')
|
||||
->posts->toBeArray()->toHaveCount(2);
|
||||
|
||||
class HasProperties
|
||||
{
|
||||
public $name = 'foo';
|
||||
|
||||
@ -18,10 +18,29 @@ it('resolves expect callables correctly')
|
||||
->toBeString()
|
||||
->toBe('bar');
|
||||
|
||||
test('does not treat method names as callables')
|
||||
->expect('it')->toBeString();
|
||||
|
||||
it('can tap into the test')
|
||||
->expect('foo')->toBeString()
|
||||
->tap(function () { expect($this)->toBeInstanceOf(TestCase::class); })
|
||||
->toBe('foo')
|
||||
->and('hello world')->toBeString();
|
||||
|
||||
it('can pass datasets into the expect callables')
|
||||
->with([[1, 2, 3]])
|
||||
->expect(function (...$numbers) { return $numbers; })->toBe([1, 2, 3])
|
||||
->and(function (...$numbers) { return $numbers; })->toBe([1, 2, 3]);
|
||||
|
||||
it('can pass datasets into the tap callable')
|
||||
->with([[1, 2, 3]])
|
||||
->tap(function (...$numbers) { expect($numbers)->toBe([1, 2, 3]); });
|
||||
|
||||
it('can pass shared datasets into callables')
|
||||
->with('numbers.closure.wrapped')
|
||||
->expect(function ($value) { return $value; })
|
||||
->and(function ($value) { return $value; })
|
||||
->tap(function ($value) { expect($value)->toBeInt(); })
|
||||
->toBeInt();
|
||||
|
||||
afterEach()->assertTrue(true);
|
||||
|
||||
@ -2,26 +2,50 @@
|
||||
|
||||
global $globalHook;
|
||||
|
||||
// NOTE: this test does not have a $globalHook->calls offset since it is first
|
||||
// in the directory and thus will always run before the others. See also the
|
||||
// BeforeAllTest.php for details.
|
||||
|
||||
uses()->afterAll(function () use ($globalHook) {
|
||||
expect($globalHook)
|
||||
->toHaveProperty('afterAll')
|
||||
->and($globalHook->afterAll)
|
||||
->toBe(0);
|
||||
->toBe(0)
|
||||
->and($globalHook->calls)
|
||||
->afterAll
|
||||
->toBe(1);
|
||||
|
||||
$globalHook->afterAll = 1;
|
||||
$globalHook->calls->afterAll++;
|
||||
});
|
||||
|
||||
afterAll(function () use ($globalHook) {
|
||||
expect($globalHook)
|
||||
->toHaveProperty('afterAll')
|
||||
->and($globalHook->afterAll)
|
||||
->toBe(1);
|
||||
->toBe(1)
|
||||
->and($globalHook->calls)
|
||||
->afterAll
|
||||
->toBe(2);
|
||||
|
||||
$globalHook->afterAll = 2;
|
||||
$globalHook->calls->afterAll++;
|
||||
});
|
||||
|
||||
test('global afterAll execution order', function () use ($globalHook) {
|
||||
expect($globalHook)
|
||||
->not()
|
||||
->toHaveProperty('afterAll');
|
||||
->toHaveProperty('afterAll')
|
||||
->and($globalHook->calls)
|
||||
->afterAll
|
||||
->toBe(0);
|
||||
});
|
||||
|
||||
it('only gets called once per file', function () use ($globalHook) {
|
||||
expect($globalHook)
|
||||
->not()
|
||||
->toHaveProperty('afterAll')
|
||||
->and($globalHook->calls)
|
||||
->afterAll
|
||||
->toBe(0);
|
||||
});
|
||||
|
||||
@ -1,28 +1,56 @@
|
||||
<?php
|
||||
|
||||
use Pest\Support\Str;
|
||||
|
||||
global $globalHook;
|
||||
|
||||
uses()->beforeAll(function () use ($globalHook) {
|
||||
// HACK: we have to determine our $globalHook->calls baseline. This is because
|
||||
// two other tests are executed before this one due to filename ordering.
|
||||
$args = $_SERVER['argv'] ?? [];
|
||||
$single = isset($args[1]) && Str::endsWith(__FILE__, $args[1]);
|
||||
$offset = $single ? 0 : 2;
|
||||
|
||||
uses()->beforeAll(function () use ($globalHook, $offset) {
|
||||
expect($globalHook)
|
||||
->toHaveProperty('beforeAll')
|
||||
->and($globalHook->beforeAll)
|
||||
->toBe(0);
|
||||
->toBe(0)
|
||||
->and($globalHook->calls)
|
||||
->beforeAll
|
||||
->toBe(1 + $offset);
|
||||
|
||||
$globalHook->beforeAll = 1;
|
||||
$globalHook->calls->beforeAll++;
|
||||
});
|
||||
|
||||
beforeAll(function () use ($globalHook) {
|
||||
beforeAll(function () use ($globalHook, $offset) {
|
||||
expect($globalHook)
|
||||
->toHaveProperty('beforeAll')
|
||||
->and($globalHook->beforeAll)
|
||||
->toBe(1);
|
||||
->toBe(1)
|
||||
->and($globalHook->calls)
|
||||
->beforeAll
|
||||
->toBe(2 + $offset);
|
||||
|
||||
$globalHook->beforeAll = 2;
|
||||
$globalHook->calls->beforeAll++;
|
||||
});
|
||||
|
||||
test('global beforeAll execution order', function () use ($globalHook) {
|
||||
test('global beforeAll execution order', function () use ($globalHook, $offset) {
|
||||
expect($globalHook)
|
||||
->toHaveProperty('beforeAll')
|
||||
->and($globalHook->beforeAll)
|
||||
->toBe(2);
|
||||
->toBe(2)
|
||||
->and($globalHook->calls)
|
||||
->beforeAll
|
||||
->toBe(3 + $offset);
|
||||
});
|
||||
|
||||
it('only gets called once per file', function () use ($globalHook, $offset) {
|
||||
expect($globalHook)
|
||||
->beforeAll
|
||||
->toBe(2)
|
||||
->and($globalHook->calls)
|
||||
->beforeAll
|
||||
->toBe(3 + $offset);
|
||||
});
|
||||
|
||||
@ -2,7 +2,8 @@
|
||||
|
||||
uses()->group('integration')->in('Visual');
|
||||
|
||||
$globalHook = (object) []; // NOTE: global test value container to be mutated and checked across files, as needed
|
||||
// NOTE: global test value container to be mutated and checked across files, as needed
|
||||
$globalHook = (object) ['calls' => (object) ['beforeAll' => 0, 'afterAll' => 0]];
|
||||
|
||||
uses()
|
||||
->beforeEach(function () {
|
||||
@ -10,11 +11,13 @@ uses()
|
||||
})
|
||||
->beforeAll(function () use ($globalHook) {
|
||||
$globalHook->beforeAll = 0;
|
||||
$globalHook->calls->beforeAll++;
|
||||
})
|
||||
->afterEach(function () {
|
||||
$this->ith = 0;
|
||||
})
|
||||
->afterAll(function () use ($globalHook) {
|
||||
$globalHook->afterAll = 0;
|
||||
$globalHook->calls->afterAll++;
|
||||
})
|
||||
->in('Hooks');
|
||||
|
||||
Reference in New Issue
Block a user