Compare commits

...

24 Commits

Author SHA1 Message Date
eb6de433b7 docs: update changelog 2021-07-11 09:08:31 +01:00
32f72cdf55 Merge pull request #344 from pestphp/callable-expect-fix
Forces higher order test callable expects to be closures
2021-07-11 09:04:59 +01:00
c160d97428 Forces higher order test callable expects to be closures. 2021-07-11 07:56:02 +01:00
5ab3c6e2d7 Merge pull request #340 from pestphp/better-tap-type-hinting
Better tap type hinting
2021-07-09 16:34:18 +01:00
50ece576a7 Improves type-hinting for target in HigherOrderTapProxy 2021-07-09 16:09:53 +01:00
9c202fa2d7 Improves type-hinting for tap method 2021-07-09 16:07:37 +01:00
e4e9cb09e4 docs: update changelog 2021-07-09 09:15:22 +01:00
6a7597c01a Merge pull request #339 from pestphp/exception-improvements
Allows you to just specify an exception message when calling `throws`.
2021-07-09 09:08:05 +01:00
dd05452edd Renames a property to be more inclusive. 2021-07-08 18:39:09 +01:00
99ea9f42e5 Allows you to just specify an exception message when calling throws. 2021-07-08 18:32:02 +01:00
7d6a86adc7 docs: update changelog 2021-07-08 18:11:02 +01:00
7fbd2661c8 Merge pull request #338 from pestphp/skip-closure-support
Closures passed to the `skip` method are now bound to the test case
2021-07-08 18:05:27 +01:00
6ce678d1c2 Adds an extra test to ensure that skipping takes place before higher order callables. 2021-07-08 17:52:56 +01:00
5049b996db Merge branch 'master' into skip-closure-support
# Conflicts:
#	src/Support/HigherOrderMessage.php
2021-07-08 17:50:48 +01:00
d838456caa Merge pull request #331 from pestphp/higher-order-tap-and-defer
Adds a new `tap` method to Higher Order tests
2021-07-08 17:38:17 +01:00
e45c4ff4f1 Refactors HigherOrderMessage 2021-07-08 17:30:39 +01:00
fa3959db17 Type hints for callable 2021-07-08 13:12:30 +01:00
b97e206f7a Closures passed to the skip method are now bound to the test case to allow for more complex logic. 2021-07-08 09:44:28 +01:00
7e9edecc7f Higher Order Tests now resolve callable expectations. The tap method now always returns the test case. 2021-07-06 14:05:40 +01:00
c290909eb3 Adds @mixin for HigherOrderCallables class 2021-06-25 09:24:16 +01:00
f2e56da2da Updates snapshots 2021-06-24 22:58:29 +01:00
e6b258534a Merge branch 'master' into higher-order-tap-and-defer
# Conflicts:
#	tests/.snapshots/success.txt
2021-06-24 22:57:48 +01:00
acef002a2d Adds tap for Higher Order tests 2021-06-24 22:57:26 +01:00
4f67eff619 Start of work 2021-06-24 21:38:02 +01:00
13 changed files with 199 additions and 21 deletions

View File

@ -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/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [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))
- 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))

View File

@ -7,14 +7,15 @@ namespace Pest\PendingObjects;
use Closure;
use Pest\Factories\TestCaseFactory;
use Pest\Support\Backtrace;
use Pest\Support\HigherOrderCallables;
use Pest\Support\NullClosure;
use Pest\TestSuite;
use SebastianBergmann\Exporter\Exporter;
/**
* @method \Pest\Expectations\Expectation expect(mixed $value)
*
* @internal
*
* @mixin HigherOrderCallables
*/
final class TestCall
{
@ -58,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
@ -143,11 +148,9 @@ final class TestCall
? $conditionOrMessage
: $message;
if ($condition() !== false) {
$this->testCaseFactory
->chains
->add(Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
}
$this->testCaseFactory
->chains
->addWhen($condition, Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
return $this;
}

View File

@ -6,7 +6,7 @@ namespace Pest;
function version(): string
{
return '1.7.1';
return '1.9.1';
}
function testDirectory(string $file = ''): string

View File

@ -81,7 +81,6 @@ final class Coverage implements AddsOutput, HandlesArguments
}
if ($input->getOption(self::MIN_OPTION) !== null) {
/* @phpstan-ignore-next-line */
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION);
}

View File

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Closure;
use Pest\Expectation;
use Pest\PendingObjects\TestCall;
use PHPUnit\Framework\TestCase;
/**
* @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($value instanceof Closure ? 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);
}
/**
* Tap into the test case to perform an action and return the test case.
*
* @return TestCall|TestCase|object
*/
public function tap(callable $callable)
{
Reflection::bindCallable($callable);
return $this->target;
}
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pest\Support;
use Closure;
use ReflectionClass;
use Throwable;
@ -50,6 +51,13 @@ final class HigherOrderMessage
*/
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.
*
@ -70,6 +78,16 @@ final class HigherOrderMessage
*/
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 {
return Reflection::call($target, $this->methodName, $this->arguments);
} 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
{
if (\PHP_MAJOR_VERSION >= 8) {

View File

@ -24,6 +24,16 @@ final class HigherOrderMessageCollection
$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.
*/

View File

@ -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;
}

View File

@ -41,15 +41,25 @@ final class Reflection
}
if (is_callable($method)) {
return Closure::fromCallable($method)->bindTo(
TestSuite::getInstance()->test
)(...$args);
return static::bindCallable($method, $args);
}
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.
*/

View File

@ -410,6 +410,8 @@
PASS Tests\Features\HigherOrderTests
✓ it proxies calls to object
✓ it is capable doing multiple assertions
✓ it resolves expect callables correctly
✓ it can tap into the test
WARN Tests\Features\Incompleted
… incompleted
@ -579,5 +581,5 @@
✓ it is a test
✓ it uses correct parent class
Tests: 4 incompleted, 7 skipped, 363 passed
Tests: 4 incompleted, 7 skipped, 365 passed

View File

@ -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');

View File

@ -1,5 +1,7 @@
<?php
use PHPUnit\Framework\TestCase;
beforeEach()->assertTrue(true);
it('proxies calls to object')->assertTrue(true);
@ -8,4 +10,21 @@ it('is capable doing multiple assertions')
->assertTrue(true)
->assertFalse(false);
it('resolves expect callables correctly')
->expect(function () { return 'foo'; })
->toBeString()
->toBe('foo')
->and('bar')
->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();
afterEach()->assertTrue(true);

View File

@ -1,5 +1,9 @@
<?php
beforeEach(function () {
$this->shouldSkip = true;
});
it('do not skips')
->skip(false)
->assertTrue(true);
@ -31,3 +35,12 @@ it('skips with condition and message')
it('skips when skip after assertion')
->assertTrue(true)
->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();