Compare commits

...

18 Commits

Author SHA1 Message Date
328427bfdb docs: update changelog 2021-07-21 12:59:45 +01:00
51f556799c Merge pull request #352 from pestphp/higher-order-callable-datasets
Higher order callable datasets
2021-07-21 12:04:35 +01:00
4c8c42cd20 Refactor 2021-07-21 08:47:55 +01:00
09682dd393 Alters test to prove order doesn't matter 2021-07-21 08:16:54 +01:00
82c18d3848 Type fixes 2021-07-21 07:58:05 +01:00
3bdba9210d Adds another test 2021-07-21 07:44:05 +01:00
371620d161 Adds support for receiving datasets in higher order tests 2021-07-21 07:40:19 +01:00
47ceb2419b Update README.md 2021-07-16 22:12:31 +01:00
771b5b2e53 Adds Spatie 2021-07-16 22:12:19 +01:00
027e69e48f Merge pull request #349 from pestphp/teamcity-assertion-fix
Fixes the assertion output showing as 0 in the TeamCity logger
2021-07-16 14:58:30 +01:00
33e01e3805 Merge pull request #348 from jdanino/stubfix
Fix the unit test stub
2021-07-16 14:26:01 +01:00
13781dcd14 Fixes the assertion output showing as 0 in the TeamCity logger. 2021-07-16 13:17:33 +01:00
164bad437a Fixes the assertion output showing as 0 in the TeamCity logger. 2021-07-16 13:10:30 +01:00
7ddcc03ad9 Update stubs/Unit.php
Co-authored-by: Owen Voke <development@voke.dev>
2021-07-16 13:00:32 +01:00
bbe4445257 Fix the unit test stub 2021-07-16 12:40:57 +02:00
d90ddf889c docs: update changelog 2021-07-12 11:54:40 +01:00
5907164749 Merge pull request #341 from pestphp/hot-hoe
Adds support for Higher Order Expectations in Higher Order Tests
2021-07-12 11:52:04 +01:00
2a8de0565f Adds support for Higher Order Expectations in Higher Order Tests 2021-07-09 16:50:15 +01:00
14 changed files with 126 additions and 31 deletions

View File

@ -4,6 +4,20 @@ 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.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) ## [v1.9.1 (2021-07-11)](https://github.com/pestphp/pest/compare/v1.9.0...v1.9.1)
### Fixed ### 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)) - Callable `expect` values in higher order tests failing if the value was an existing method name ([#334](https://github.com/pestphp/pest/pull/344))

View File

