mirror of
https://github.com/pestphp/pest.git
synced 2026-03-07 00:07:22 +01:00
Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e4e9cb09e4 | |||
| 6a7597c01a | |||
| dd05452edd | |||
| 99ea9f42e5 | |||
| 7d6a86adc7 | |||
| 7fbd2661c8 | |||
| 6ce678d1c2 | |||
| 5049b996db | |||
| d838456caa | |||
| e45c4ff4f1 | |||
| fa3959db17 | |||
| b97e206f7a | |||
| 7e9edecc7f | |||
| c290909eb3 | |||
| f2e56da2da | |||
| e6b258534a | |||
| acef002a2d | |||
| 11ebe014fb | |||
| 2d13c6e219 | |||
| fbcb492c79 | |||
| 4f67eff619 | |||
| 3c2c767e09 | |||
| ff527baa1d | |||
| 621718d4b1 | |||
| 59adc57344 |
13
CHANGELOG.md
13
CHANGELOG.md
@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
|
## [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))
|
||||||
|
- Access to test case methods and properties when using `skip` ([#338](https://github.com/pestphp/pest/pull/338))
|
||||||
|
|
||||||
|
## [v1.7.1 (2021-06-24)](https://github.com/pestphp/pest/compare/v1.7.0...v1.7.1)
|
||||||
|
### Fixed
|
||||||
|
- The `and` method not being usable in Higher Order expectations ([#330](https://github.com/pestphp/pest/pull/330))
|
||||||
|
|
||||||
## [v1.7.0 (2021-06-19)](https://github.com/pestphp/pest/compare/v1.6.0...v1.7.0)
|
## [v1.7.0 (2021-06-19)](https://github.com/pestphp/pest/compare/v1.6.0...v1.7.0)
|
||||||
### Added
|
### Added
|
||||||
- Support for non-callable values in the sequence method, which will be passed as `toEqual` ([#323](https://github.com/pestphp/pest/pull/323))
|
- Support for non-callable values in the sequence method, which will be passed as `toEqual` ([#323](https://github.com/pestphp/pest/pull/323))
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
"php": "^7.3 || ^8.0",
|
"php": "^7.3 || ^8.0",
|
||||||
"nunomaduro/collision": "^5.4.0",
|
"nunomaduro/collision": "^5.4.0",
|
||||||
"pestphp/pest-plugin": "^1.0.0",
|
"pestphp/pest-plugin": "^1.0.0",
|
||||||
"phpunit/phpunit": ">= 9.3.7 <= 9.5.5"
|
"phpunit/phpunit": ">= 9.3.7 <= 9.5.6"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|||||||
@ -63,6 +63,20 @@ final class HigherOrderExpectation
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new expectation.
|
||||||
|
*
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
|
* @param TValue $value
|
||||||
|
*
|
||||||
|
* @return Expectation<TValue>
|
||||||
|
*/
|
||||||
|
public function and($value): Expectation
|
||||||
|
{
|
||||||
|
return $this->expect($value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamically calls methods on the class with the given arguments.
|
* Dynamically calls methods on the class with the given arguments.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -7,14 +7,15 @@ namespace Pest\PendingObjects;
|
|||||||
use Closure;
|
use Closure;
|
||||||
use Pest\Factories\TestCaseFactory;
|
use Pest\Factories\TestCaseFactory;
|
||||||
use Pest\Support\Backtrace;
|
use Pest\Support\Backtrace;
|
||||||
|
use Pest\Support\HigherOrderCallables;
|
||||||
use Pest\Support\NullClosure;
|
use Pest\Support\NullClosure;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
use SebastianBergmann\Exporter\Exporter;
|
use SebastianBergmann\Exporter\Exporter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @method \Pest\Expectations\Expectation expect(mixed $value)
|
|
||||||
*
|
|
||||||
* @internal
|
* @internal
|
||||||
|
*
|
||||||
|
* @mixin HigherOrderCallables
|
||||||
*/
|
*/
|
||||||
final class TestCall
|
final class TestCall
|
||||||
{
|
{
|
||||||
@ -58,11 +59,15 @@ final class TestCall
|
|||||||
/**
|
/**
|
||||||
* Asserts that the test throws the given `$exceptionClass` when called.
|
* 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
|
if (class_exists($exception)) {
|
||||||
->proxies
|
$this->testCaseFactory
|
||||||
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]);
|
->proxies
|
||||||
|
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]);
|
||||||
|
} else {
|
||||||
|
$exceptionMessage = $exception;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_string($exceptionMessage)) {
|
if (is_string($exceptionMessage)) {
|
||||||
$this->testCaseFactory
|
$this->testCaseFactory
|
||||||
@ -143,11 +148,9 @@ final class TestCall
|
|||||||
? $conditionOrMessage
|
? $conditionOrMessage
|
||||||
: $message;
|
: $message;
|
||||||
|
|
||||||
if ($condition() !== false) {
|
$this->testCaseFactory
|
||||||
$this->testCaseFactory
|
->chains
|
||||||
->chains
|
->addWhen($condition, Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
|
||||||
->add(Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ namespace Pest;
|
|||||||
|
|
||||||
function version(): string
|
function version(): string
|
||||||
{
|
{
|
||||||
return '1.7.0';
|
return '1.9.0';
|
||||||
}
|
}
|
||||||
|
|
||||||
function testDirectory(string $file = ''): string
|
function testDirectory(string $file = ''): string
|
||||||
|
|||||||
@ -81,7 +81,6 @@ final class Coverage implements AddsOutput, HandlesArguments
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($input->getOption(self::MIN_OPTION) !== null) {
|
if ($input->getOption(self::MIN_OPTION) !== null) {
|
||||||
/* @phpstan-ignore-next-line */
|
|
||||||
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION);
|
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
65
src/Support/HigherOrderCallables.php
Normal file
65
src/Support/HigherOrderCallables.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Support;
|
||||||
|
|
||||||
|
use Pest\Expectation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class HigherOrderCallables
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var object
|
||||||
|
*/
|
||||||
|
private $target;
|
||||||
|
|
||||||
|
public function __construct(object $target)
|
||||||
|
{
|
||||||
|
$this->target = $target;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
|
* Create a new expectation. Callable values will be executed prior to returning the new expectation.
|
||||||
|
*
|
||||||
|
* @param callable|TValue $value
|
||||||
|
*
|
||||||
|
* @return Expectation<TValue>
|
||||||
|
*/
|
||||||
|
public function expect($value)
|
||||||
|
{
|
||||||
|
return new Expectation(is_callable($value) ? Reflection::bindCallable($value) : $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
|
* Create a new expectation. Callable values will be executed prior to returning the new expectation.
|
||||||
|
*
|
||||||
|
* @param callable|TValue $value
|
||||||
|
*
|
||||||
|
* @return Expectation<TValue>
|
||||||
|
*/
|
||||||
|
public function and($value)
|
||||||
|
{
|
||||||
|
return $this->expect($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
|
* @param callable(): TValue $callable
|
||||||
|
*
|
||||||
|
* @return TValue|object
|
||||||
|
*/
|
||||||
|
public function tap(callable $callable)
|
||||||
|
{
|
||||||
|
Reflection::bindCallable($callable);
|
||||||
|
|
||||||
|
return $this->target;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Support;
|
namespace Pest\Support;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@ -50,6 +51,13 @@ final class HigherOrderMessage
|
|||||||
*/
|
*/
|
||||||
public $arguments;
|
public $arguments;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An optional condition that will determine if the message will be executed.
|
||||||
|
*
|
||||||
|
* @var callable(): bool|null
|
||||||
|
*/
|
||||||
|
public $condition = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new higher order message.
|
* Creates a new higher order message.
|
||||||
*
|
*
|
||||||
@ -70,6 +78,16 @@ final class HigherOrderMessage
|
|||||||
*/
|
*/
|
||||||
public function call(object $target)
|
public function call(object $target)
|
||||||
{
|
{
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
if (is_callable($this->condition) && call_user_func(Closure::bind($this->condition, $target)) === false) {
|
||||||
|
return $target;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->hasHigherOrderCallable()) {
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
return (new HigherOrderCallables($target))->{$this->methodName}(...$this->arguments);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Reflection::call($target, $this->methodName, $this->arguments);
|
return Reflection::call($target, $this->methodName, $this->arguments);
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
@ -88,6 +106,28 @@ final class HigherOrderMessage
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that this message should only be called when the given condition is true.
|
||||||
|
*
|
||||||
|
* @param callable(): bool $condition
|
||||||
|
*/
|
||||||
|
public function when(callable $condition): self
|
||||||
|
{
|
||||||
|
$this->condition = $condition;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether or not there exists a higher order callable with the message name.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function hasHigherOrderCallable()
|
||||||
|
{
|
||||||
|
return in_array($this->methodName, get_class_methods(HigherOrderCallables::class), true);
|
||||||
|
}
|
||||||
|
|
||||||
private static function getUndefinedMethodMessage(object $target, string $methodName): string
|
private static function getUndefinedMethodMessage(object $target, string $methodName): string
|
||||||
{
|
{
|
||||||
if (\PHP_MAJOR_VERSION >= 8) {
|
if (\PHP_MAJOR_VERSION >= 8) {
|
||||||
|
|||||||
@ -24,6 +24,16 @@ final class HigherOrderMessageCollection
|
|||||||
$this->messages[] = new HigherOrderMessage($filename, $line, $methodName, $arguments);
|
$this->messages[] = new HigherOrderMessage($filename, $line, $methodName, $arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new higher order message to the collection if the callable condition is does not return false.
|
||||||
|
*
|
||||||
|
* @param array<int, mixed> $arguments
|
||||||
|
*/
|
||||||
|
public function addWhen(callable $condition, string $filename, int $line, string $methodName, array $arguments): void
|
||||||
|
{
|
||||||
|
$this->messages[] = (new HigherOrderMessage($filename, $line, $methodName, $arguments))->when($condition);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proxy all the messages starting from the target.
|
* Proxy all the messages starting from the target.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -41,15 +41,25 @@ final class Reflection
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_callable($method)) {
|
if (is_callable($method)) {
|
||||||
return Closure::fromCallable($method)->bindTo(
|
return static::bindCallable($method, $args);
|
||||||
TestSuite::getInstance()->test
|
|
||||||
)(...$args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $exception;
|
throw $exception;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind a callable to the TestCase and return the result.
|
||||||
|
*
|
||||||
|
* @param array<int, mixed> $args
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function bindCallable(callable $callable, array $args = [])
|
||||||
|
{
|
||||||
|
return Closure::fromCallable($callable)->bindTo(TestSuite::getInstance()->test)(...$args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Infers the file name from the given closure.
|
* Infers the file name from the given closure.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -116,6 +116,7 @@
|
|||||||
PASS Tests\Features\Expect\HigherOrder\methodsAndProperties
|
PASS Tests\Features\Expect\HigherOrder\methodsAndProperties
|
||||||
✓ it can access methods and properties
|
✓ it can access methods and properties
|
||||||
✓ it can handle nested methods and properties
|
✓ it can handle nested methods and properties
|
||||||
|
✓ it can start a new higher order expectation using the and syntax
|
||||||
|
|
||||||
PASS Tests\Features\Expect\HigherOrder\properties
|
PASS Tests\Features\Expect\HigherOrder\properties
|
||||||
✓ it allows properties to be accessed from the value
|
✓ it allows properties to be accessed from the value
|
||||||
@ -409,6 +410,8 @@
|
|||||||
PASS Tests\Features\HigherOrderTests
|
PASS Tests\Features\HigherOrderTests
|
||||||
✓ it proxies calls to object
|
✓ it proxies calls to object
|
||||||
✓ it is capable doing multiple assertions
|
✓ it is capable doing multiple assertions
|
||||||
|
✓ it resolves expect callables correctly
|
||||||
|
✓ it can tap into the test
|
||||||
|
|
||||||
WARN Tests\Features\Incompleted
|
WARN Tests\Features\Incompleted
|
||||||
… incompleted
|
… incompleted
|
||||||
@ -578,5 +581,5 @@
|
|||||||
✓ it is a test
|
✓ it is a test
|
||||||
✓ it uses correct parent class
|
✓ it uses correct parent class
|
||||||
|
|
||||||
Tests: 4 incompleted, 7 skipped, 362 passed
|
Tests: 4 incompleted, 7 skipped, 365 passed
|
||||||
|
|
||||||
@ -13,3 +13,7 @@ it('catch exceptions', function () {
|
|||||||
it('catch exceptions and messages', function () {
|
it('catch exceptions and messages', function () {
|
||||||
throw new Exception('Something bad happened');
|
throw new Exception('Something bad happened');
|
||||||
})->throws(Exception::class, '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');
|
||||||
|
|||||||
@ -22,6 +22,17 @@ it('can handle nested methods and properties', function () {
|
|||||||
->newInstance()->books()->toBeArray();
|
->newInstance()->books()->toBeArray();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can start a new higher order expectation using the and syntax', function () {
|
||||||
|
expect(new HasMethodsAndProperties())
|
||||||
|
->toBeInstanceOf(HasMethodsAndProperties::class)
|
||||||
|
->meta->toBeArray
|
||||||
|
->and(['foo' => 'bar'])
|
||||||
|
->toBeArray()
|
||||||
|
->foo->toEqual('bar');
|
||||||
|
|
||||||
|
expect(static::getCount())->toEqual(4);
|
||||||
|
});
|
||||||
|
|
||||||
class HasMethodsAndProperties
|
class HasMethodsAndProperties
|
||||||
{
|
{
|
||||||
public $name = 'Has Methods and Properties';
|
public $name = 'Has Methods and Properties';
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
beforeEach()->assertTrue(true);
|
beforeEach()->assertTrue(true);
|
||||||
|
|
||||||
it('proxies calls to object')->assertTrue(true);
|
it('proxies calls to object')->assertTrue(true);
|
||||||
@ -8,4 +10,18 @@ it('is capable doing multiple assertions')
|
|||||||
->assertTrue(true)
|
->assertTrue(true)
|
||||||
->assertFalse(false);
|
->assertFalse(false);
|
||||||
|
|
||||||
|
it('resolves expect callables correctly')
|
||||||
|
->expect(function () { return 'foo'; })
|
||||||
|
->toBeString()
|
||||||
|
->toBe('foo')
|
||||||
|
->and('bar')
|
||||||
|
->toBeString()
|
||||||
|
->toBe('bar');
|
||||||
|
|
||||||
|
it('can tap into the test')
|
||||||
|
->expect('foo')->toBeString()
|
||||||
|
->tap(function () { expect($this)->toBeInstanceOf(TestCase::class); })
|
||||||
|
->toBe('foo')
|
||||||
|
->and('hello world')->toBeString();
|
||||||
|
|
||||||
afterEach()->assertTrue(true);
|
afterEach()->assertTrue(true);
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->shouldSkip = true;
|
||||||
|
});
|
||||||
|
|
||||||
it('do not skips')
|
it('do not skips')
|
||||||
->skip(false)
|
->skip(false)
|
||||||
->assertTrue(true);
|
->assertTrue(true);
|
||||||
@ -31,3 +35,12 @@ it('skips with condition and message')
|
|||||||
it('skips when skip after assertion')
|
it('skips when skip after assertion')
|
||||||
->assertTrue(true)
|
->assertTrue(true)
|
||||||
->skip();
|
->skip();
|
||||||
|
|
||||||
|
it('can use something in the test case as a condition')
|
||||||
|
->skip(function () { return $this->shouldSkip; }, 'This test was skipped')
|
||||||
|
->assertTrue(false);
|
||||||
|
|
||||||
|
it('can user higher order callables and skip')
|
||||||
|
->skip(function () { return $this->shouldSkip; })
|
||||||
|
->expect(function () { return $this->shouldSkip; })
|
||||||
|
->toBeFalse();
|
||||||
|
|||||||
Reference in New Issue
Block a user