@ -21,8 +21,9 @@ We would like to extend our thanks to the following sponsors for funding Pest de
### Premium Sponsors ### Premium Sponsors
- **[Scout APM](https://scoutapm.com)**
- **[Akaunting](https://akaunting.com)** - **[Akaunting](https://akaunting.com)**
- **[Scout APM](https://scoutapm.com)**
- **[Meema](https://meema.io/)** - **[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)**. 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)**.

View File

@ -8,9 +8,11 @@ use function getmypid;
use Pest\Concerns\Testable; use Pest\Concerns\Testable;
use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test; use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult; use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestSuite; use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning; use PHPUnit\Framework\Warning;
use PHPUnit\Runner\PhptTestCase;
use PHPUnit\TextUI\DefaultResultPrinter; use PHPUnit\TextUI\DefaultResultPrinter;
use function round; use function round;
use function str_replace; use function str_replace;
@ -137,6 +139,12 @@ final class TeamCity extends DefaultResultPrinter
return; return;
} }
if ($test instanceof TestCase) {
$this->numAssertions += $test->getNumAssertions();
} elseif ($test instanceof PhptTestCase) {
$this->numAssertions++;
}
$this->printEvent('testFinished', [ $this->printEvent('testFinished', [
self::NAME => $test->getName(), self::NAME => $test->getName(),
self::DURATION => self::toMilliseconds($time), self::DURATION => self::toMilliseconds($time),

View File

@ -155,12 +155,30 @@ final class TestCall
return $this; 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. * Saves the calls to be used on the target.
* *
* @param array<int, mixed> $arguments * @param array<int, mixed> $arguments
*/ */
public function __call(string $name, array $arguments): self 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 $this->testCaseFactory
->chains ->chains
@ -171,7 +189,9 @@ final class TestCall
if ($this->testCaseFactory->description !== null) { if ($this->testCaseFactory->description !== null) {
$this->testCaseFactory->description .= ' → '; $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; return $this;

View File

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

View File

@ -35,7 +35,7 @@ final class HigherOrderCallables
*/ */
public function expect($value) public function expect($value)
{ {
return new Expectation($value instanceof Closure ? Reflection::bindCallable($value) : $value); return new Expectation($value instanceof Closure ? Reflection::bindCallableWithData($value) : $value);
} }
/** /**
@ -59,7 +59,7 @@ final class HigherOrderCallables
*/ */
public function tap(callable $callable) public function tap(callable $callable)
{ {
Reflection::bindCallable($callable); Reflection::bindCallableWithData($callable);
return $this->target; return $this->target;
} }

View File

@ -34,18 +34,18 @@ final class HigherOrderMessage
public $line; public $line;
/** /**
* The method name. * The method or property name to access.
* *
* @readonly * @readonly
* *
* @var string * @var string
*/ */
public $methodName; public $name;
/** /**
* The arguments. * The arguments.
* *
* @var array<int, mixed> * @var array<int, mixed>|null
* *
* @readonly * @readonly
*/ */
@ -61,13 +61,13 @@ final class HigherOrderMessage
/** /**
* Creates a new higher order message. * 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->filename = $filename;
$this->line = $line; $this->line = $line;
$this->methodName = $methodName; $this->name = $methodName;
$this->arguments = $arguments; $this->arguments = $arguments;
} }
@ -85,21 +85,23 @@ final class HigherOrderMessage
if ($this->hasHigherOrderCallable()) { if ($this->hasHigherOrderCallable()) {
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
return (new HigherOrderCallables($target))->{$this->methodName}(...$this->arguments); return (new HigherOrderCallables($target))->{$this->name}(...$this->arguments);
} }
try { 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) { } catch (Throwable $throwable) {
Reflection::setPropertyValue($throwable, 'file', $this->filename); Reflection::setPropertyValue($throwable, 'file', $this->filename);
Reflection::setPropertyValue($throwable, 'line', $this->line); 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 */ /** @var ReflectionClass $reflection */
$reflection = new ReflectionClass($target); $reflection = new ReflectionClass($target);
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
$reflection = $reflection->getParentClass() ?: $reflection; $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; throw $throwable;
@ -125,7 +127,7 @@ final class HigherOrderMessage
*/ */
private function hasHigherOrderCallable() 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 private static function getUndefinedMethodMessage(object $target, string $methodName): string

View File

@ -17,21 +17,21 @@ final class HigherOrderMessageCollection
/** /**
* Adds a new higher order message to the collection. * 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. * 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);
} }
/** /**

View File

@ -60,6 +60,21 @@ final class Reflection
return Closure::fromCallable($callable)->bindTo(TestSuite::getInstance()->test)(...$args); 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. * 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); $reflectionProperty->setAccessible(true);
return $reflectionProperty->getValue($object); 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->setAccessible(true);
$reflectionProperty->setValue($object, $value); $reflectionProperty->setValue($object, $value);
} }

View File

@ -1,5 +1,5 @@
<?php <?php
test('{name}', function () { test('{name}', function () {
assertTrue(true); expect(true)->toBeTrue();
}); });

View File

@ -67,6 +67,13 @@ it('can handle nested method calls', function () {
->books()->each->toBeArray(); ->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 class HasMethods
{ {
public function name() public function name()

View File

@ -22,6 +22,13 @@ it('can handle nested methods and properties', function () {
->newInstance()->books()->toBeArray(); ->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 () { it('can start a new higher order expectation using the and syntax', function () {
expect(new HasMethodsAndProperties()) expect(new HasMethodsAndProperties())
->toBeInstanceOf(HasMethodsAndProperties::class) ->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); 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 class HasMethodsAndProperties
{ {
public $name = 'Has Methods and Properties'; public $name = 'Has Methods and Properties';

View File

@ -64,6 +64,11 @@ it('works with nested properties', function () {
->posts->toBeArray()->toHaveCount(2); ->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 class HasProperties
{ {
public $name = 'foo'; public $name = 'foo';

View File

@ -27,4 +27,20 @@ it('can tap into the test')
->toBe('foo') ->toBe('foo')
->and('hello world')->toBeString(); ->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); afterEach()->assertTrue(true);