Compare commits

...

105 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
11ebe014fb docs: update changelog 2021-06-24 21:44:57 +01:00
2d13c6e219 Merge pull request #330 from pestphp/higher-order-expectation-and
Allows `and` in Higher Order Expectations.
2021-06-24 21:42:22 +01:00
fbcb492c79 Simplification 2021-06-24 21:40:24 +01:00
4f67eff619 Start of work 2021-06-24 21:38:02 +01:00
3c2c767e09 Updates snapshot 2021-06-24 21:35:16 +01:00
ff527baa1d Allows and in Higher Order Expectations. 2021-06-24 21:31:12 +01:00
621718d4b1 Merge pull request #329 from bigint/master
Update phpunit/phpunit to 9.5.6
2021-06-23 10:28:49 +01:00
59adc57344 Update phpunit/phpunit to 9.5.6 2021-06-23 14:55:09 +05:30
1680613e12 docs: update changelog 2021-06-19 14:42:29 +01:00
f6d3ce41bc chore: updates funding 2021-06-18 22:17:44 +01:00
d16a48bf0f chore: cs 2021-06-18 22:16:54 +01:00
9459ce4030 Merge pull request #324 from pestphp/nested-higher-order-expectations
Adds Nested Higher Order Expectations
2021-06-18 22:12:17 +01:00
c773d1cd57 chore: type checks scripts folder 2021-06-18 22:09:56 +01:00
22a1aac84a Fixes types 2021-06-18 22:08:47 +01:00
3a20696da4 CS 2021-06-18 22:03:51 +01:00
99bcf98617 Rebuilds snapshots 2021-06-18 22:03:01 +01:00
09d9bae988 Merge branch 'master' into nested-higher-order-expectations
# Conflicts:
#	tests/.snapshots/success.txt
2021-06-18 22:01:30 +01:00
27de6106ab Adds type hinting 2021-06-18 22:01:16 +01:00
aeded0a356 refacto: coding style updates 2021-06-18 22:01:14 +01:00
afef1d56e8 Merge pull request #323 from pestphp/non-callable-sequence
Sequenced expectations can now be passed as non-callable values
2021-06-18 21:53:04 +01:00
4b55de27f1 Adds generics 2021-06-18 21:52:24 +01:00
3d7b6426a1 Updates snapshots 2021-06-18 21:49:38 +01:00
1d415eb7fb Merge branch 'master' into nested-higher-order-expectations
# Conflicts:
#	tests/.snapshots/success.txt
2021-06-18 21:49:05 +01:00
8a384a6d65 Merge fixes 2021-06-18 21:46:57 +01:00
7c4dd2f2e7 Merge branch 'master' into non-callable-sequence
# Conflicts:
#	tests/.snapshots/success.txt
2021-06-18 21:45:02 +01:00
b6f0496c3c chore: updates funding 2021-06-18 12:39:05 +01:00
9b34650e72 Adds changelog for 1.6.0 2021-06-18 12:36:43 +01:00
db9f1254b5 Tags Pest 1.6 2021-06-18 12:30:57 +01:00
b0e2ce6896 Merge pull request #325 from dansysanalyst/feat/json
Method json() to parse JSON strings
2021-06-18 12:28:45 +01:00
3afdedbd3f Snapshots 2021-06-18 13:11:37 +02:00
4bf69b97bd feat(json): updates docs 2021-06-18 12:06:41 +01:00
ecb37fce91 Refactoring to use toBeJson()
Refactoring according to @lukeraymonddowning suggestion
2021-06-18 13:04:31 +02:00
c1b27579ca Method json() to parse JSON strings
- Parse a JSON string into array
- Test
2021-06-18 12:43:17 +02:00
d96a2485b6 Updates snapshot 2021-06-17 00:18:12 +01:00
9e9d1cc8cc Merge branch 'master' into nested-higher-order-expectations
# Conflicts:
#	src/Expectation.php
#	tests/.snapshots/success.txt
2021-06-17 00:17:25 +01:00
579bb1b90c Updates DocBlock type hinting 2021-06-17 00:15:42 +01:00
5116b4341e Merge pull request #322 from pestphp/to-have-key
Reimplements the new `toHaveArray` features.
2021-06-16 23:12:43 +01:00
7ff64540a6 Adds nested Higher Order Expectations. 2021-06-16 20:48:23 +01:00
241d4cf94c Reimplements non-callable sequence values. 2021-06-16 20:34:17 +01:00
a5ce2dc1a1 Reimplements the new toHaveArray features. 2021-06-16 20:27:36 +01:00
729638a3bb release: v1.5.0 2021-06-15 23:06:43 +01:00
82768382da chore: bumps dependencies 2021-06-15 23:06:22 +01:00
3cb52447bb chore: adjusts type checker 2021-06-15 23:06:06 +01:00
8d670b4ed7 refactor: renames traits 2021-06-15 23:05:57 +01:00
624f6e0acc Merge pull request #320 from owenvoke/feature/expectations
feat: move Expectations API out of external plugin
2021-06-15 22:38:44 +01:00
c07cd8c252 feat: move Expectations API out of external plugin 2021-06-15 17:10:21 +01:00
a10b29a8da Merge pull request #318 from owenvoke/feature/coverage
feat: move Coverage out of external plugin
2021-06-15 14:34:49 +01:00
ea696f819e feat: move Coverage out of external plugin 2021-06-15 14:26:57 +01:00
e9b564a50c Merge pull request #317 from owenvoke/feature/init
feat: move init command out of external plugin
2021-06-15 13:17:59 +01:00
fa16775ee2 fix: allow copying the phpunit.xml 2021-06-15 09:37:58 +01:00
55449c956a feat: move init command out of external plugin 2021-06-15 09:29:33 +01:00
e4b4e55dcd Merge pull request #314 from ordago/typos
fixes typos
2021-06-13 14:35:48 +01:00
1c57de7e36 fixes typos 2021-06-13 11:45:18 +01:00
dd2921fd26 tests: fixes snapshots 2021-06-10 19:26:42 +01:00
977dbb5bcb docs: updates changelog 2021-06-10 19:21:02 +01:00
49de462250 Adds support for incompleted tests 2021-06-10 19:20:16 +01:00
95e8add29b Merge pull request #303 from def-studio/matrix-datasets
Matrix datasets
2021-06-10 18:43:31 +01:00
b682fe631d chore: bumps expectation plugin 2021-06-10 18:40:38 +01:00
d32a648af5 updated test snapshots after merge from master 2021-06-10 09:03:32 +02:00
50e9978dc3 Merge branch 'master' into matrix-datasets
# Conflicts:
#	tests/.snapshots/success.txt
2021-06-10 09:01:09 +02:00
17d407a26a Updates changelog 2021-06-07 15:27:33 +01:00
cdc3bd3f45 Merge pull request #308 from titouanmathis/fix/test-key-separator
Fix/test key separator
2021-06-07 15:22:05 +01:00
95b4192c0d Fix visual success test 2021-06-07 10:40:11 +02:00
4c911cd0eb docs: update changelog 2021-06-07 08:59:27 +01:00
7408999b0e Merge branch 'master' into matrix-datasets 2021-06-06 23:59:54 +02:00
bb13bdaa80 docs: updates changelog 2021-06-06 01:07:27 +01:00
10e7cbe006 Merge pull request #310 from bigint/master
Update phpunit/phpunit to 9.5.5
2021-06-05 16:29:02 +01:00
0ad232e9de Update phpunit/phpunit to 9.5.5 2021-06-05 20:47:57 +05:30
574cd11a40 Add tests 2021-06-04 02:08:12 +02:00
c04d6d946d Fix a bug where plugins could not be used in a path containing an @
Fix #307
2021-06-03 14:15:20 +02:00
36c2a985a6 Merge branch 'pestphp:master' into matrix-datasets 2021-06-03 12:19:15 +02:00
91eff755fd Merge pull request #306 from owenvoke/feature/lock-plugins
chore(deps): update minimum plugin versions
2021-06-02 12:43:05 +01:00
c05d287fcc chore(deps): update minimum plugin versions 2021-06-02 09:24:07 +01:00
ea0be9e7a4 Refactored Datasets::resolve() to make it more readable 2021-05-27 08:46:38 +02:00
838ac273ab writed tests with multiple datasets
Took 1 hour 6 minutes
2021-05-25 23:56:46 +02:00
296e1c37e8 updates snapshots
Took 7 minutes
2021-05-24 23:43:53 +02:00
3117f11fae phpstan fixes
Took 3 minutes
2021-05-24 23:36:51 +02:00
294c41f0dc lint fixes
Took 3 minutes
2021-05-24 23:33:43 +02:00
60afbb2c20 adds new test to check dataset matrix generation
Took 39 seconds
2021-05-24 23:30:50 +02:00
19a45c856e updates Dataset::resolve to generate a matrix of values from multiple datasets
Took 42 seconds
2021-05-24 23:30:11 +02:00
3b784060b8 updated TestCaseFactory.php to store multiple datasets
Took 44 seconds
2021-05-24 23:29:29 +02:00
dd5a11a61f updated TestCall.php to store multiple datasets
Took 1 hour 59 minutes
2021-05-24 23:28:46 +02:00
101 changed files with 4072 additions and 82 deletions

2
.gitattributes vendored
View File

@ -8,7 +8,7 @@
.gitattributes export-ignore
.gitignore export-ignore
phpstan.neon export-ignore
phpunit.xml export-ignore
/phpunit.xml export-ignore
CHANGELOG.md export-ignore
CONTRIBUTING.md export-ignore
README.md export-ignore

3
.github/FUNDING.yml vendored
View File

@ -1,5 +1,4 @@
# These are supported funding model platforms
github: [nunomaduro,owenvoke,olivernybroe,octoper]
github: [nunomaduro,owenvoke,olivernybroe,octoper,lukeraymonddowning]
patreon: nunomaduro
custom: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L

View File

@ -4,6 +4,53 @@ 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))
## [v1.7.0 (2021-06-19)](https://github.com/pestphp/pest/compare/v1.6.0...v1.7.0)
### 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 nested Higher Order Expectations ([#324](https://github.com/pestphp/pest/pull/324))
## [v1.6.0 (2021-06-18)](https://github.com/pestphp/pest/compare/v1.5.0...v1.6.0)
### Added
- Adds a new `json` expectation method to improve testing with JSON strings ([#325](https://github.com/pestphp/pest/pull/325))
- Adds dot notation support to the `toHaveKey` and `toHaveKeys` expectations ([#322](https://github.com/pestphp/pest/pull/322))
## [v1.5.0 (2021-06-15)](https://github.com/pestphp/pest/compare/v1.4.0...v1.5.0)
### Changed
- Moves plugins from the `require` section to the core itself ([#317](https://github.com/pestphp/pest/pull/317)), ([#318](https://github.com/pestphp/pest/pull/318)), ([#320](https://github.com/pestphp/pest/pull/320))
## [v1.4.0 (2021-06-10)](https://github.com/pestphp/pest/compare/v1.3.2...v1.4.0)
### Added
- Support for multiple datasets (Matrix) on the `with` method ([#303](https://github.com/pestphp/pest/pull/303))
- Support for incompleted tests ([49de462](https://github.com/pestphp/pest/commit/49de462250cf9f65f09e13eaf6dcc0e06865b930))
## [v1.3.2 (2021-06-07)](https://github.com/pestphp/pest/compare/v1.3.1...v1.3.2)
### Fixed
- Test cases with the @ symbol in the directory fail ([#308](https://github.com/pestphp/pest/pull/308))
## [v1.3.1 (2021-06-06)](https://github.com/pestphp/pest/compare/v1.3.0...v1.3.1)
### Added
- Added for PHPUnit 9.5.5 ([#310](https://github.com/pestphp/pest/pull/310))
### Changed
- Lock minimum Pest plugin versions ([#306](https://github.com/pestphp/pest/pull/306))
## [v1.3.0 (2021-05-23)](https://github.com/pestphp/pest/compare/v1.2.1...v1.3.0)
### Added
- Named datasets no longer show the arguments ([#302](https://github.com/pestphp/pest/pull/302))
@ -151,7 +198,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Expectation API (TODO)
- PHPUnit 9.3 and PHP 8 support ([#128](https://github.com/pestphp/pest/pull/128))
- Fowards `$this` calls to globals ([#169](https://github.com/pestphp/pest/pull/169))
- Forwards `$this` calls to globals ([#169](https://github.com/pestphp/pest/pull/169))
### Fixed
- don't decorate output if --colors=never is set ([36b879f](https://github.com/pestphp/pest/commit/36b879f97d7b187c87a94eb60af5b7d3b7253d56))
@ -166,7 +213,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `depends` phpunit feature ([#103](https://github.com/pestphp/pest/pull/103))
### Fixes
- datasets name conflit ([#101](https://github.com/pestphp/pest/pull/101))
- datasets name conflict ([#101](https://github.com/pestphp/pest/pull/101))
## [v0.2.1 (2020-06-17)](https://github.com/pestphp/pest/compare/v0.2.0...v0.2.1)
### Fixes

View File

@ -18,12 +18,9 @@
],
"require": {
"php": "^7.3 || ^8.0",
"nunomaduro/collision": "^5.0",
"pestphp/pest-plugin": "^1.0",
"pestphp/pest-plugin-coverage": "^1.0",
"pestphp/pest-plugin-expectations": "^1.0",
"pestphp/pest-plugin-init": "^1.0",
"phpunit/phpunit": ">= 9.3.7 <= 9.5.4"
"nunomaduro/collision": "^5.4.0",
"pestphp/pest-plugin": "^1.0.0",
"phpunit/phpunit": ">= 9.3.7 <= 9.5.6"
},
"autoload": {
"psr-4": {
@ -43,9 +40,9 @@
]
},
"require-dev": {
"illuminate/console": "^8.32.1",
"illuminate/support": "^8.32.1",
"laravel/dusk": "^6.13.0",
"illuminate/console": "^8.47.0",
"illuminate/support": "^8.47.0",
"laravel/dusk": "^6.15.0",
"pestphp/pest-dev-tools": "dev-master"
},
"minimum-stability": "dev",
@ -77,6 +74,8 @@
},
"pest": {
"plugins": [
"Pest\\Plugins\\Coverage",
"Pest\\Plugins\\Init",
"Pest\\Plugins\\Version"
]
},

View File

@ -7,6 +7,7 @@ parameters:
level: max
paths:
- src
- scripts
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: false
@ -24,7 +25,7 @@ parameters:
message: '#Call to an undefined method PHPUnit\\Framework\\Test::getName\(\)#'
path: src/Logging
-
message: '#invalid typehint type Pest\\Concerns\\TestCase#'
message: '#invalid typehint type Pest\\Concerns\\Testable#'
path: src/Logging
-
message: '#is not subtype of native type PHPUnit\\Framework\\Test#'

View File

@ -15,6 +15,7 @@ $globalsFilePath = implode(DIRECTORY_SEPARATOR, [
$compiledFilePath = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__), 'compiled', 'globals.php']);
/* @phpstan-ignore-next-line */
@unlink($compiledFilePath);
$replace = function ($contents, $string, $by) {

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Pest\Concerns;
use Pest\Expectation;
/**
* @internal
*/
trait Expectable
{
/**
* @template TValue
*
* Creates a new expectation.
*
* @param TValue $value
*
* @return Expectation<TValue>
*/
public function expect($value): Expectation
{
return new Expectation($value);
}
}

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Pest\Concerns;
use BadMethodCallException;
use Closure;
/**
* @internal
*/
trait Extendable
{
/**
* @var array<string, Closure>
*/
private static $extends = [];
/**
* Register a custom extend.
*/
public static function extend(string $name, Closure $extend): void
{
static::$extends[$name] = $extend;
}
/**
* Checks if extend is registered.
*/
public static function hasExtend(string $name): bool
{
return array_key_exists($name, static::$extends);
}
/**
* Dynamically handle calls to the class.
*
* @param array<int, mixed> $parameters
*
* @return mixed
*/
public function __call(string $method, array $parameters)
{
if (!static::hasExtend($method)) {
throw new BadMethodCallException("$method is not a callable method name.");
}
/** @var Closure $extend */
$extend = static::$extends[$method]->bindTo($this, static::class);
return $extend(...$parameters);
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Pest\Concerns;
/**
* @internal
*/
trait RetrievesValues
{
/**
* @template TRetrievableValue
*
* Safely retrieve the value at the given key from an object or array.
*
* @param array<string, TRetrievableValue>|object $value
* @param TRetrievableValue|null $default
*
* @return TRetrievableValue|null
*/
private function retrieve(string $key, $value, $default = null)
{
if (is_array($value)) {
return $value[$key] ?? $default;
}
// @phpstan-ignore-next-line
return $value->$key ?? $default;
}
}

View File

@ -17,7 +17,7 @@ use Throwable;
*
* @internal
*/
trait TestCase
trait Testable
{
/**
* The test case description. Contains the first

View File

@ -51,36 +51,34 @@ final class Datasets
/**
* Resolves the current dataset to an array value.
*
* @param Traversable<int|string, mixed>|Closure|iterable<int|string, mixed>|string|null $data
* @param array<Closure|iterable<int|string, mixed>|string> $datasets
*
* @return array<string, mixed>
*/
public static function resolve(string $description, $data): array
public static function resolve(string $description, array $datasets): array
{
/* @phpstan-ignore-next-line */
if (is_null($data) || empty($data)) {
if (empty($datasets)) {
return [$description => []];
}
if (is_string($data)) {
$data = self::get($data);
}
$datasets = self::processDatasets($datasets);
if (is_callable($data)) {
$data = call_user_func($data);
}
if ($data instanceof Traversable) {
$data = iterator_to_array($data);
}
$datasetCombinations = self::getDataSetsCombinations($datasets);
$dataSetDescriptions = [];
$dataSetValues = [];
foreach ($data as $key => $values) {
$values = is_array($values) ? $values : [$values];
foreach ($datasetCombinations as $datasetCombination) {
$partialDescriptions = [];
$values = [];
$dataSetDescriptions[] = $description . self::getDataSetDescription($key, $values);
foreach ($datasetCombination as $dataset_data) {
$partialDescriptions[] = $dataset_data['label'];
$values = array_merge($values, $dataset_data['values']);
}
$dataSetDescriptions[] = $description . ' with ' . implode(' / ', $partialDescriptions);
$dataSetValues[] = $values;
}
@ -103,6 +101,65 @@ final class Datasets
return $namedData;
}
/**
* @param array<Closure|iterable<int|string, mixed>|string> $datasets
*
* @return array<array>
*/
private static function processDatasets(array $datasets): array
{
$processedDatasets = [];
foreach ($datasets as $index => $data) {
$processedDataset = [];
if (is_string($data)) {
$datasets[$index] = self::get($data);
}
if (is_callable($datasets[$index])) {
$datasets[$index] = call_user_func($datasets[$index]);
}
if ($datasets[$index] instanceof Traversable) {
$datasets[$index] = iterator_to_array($datasets[$index]);
}
foreach ($datasets[$index] as $key => $values) {
$values = is_array($values) ? $values : [$values];
$processedDataset[] = [
'label' => self::getDataSetDescription($key, $values),
'values' => $values,
];
}
$processedDatasets[] = $processedDataset;
}
return $processedDatasets;
}
/**
* @param array<array> $combinations
*
* @return array<array>
*/
private static function getDataSetsCombinations(array $combinations): array
{
$result = [[]];
foreach ($combinations as $index => $values) {
$tmp = [];
foreach ($result as $resultItem) {
foreach ($values as $value) {
$tmp[] = array_merge($resultItem, [$index => $value]);
}
}
$result = $tmp;
}
return $result;
}
/**
* @param int|string $key
* @param array<int, mixed> $data
@ -112,9 +169,9 @@ final class Datasets
$exporter = new Exporter();
if (is_int($key)) {
return \sprintf(' with (%s)', $exporter->shortenedRecursiveExport($data));
return \sprintf('(%s)', $exporter->shortenedRecursiveExport($data));
}
return \sprintf(' with data set "%s"', $key);
return \sprintf('data set "%s"', $key);
}
}

77
src/Each.php Normal file
View File

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Pest;
/**
* @internal
*
* @mixin Expectation
*/
final class Each
{
/**
* @var Expectation
*/
private $original;
/**
* @var bool
*/
private $opposite = false;
/**
* Creates an expectation on each item of the iterable "value".
*/
public function __construct(Expectation $original)
{
$this->original = $original;
}
/**
* Creates a new expectation.
*
* @param mixed $value
*/
public function and($value): Expectation
{
return $this->original->and($value);
}
/**
* Creates the opposite expectation for the value.
*/
public function not(): Each
{
$this->opposite = true;
return $this;
}
/**
* Dynamically calls methods on the class with the given arguments on each item.
*
* @param array<int|string, mixed> $arguments
*/
public function __call(string $name, array $arguments): Each
{
foreach ($this->original->value as $item) {
/* @phpstan-ignore-next-line */
$this->opposite ? expect($item)->not()->$name(...$arguments) : expect($item)->$name(...$arguments);
}
$this->opposite = false;
return $this;
}
/**
* Dynamically calls methods on the class without any arguments on each item.
*/
public function __get(string $name): Each
{
/* @phpstan-ignore-next-line */
return $this->$name();
}
}

765
src/Expectation.php Normal file
View File

@ -0,0 +1,765 @@
<?php
declare(strict_types=1);
namespace Pest;
use BadMethodCallException;
use Pest\Concerns\Extendable;
use Pest\Concerns\RetrievesValues;
use Pest\Support\Arr;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Exporter\Exporter;
/**
* @internal
*
* @template TValue
*
* @property Expectation $not Creates the opposite expectation.
* @property Each $each Creates an expectation on each element on the traversable value.
*/
final class Expectation
{
use Extendable {
__call as __extendsCall;
}
use RetrievesValues;
/**
* The expectation value.
*
* @readonly
*
* @var mixed
*/
public $value;
/**
* The exporter instance, if any.
*
* @readonly
*
* @var Exporter|null
*/
private $exporter;
/**
* Creates a new expectation.
*
* @param TValue $value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Creates a new expectation.
*
* @param TValue $value
*
* @return Expectation<TValue>
*/
public function and($value): Expectation
{
return new self($value);
}
/**
* Creates a new expectation with the decoded JSON value.
*/
public function json(): Expectation
{
return $this->toBeJson()->and(json_decode($this->value, true));
}
/**
* Dump the expectation value and end the script.
*
* @param mixed $arguments
*
* @return never
*/
public function dd(...$arguments): void
{
if (function_exists('dd')) {
dd($this->value, ...$arguments);
}
var_dump($this->value);
exit(1);
}
/**
* Send the expectation value to Ray along with all given arguments.
*
* @param mixed $arguments
*/
public function ray(...$arguments): self
{
if (function_exists('ray')) {
// @phpstan-ignore-next-line
ray($this->value, ...$arguments);
}
return $this;
}
/**
* Creates the opposite expectation for the value.
*/
public function not(): OppositeExpectation
{
return new OppositeExpectation($this);
}
/**
* Creates an expectation on each item of the iterable "value".
*/
public function each(callable $callback = null): Each
{
if (!is_iterable($this->value)) {
throw new BadMethodCallException('Expectation value is not iterable.');
}
if (is_callable($callback)) {
foreach ($this->value as $item) {
$callback(new self($item));
}
}
return new Each($this);
}
/**
* Allows you to specify a sequential set of expectations for each item in a iterable "value".
*
* @template TSequenceValue
*
* @param callable(self, self): void|TSequenceValue ...$callbacks
*/
public function sequence(...$callbacks): Expectation
{
if (!is_iterable($this->value)) {
throw new BadMethodCallException('Expectation value is not iterable.');
}
$value = is_array($this->value) ? $this->value : iterator_to_array($this->value);
$keys = array_keys($value);
$values = array_values($value);
$index = 0;
while (count($callbacks) < count($values)) {
$callbacks[] = $callbacks[$index];
$index = $index < count($values) - 1 ? $index + 1 : 0;
}
foreach ($values as $key => $item) {
if (is_callable($callbacks[$key])) {
call_user_func($callbacks[$key], new self($item), new self($keys[$key]));
continue;
}
(new self($item))->toEqual($callbacks[$key]);
}
return $this;
}
/**
* Asserts that two variables have the same type and
* value. Used on objects, it asserts that two
* variables reference the same object.
*
* @param mixed $expected
*/
public function toBe($expected): Expectation
{
Assert::assertSame($expected, $this->value);
return $this;
}
/**
* Asserts that the value is empty.
*/
public function toBeEmpty(): Expectation
{
Assert::assertEmpty($this->value);
return $this;
}
/**
* Asserts that the value is true.
*/
public function toBeTrue(): Expectation
{
Assert::assertTrue($this->value);
return $this;
}
/**
* Asserts that the value is false.
*/
public function toBeFalse(): Expectation
{
Assert::assertFalse($this->value);
return $this;
}
/**
* Asserts that the value is greater than $expected.
*
* @param int|float $expected
*/
public function toBeGreaterThan($expected): Expectation
{
Assert::assertGreaterThan($expected, $this->value);
return $this;
}
/**
* Asserts that the value is greater than or equal to $expected.
*
* @param int|float $expected
*/
public function toBeGreaterThanOrEqual($expected): Expectation
{
Assert::assertGreaterThanOrEqual($expected, $this->value);
return $this;
}
/**
* Asserts that the value is less than or equal to $expected.
*
* @param int|float $expected
*/
public function toBeLessThan($expected): Expectation
{
Assert::assertLessThan($expected, $this->value);
return $this;
}
/**
* Asserts that the value is less than $expected.
*
* @param int|float $expected
*/
public function toBeLessThanOrEqual($expected): Expectation
{
Assert::assertLessThanOrEqual($expected, $this->value);
return $this;
}
/**
* Asserts that $needle is an element of the value.
*
* @param mixed $needle
*/
public function toContain($needle): Expectation
{
if (is_string($this->value)) {
Assert::assertStringContainsString($needle, $this->value);
} else {
Assert::assertContains($needle, $this->value);
}
return $this;
}
/**
* Asserts that the value starts with $expected.
*/
public function toStartWith(string $expected): Expectation
{
Assert::assertStringStartsWith($expected, $this->value);
return $this;
}
/**
* Asserts that the value ends with $expected.
*/
public function toEndWith(string $expected): Expectation
{
Assert::assertStringEndsWith($expected, $this->value);
return $this;
}
/**
* Asserts that $count matches the number of elements of the value.
*/
public function toHaveCount(int $count): Expectation
{
Assert::assertCount($count, $this->value);
return $this;
}
/**
* Asserts that the value contains the property $name.
*
* @param mixed $value
*/
public function toHaveProperty(string $name, $value = null): Expectation
{
$this->toBeObject();
Assert::assertTrue(property_exists($this->value, $name));
if (func_num_args() > 1) {
/* @phpstan-ignore-next-line */
Assert::assertEquals($value, $this->value->{$name});
}
return $this;
}
/**
* Asserts that two variables have the same value.
*
* @param mixed $expected
*/
public function toEqual($expected): Expectation
{
Assert::assertEquals($expected, $this->value);
return $this;
}
/**
* Asserts that two variables have the same value.
* The contents of $expected and the $this->value are
* canonicalized before they are compared. For instance, when the two
* variables $expected and $this->value are arrays, then these arrays
* are sorted before they are compared. When $expected and $this->value
* are objects, each object is converted to an array containing all
* private, protected and public attributes.
*
* @param mixed $expected
*/
public function toEqualCanonicalizing($expected): Expectation
{
Assert::assertEqualsCanonicalizing($expected, $this->value);
return $this;
}
/**
* Asserts that the absolute difference between the value and $expected
* is lower than $delta.
*
* @param mixed $expected
*/
public function toEqualWithDelta($expected, float $delta): Expectation
{
Assert::assertEqualsWithDelta($expected, $this->value, $delta);
return $this;
}
/**
* Asserts that the value is infinite.
*/
public function toBeInfinite(): Expectation
{
Assert::assertInfinite($this->value);
return $this;
}
/**
* Asserts that the value is an instance of $class.
*
* @param string $class
*/
public function toBeInstanceOf($class): Expectation
{
/* @phpstan-ignore-next-line */
Assert::assertInstanceOf($class, $this->value);
return $this;
}
/**
* Asserts that the value is an array.
*/
public function toBeArray(): Expectation
{
Assert::assertIsArray($this->value);
return $this;
}
/**
* Asserts that the value is of type bool.
*/
public function toBeBool(): Expectation
{
Assert::assertIsBool($this->value);
return $this;
}
/**
* Asserts that the value is of type callable.
*/
public function toBeCallable(): Expectation
{
Assert::assertIsCallable($this->value);
return $this;
}
/**
* Asserts that the value is of type float.
*/
public function toBeFloat(): Expectation
{
Assert::assertIsFloat($this->value);
return $this;
}
/**
* Asserts that the value is of type int.
*/
public function toBeInt(): Expectation
{
Assert::assertIsInt($this->value);
return $this;
}
/**
* Asserts that the value is of type iterable.
*/
public function toBeIterable(): Expectation
{
Assert::assertIsIterable($this->value);
return $this;
}
/**
* Asserts that the value is of type numeric.
*/
public function toBeNumeric(): Expectation
{
Assert::assertIsNumeric($this->value);
return $this;
}
/**
* Asserts that the value is of type object.
*/
public function toBeObject(): Expectation
{
Assert::assertIsObject($this->value);
return $this;
}
/**
* Asserts that the value is of type resource.
*/
public function toBeResource(): Expectation
{
Assert::assertIsResource($this->value);
return $this;
}
/**
* Asserts that the value is of type scalar.
*/
public function toBeScalar(): Expectation
{
Assert::assertIsScalar($this->value);
return $this;
}
/**
* Asserts that the value is of type string.
*/
public function toBeString(): Expectation
{
Assert::assertIsString($this->value);
return $this;
}
/**
* Asserts that the value is a JSON string.
*/
public function toBeJson(): Expectation
{
Assert::assertIsString($this->value);
Assert::assertJson($this->value);
return $this;
}
/**
* Asserts that the value is NAN.
*/
public function toBeNan(): Expectation
{
Assert::assertNan($this->value);
return $this;
}
/**
* Asserts that the value is null.
*/
public function toBeNull(): Expectation
{
Assert::assertNull($this->value);
return $this;
}
/**
* Asserts that the value array has the provided $key.
*
* @param string|int $key
* @param mixed $value
*/
public function toHaveKey($key, $value = null): Expectation
{
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
$array = $this->value->toArray();
} else {
$array = (array) $this->value;
}
try {
Assert::assertTrue(Arr::has($array, $key));
/* @phpstan-ignore-next-line */
} catch (ExpectationFailedException $exception) {
throw new ExpectationFailedException("Failed asserting that an array has the key '$key'", $exception->getComparisonFailure());
}
if (func_num_args() > 1) {
Assert::assertEquals($value, Arr::get($array, $key));
}
return $this;
}
/**
* Asserts that the value array has the provided $keys.
*
* @param array<int, int|string> $keys
*/
public function toHaveKeys(array $keys): Expectation
{
foreach ($keys as $key) {
$this->toHaveKey($key);
}
return $this;
}
/**
* Asserts that the value is a directory.
*/
public function toBeDirectory(): Expectation
{
Assert::assertDirectoryExists($this->value);
return $this;
}
/**
* Asserts that the value is a directory and is readable.
*/
public function toBeReadableDirectory(): Expectation
{
Assert::assertDirectoryIsReadable($this->value);
return $this;
}
/**
* Asserts that the value is a directory and is writable.
*/
public function toBeWritableDirectory(): Expectation
{
Assert::assertDirectoryIsWritable($this->value);
return $this;
}
/**
* Asserts that the value is a file.
*/
public function toBeFile(): Expectation
{
Assert::assertFileExists($this->value);
return $this;
}
/**
* Asserts that the value is a file and is readable.
*/
public function toBeReadableFile(): Expectation
{
Assert::assertFileIsReadable($this->value);
return $this;
}
/**
* Asserts that the value is a file and is writable.
*/
public function toBeWritableFile(): Expectation
{
Assert::assertFileIsWritable($this->value);
return $this;
}
/**
* Asserts that the value array matches the given array subset.
*
* @param array<int|string, mixed> $array
*/
public function toMatchArray($array): Expectation
{
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
$valueAsArray = $this->value->toArray();
} else {
$valueAsArray = (array) $this->value;
}
foreach ($array as $key => $value) {
Assert::assertArrayHasKey($key, $valueAsArray);
Assert::assertEquals(
$value,
$valueAsArray[$key],
sprintf(
'Failed asserting that an array has a key %s with the value %s.',
$this->export($key),
$this->export($valueAsArray[$key]),
),
);
}
return $this;
}
/**
* Asserts that the value object matches a subset
* of the properties of an given object.
*
* @param array<string, mixed>|object $object
*/
public function toMatchObject($object): Expectation
{
foreach ((array) $object as $property => $value) {
Assert::assertTrue(property_exists($this->value, $property));
/* @phpstan-ignore-next-line */
$propertyValue = $this->value->{$property};
Assert::assertEquals(
$value,
$propertyValue,
sprintf(
'Failed asserting that an object has a property %s with the value %s.',
$this->export($property),
$this->export($propertyValue),
),
);
}
return $this;
}
/**
* Asserts that the value matches a regular expression.
*/
public function toMatch(string $expression): Expectation
{
Assert::assertMatchesRegularExpression($expression, $this->value);
return $this;
}
/**
* Asserts that the value matches a constraint.
*/
public function toMatchConstraint(Constraint $constraint): Expectation
{
Assert::assertThat($this->value, $constraint);
return $this;
}
/**
* Exports the given value.
*
* @param mixed $value
*/
private function export($value): string
{
if ($this->exporter === null) {
$this->exporter = new Exporter();
}
return $this->exporter->export($value);
}
/**
* Dynamically handle calls to the class or
* creates a new higher order expectation.
*
* @param array<int, mixed> $parameters
*
* @return HigherOrderExpectation|mixed
*/
public function __call(string $method, array $parameters)
{
if (!static::hasExtend($method)) {
/* @phpstan-ignore-next-line */
return new HigherOrderExpectation($this, $this->value->$method(...$parameters));
}
return $this->__extendsCall($method, $parameters);
}
/**
* Dynamically calls methods on the class without any arguments
* or creates a new higher order expectation.
*
* @return Expectation|HigherOrderExpectation
*/
public function __get(string $name)
{
if (!method_exists($this, $name) && !static::hasExtend($name)) {
return new HigherOrderExpectation($this, $this->retrieve($name, $this->value));
}
/* @phpstan-ignore-next-line */
return $this->{$name}();
}
}

View File

@ -11,9 +11,9 @@ use Pest\Contracts\HasPrintableTestCaseName;
use Pest\Datasets;
use Pest\Exceptions\ShouldNotHappen;
use Pest\Support\HigherOrderMessageCollection;
use Pest\Support\NullClosure;
use Pest\Support\Str;
use Pest\TestSuite;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use RuntimeException;
@ -62,9 +62,9 @@ final class TestCaseFactory
/**
* Holds the dataset, if any.
*
* @var Closure|iterable<int|string, mixed>|string|null
* @var array<Closure|iterable<int|string, mixed>|string>
*/
public $dataset;
public $datasets = [];
/**
* The FQN of the test case class.
@ -79,7 +79,8 @@ final class TestCaseFactory
* @var array <int, string>
*/
public $traits = [
Concerns\TestCase::class,
Concerns\Testable::class,
Concerns\Expectable::class,
];
/**
@ -113,7 +114,11 @@ final class TestCaseFactory
{
$this->filename = $filename;
$this->description = $description;
$this->test = $closure ?? NullClosure::create();
$this->test = $closure ?? function (): void {
if (Assert::getCount() === 0) {
self::markTestIncomplete(); // @phpstan-ignore-line
}
};
$this->factoryProxies = new HigherOrderMessageCollection();
$this->proxies = new HigherOrderMessageCollection();
@ -155,7 +160,7 @@ final class TestCaseFactory
return $testCase;
};
$datasets = Datasets::resolve($this->description, $this->dataset);
$datasets = Datasets::resolve($this->description, $this->datasets);
return array_map($createTest, array_keys($datasets), $datasets);
}

View File

@ -3,15 +3,33 @@
declare(strict_types=1);
use Pest\Datasets;
use Pest\Expectation;
use Pest\PendingObjects\AfterEachCall;
use Pest\PendingObjects\BeforeEachCall;
use Pest\PendingObjects\TestCall;
use Pest\PendingObjects\UsesCall;
use Pest\Support\Backtrace;
use Pest\Support\Extendable;
use Pest\Support\HigherOrderTapProxy;
use Pest\TestSuite;
use PHPUnit\Framework\TestCase;
/**
* Creates a new expectation.
*
* @param mixed $value the Value
*
* @return Expectation|Extendable
*/
function expect($value = null)
{
if (func_num_args() === 0) {
return new Extendable(Expectation::class);
}
return new Expectation($value);
}
if (!function_exists('beforeAll')) {
/**
* Runs the given closure before all tests in the current file.

View File

@ -0,0 +1,144 @@
<?php
declare(strict_types=1);
namespace Pest;
use Pest\Concerns\Expectable;
use Pest\Concerns\RetrievesValues;
/**
* @internal
*
* @mixin Expectation
*/
final class HigherOrderExpectation
{
use Expectable;
use RetrievesValues;
/**
* @var Expectation
*/
private $original;
/**
* @var Expectation|Each
*/
private $expectation;
/**
* @var bool
*/
private $opposite = false;
/**
* @var bool
*/
private $shouldReset = false;
/**
* @var string
*/
private $name;
/**
* Creates a new higher order expectation.
*
* @param mixed $value
*/
public function __construct(Expectation $original, $value)
{
$this->original = $original;
$this->expectation = $this->expect($value);
}
/**
* Creates the opposite expectation for the value.
*/
public function not(): HigherOrderExpectation
{
$this->opposite = !$this->opposite;
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.
*
* @param array<int, mixed> $arguments
*/
public function __call(string $name, array $arguments): self
{
if (!$this->expectationHasMethod($name)) {
/* @phpstan-ignore-next-line */
return new self($this->original, $this->getValue()->$name(...$arguments));
}
return $this->performAssertion($name, $arguments);
}
/**
* Accesses properties in the value or in the expectation.
*/
public function __get(string $name): self
{
if ($name === 'not') {
return $this->not();
}
if (!$this->expectationHasMethod($name)) {
return new self($this->original, $this->retrieve($name, $this->getValue()));
}
return $this->performAssertion($name, []);
}
/**
* Determines if the original expectation has the given method name.
*/
private function expectationHasMethod(string $name): bool
{
return method_exists($this->original, $name) || $this->original::hasExtend($name);
}
/**
* Retrieve the applicable value based on the current reset condition.
*
* @return mixed
*/
private function getValue()
{
return $this->shouldReset ? $this->original->value : $this->expectation->value;
}
/**
* Performs the given assertion with the current expectation.
*
* @param array<int, mixed> $arguments
*/
private function performAssertion(string $name, array $arguments): self
{
/* @phpstan-ignore-next-line */
$this->expectation = ($this->opposite ? $this->expectation->not() : $this->expectation)->{$name}(...$arguments);
$this->opposite = false;
$this->shouldReset = true;
return $this;
}
}

View File

@ -18,7 +18,7 @@ use DOMElement;
use Exception;
use function get_class;
use function method_exists;
use Pest\Concerns\TestCase;
use Pest\Concerns\Testable;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\ExceptionWrapper;
use PHPUnit\Framework\SelfDescribing;
@ -266,7 +266,7 @@ final class JUnit extends Printer implements TestListener
/**
* A test started.
*
* @param Test|TestCase $test
* @param Test|Testable $test
*/
public function startTest(Test $test): void
{

View File

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Pest\Logging;
use function getmypid;
use Pest\Concerns\TestCase;
use Pest\Concerns\Testable;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestResult;
@ -109,7 +109,7 @@ final class TeamCity extends DefaultResultPrinter
}
/**
* @param Test|TestCase $test
* @param Test|Testable $test
*/
public function startTest(Test $test): void
{
@ -127,7 +127,7 @@ final class TeamCity extends DefaultResultPrinter
}
/**
* @param Test|TestCase $test
* @param Test|Testable $test
*/
public function endTest(Test $test, float $time): void
{
@ -144,7 +144,7 @@ final class TeamCity extends DefaultResultPrinter
}
/**
* @param Test|TestCase $test
* @param Test|Testable $test
*/
public function addError(Test $test, Throwable $t, float $time): void
{
@ -154,7 +154,7 @@ final class TeamCity extends DefaultResultPrinter
/**
* @phpstan-ignore-next-line
*
* @param Test|TestCase $test
* @param Test|Testable $test
*/
public function addWarning(Test $test, Warning $e, float $time): void
{
@ -208,6 +208,6 @@ final class TeamCity extends DefaultResultPrinter
/** @var array<string, string> $uses */
$uses = class_uses($test);
return in_array(TestCase::class, $uses, true);
return in_array(Testable::class, $uses, true);
}
}

View File

@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace Pest;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Exporter\Exporter;
/**
* @internal
*
* @mixin Expectation
*/
final class OppositeExpectation
{
/**
* @var Expectation
*/
private $original;
/**
* Creates a new opposite expectation.
*/
public function __construct(Expectation $original)
{
$this->original = $original;
}
/**
* Asserts that the value array not has the provided $keys.
*
* @param array<int, int|string> $keys
*/
public function toHaveKeys(array $keys): Expectation
{
foreach ($keys as $key) {
try {
$this->original->toHaveKey($key);
} catch (ExpectationFailedException $e) {
continue;
}
$this->throwExpectationFailedException('toHaveKey', [$key]);
}
return $this->original;
}
/**
* Handle dynamic method calls into the original expectation.
*
* @param array<int, mixed> $arguments
*/
public function __call(string $name, array $arguments): Expectation
{
try {
/* @phpstan-ignore-next-line */
$this->original->{$name}(...$arguments);
} catch (ExpectationFailedException $e) {
return $this->original;
}
// @phpstan-ignore-next-line
$this->throwExpectationFailedException($name, $arguments);
}
/**
* Handle dynamic properties gets into the original expectation.
*/
public function __get(string $name): Expectation
{
try {
/* @phpstan-ignore-next-line */
$this->original->{$name};
} catch (ExpectationFailedException $e) {
return $this->original;
}
// @phpstan-ignore-next-line
$this->throwExpectationFailedException($name);
}
/**
* Creates a new expectation failed exception with a nice readable message.
*
* @param array<int, mixed> $arguments
*/
private function throwExpectationFailedException(string $name, array $arguments = []): void
{
$exporter = new Exporter();
$toString = function ($argument) use ($exporter): string {
return $exporter->shortenedExport($argument);
};
throw new ExpectationFailedException(sprintf('Expecting %s not %s %s.', $toString($this->original->value), strtolower((string) preg_replace('/(?<!\ )[A-Z]/', ' $0', $name)), implode(' ', array_map(function ($argument) use ($toString): string { return $toString($argument); }, $arguments))));
}
}

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
{
if (class_exists($exception)) {
$this->testCaseFactory
->proxies
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]);
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]);
} else {
$exceptionMessage = $exception;
}
if (is_string($exceptionMessage)) {
$this->testCaseFactory
@ -77,11 +82,13 @@ final class TestCall
* Runs the current test multiple times with
* each item of the given `iterable`.
*
* @param \Closure|iterable<int|string, mixed>|string $data
* @param array<\Closure|iterable<int|string, mixed>|string> $data
*/
public function with($data): TestCall
public function with(...$data): TestCall
{
$this->testCaseFactory->dataset = $data;
foreach ($data as $dataset) {
$this->testCaseFactory->datasets[] = $dataset;
}
return $this;
}
@ -141,11 +148,9 @@ final class TestCall
? $conditionOrMessage
: $message;
if ($condition() !== false) {
$this->testCaseFactory
->chains
->add(Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
}
->addWhen($condition, Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
return $this;
}

View File

@ -68,7 +68,7 @@ final class UsesCall
/**
* The directories or file where the
* class or trais should be used.
* class or traits should be used.
*/
public function in(string ...$targets): void
{

View File

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

118
src/Plugins/Coverage.php Normal file
View File

@ -0,0 +1,118 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\AddsOutput;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Support\Str;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class Coverage implements AddsOutput, HandlesArguments
{
/**
* @var string
*/
private const COVERAGE_OPTION = 'coverage';
/**
* @var string
*/
private const MIN_OPTION = 'min';
/**
* Whether should show the coverage or not.
*
* @var bool
*/
public $coverage = false;
/**
* The minimum coverage.
*
* @var float
*/
public $coverageMin = 0.0;
/**
* @var OutputInterface
*/
private $output;
public function __construct(OutputInterface $output)
{
$this->output = $output;
}
public function handleArguments(array $originals): array
{
$arguments = array_merge([''], array_values(array_filter($originals, function ($original): bool {
foreach ([self::COVERAGE_OPTION, self::MIN_OPTION] as $option) {
if ($original === sprintf('--%s', $option) || Str::startsWith($original, sprintf('--%s=', $option))) {
return true;
}
}
return false;
})));
$originals = array_flip($originals);
foreach ($arguments as $argument) {
unset($originals[$argument]);
}
$originals = array_flip($originals);
$inputs = [];
$inputs[] = new InputOption(self::COVERAGE_OPTION, null, InputOption::VALUE_NONE);
$inputs[] = new InputOption(self::MIN_OPTION, null, InputOption::VALUE_REQUIRED);
$input = new ArgvInput($arguments, new InputDefinition($inputs));
if ((bool) $input->getOption(self::COVERAGE_OPTION)) {
$this->coverage = true;
$originals[] = '--coverage-php';
$originals[] = \Pest\Support\Coverage::getPath();
}
if ($input->getOption(self::MIN_OPTION) !== null) {
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION);
}
return $originals;
}
/**
* Allows to add custom output after the test suite was executed.
*/
public function addOutput(int $result): int
{
if ($result === 0 && $this->coverage) {
if (!\Pest\Support\Coverage::isAvailable()) {
$this->output->writeln(
"\n <fg=white;bg=red;options=bold> ERROR </> No code coverage driver is available.</>",
);
exit(1);
}
$coverage = \Pest\Support\Coverage::report($this->output);
$result = (int) ($coverage < $this->coverageMin);
if ($result === 1) {
$this->output->writeln(sprintf(
"\n <fg=white;bg=red;options=bold> FAIL </> Code coverage below expected:<fg=red;options=bold> %s %%</>. Minimum:<fg=white;options=bold> %s %%</>.",
number_format($coverage, 1),
number_format($this->coverageMin, 1)
));
}
}
return $result;
}
}

128
src/Plugins/Init.php Normal file
View File

@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Console\Thanks;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\TestSuite;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class Init implements HandlesArguments
{
/**
* The option the triggers the init job.
*/
private const INIT_OPTION = '--init';
/**
* The files that will be created.
*/
private const STUBS = [
'phpunit.xml' => 'phpunit.xml',
'Pest.php' => 'tests/Pest.php',
'ExampleTest.php' => 'tests/ExampleTest.php',
];
/**
* @var OutputInterface
*/
private $output;
/**
* @var TestSuite
*/
private $testSuite;
/**
* Creates a new Plugin instance.
*/
public function __construct(TestSuite $testSuite, OutputInterface $output)
{
$this->testSuite = $testSuite;
$this->output = $output;
}
public function handleArguments(array $arguments): array
{
if (!array_key_exists(1, $arguments) || $arguments[1] !== self::INIT_OPTION) {
return $arguments;
}
unset($arguments[1]);
$this->init();
return array_values($arguments);
}
private function init(): void
{
$testsBaseDir = "{$this->testSuite->rootPath}/tests";
if (!is_dir($testsBaseDir)) {
if (!mkdir($testsBaseDir) && !is_dir($testsBaseDir)) {
$this->output->writeln(sprintf(
"\n <fg=white;bg=red;options=bold> ERROR </> Directory `%s` was not created.</>",
$testsBaseDir
));
return;
}
$this->output->writeln(
' <fg=black;bg=green;options=bold> DONE </> Created `tests` directory.</>',
);
}
foreach (self::STUBS as $from => $to) {
$fromPath = __DIR__ . "/../../stubs/init/{$from}";
$toPath = "{$this->testSuite->rootPath}/{$to}";
if (file_exists($toPath)) {
$this->output->writeln(sprintf(
' <fg=black;bg=yellow;options=bold> INFO </> File `%s` already exists, skipped.</>',
$to
));
continue;
}
if ($from === 'phpunit.xml' && file_exists($toPath . '.dist')) {
$this->output->writeln(sprintf(
' <fg=black;bg=yellow;options=bold> INFO </> File `%s` already exists, skipped.</>',
$to . '.dist'
));
continue;
}
if (!copy($fromPath, $toPath)) {
$this->output->writeln(sprintf(
'<fg=black;bg=red>[WARNING] Failed to copy stub `%s` to `%s`</>',
$from,
$toPath
));
continue;
}
$this->output->writeln(sprintf(
' <fg=black;bg=green;options=bold> DONE </> Created `%s` file.</>',
$to
));
}
$this->output->writeln(
"\n <fg=black;bg=green;options=bold> DONE </> Pest initialised.</>\n",
);
(new Thanks($this->output))();
exit(0);
}
}

View File

@ -19,6 +19,11 @@ use PHPUnit\Framework\TestCase;
*/
final class TestRepository
{
/**
* @var string
*/
private const SEPARATOR = '>>>';
/**
* @var array<string, TestCaseFactory>
*/
@ -50,7 +55,7 @@ final class TestRepository
[$classOrTraits, $groups, $hooks] = $uses;
$setClassName = function (TestCaseFactory $testCase, string $key) use ($path, $classOrTraits, $groups, $startsWith, $hooks): void {
[$filename] = explode('@', $key);
[$filename] = explode(self::SEPARATOR, $key);
if ((!is_dir($path) && $filename === $path) || (is_dir($path) && $startsWith($filename, $path))) {
foreach ($classOrTraits as $class) { /** @var string $class */
@ -131,10 +136,10 @@ final class TestRepository
throw ShouldNotHappen::fromMessage('Trying to create a test without description.');
}
if (array_key_exists(sprintf('%s@%s', $test->filename, $test->description), $this->state)) {
if (array_key_exists(sprintf('%s%s%s', $test->filename, self::SEPARATOR, $test->description), $this->state)) {
throw new TestAlreadyExist($test->filename, $test->description);
}
$this->state[sprintf('%s@%s', $test->filename, $test->description)] = $test;
$this->state[sprintf('%s%s%s', $test->filename, self::SEPARATOR, $test->description)] = $test;
}
}

68
src/Support/Arr.php Normal file
View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
/**
* Credits: most of this class methods and implementations
* belongs to the Arr helper of laravel/framework project
* (https://github.com/laravel/framework).
*
* @internal
*/
final class Arr
{
/**
* @param array<mixed> $array
* @param string|int $key
*/
public static function has(array $array, $key): bool
{
$key = (string) $key;
if (array_key_exists($key, $array)) {
return true;
}
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return false;
}
}
return true;
}
/**
* @param array<mixed> $array
* @param string|int $key
* @param null $default
*
* @return array|mixed|null
*/
public static function get(array $array, $key, $default = null)
{
$key = (string) $key;
if (array_key_exists($key, $array)) {
return $array[$key];
}
if (strpos($key, '.') === false) {
return $array[$key] ?? $default;
}
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return $default;
}
}
return $array;
}
}

169
src/Support/Coverage.php Normal file
View File

@ -0,0 +1,169 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Pest\Exceptions\ShouldNotHappen;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Node\Directory;
use SebastianBergmann\CodeCoverage\Node\File;
use SebastianBergmann\Environment\Runtime;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
/**
* @internal
*/
final class Coverage
{
/**
* Returns the coverage path.
*/
public static function getPath(): string
{
return implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 2),
'.temp',
'coverage.php',
]);
}
/**
* Runs true there is any code
* coverage driver available.
*/
public static function isAvailable(): bool
{
return (new Runtime())->canCollectCodeCoverage();
}
/**
* Reports the code coverage report to the
* console and returns the result in float.
*/
public static function report(OutputInterface $output): float
{
if (!file_exists($reportPath = self::getPath())) {
throw ShouldNotHappen::fromMessage(sprintf('Coverage not found in path: %s.', $reportPath));
}
/** @var CodeCoverage $codeCoverage */
$codeCoverage = require $reportPath;
unlink($reportPath);
$totalWidth = (new Terminal())->getWidth();
$dottedLineLength = $totalWidth <= 70 ? $totalWidth : 70;
$totalCoverage = $codeCoverage->getReport()->percentageOfExecutedLines();
$output->writeln(
sprintf(
' <fg=white;options=bold>Cov: </><fg=default>%s</>',
$totalCoverage->asString()
)
);
$output->writeln('');
/** @var Directory<File|Directory> $report */
$report = $codeCoverage->getReport();
foreach ($report->getIterator() as $file) {
if (!$file instanceof File) {
continue;
}
$dirname = dirname($file->id());
$basename = basename($file->id(), '.php');
$name = $dirname === '.' ? $basename : implode(DIRECTORY_SEPARATOR, [
$dirname,
$basename,
]);
$rawName = $dirname === '.' ? $basename : implode(DIRECTORY_SEPARATOR, [
$dirname,
$basename,
]);
$linesExecutedTakenSize = 0;
if ($file->percentageOfExecutedLines()->asString() != '0.00%') {
$linesExecutedTakenSize = strlen($uncoveredLines = trim(implode(', ', self::getMissingCoverage($file)))) + 1;
$name .= sprintf(' <fg=red>%s</>', $uncoveredLines);
}
$percentage = $file->numberOfExecutableLines() === 0
? '100.0'
: number_format($file->percentageOfExecutedLines()->asFloat(), 1, '.', '');
$takenSize = strlen($rawName . $percentage) + 4 + $linesExecutedTakenSize; // adding 3 space and percent sign
$percentage = sprintf(
'<fg=%s>%s</>',
$percentage === '100.0' ? 'green' : ($percentage === '0.0' ? 'red' : 'yellow'),
$percentage
);
$output->writeln(sprintf(
' %s %s %s %%',
$name,
str_repeat('.', max($dottedLineLength - $takenSize, 1)),
$percentage
));
}
return $totalCoverage->asFloat();
}
/**
* Generates an array of missing coverage on the following format:.
*
* ```
* ['11', '20..25', '50', '60..80'];
* ```
*
* @param File $file
*
* @return array<int, string>
*/
public static function getMissingCoverage($file): array
{
$shouldBeNewLine = true;
$eachLine = function (array $array, array $tests, int $line) use (&$shouldBeNewLine): array {
if (count($tests) > 0) {
$shouldBeNewLine = true;
return $array;
}
if ($shouldBeNewLine) {
$array[] = (string) $line;
$shouldBeNewLine = false;
return $array;
}
$lastKey = count($array) - 1;
if (array_key_exists($lastKey, $array) && strpos($array[$lastKey], '..') !== false) {
[$from] = explode('..', $array[$lastKey]);
$array[$lastKey] = $line > $from ? sprintf('%s..%s', $from, $line) : sprintf('%s..%s', $line, $from);
return $array;
}
$array[$lastKey] = sprintf('%s..%s', $array[$lastKey], $line);
return $array;
};
$array = [];
foreach (array_filter($file->lineCoverageData(), 'is_array') as $line => $tests) {
$array = $eachLine($array, $tests, $line);
}
return $array;
}
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Closure;
final class Extendable
{
/**
* The extendable class.
*
* @var string
*/
private $extendableClass;
/**
* Creates a new extendable instance.
*/
public function __construct(string $extendableClass)
{
$this->extendableClass = $extendableClass;
}
/**
* Register a custom extend.
*/
public function extend(string $name, Closure $extend): void
{
$this->extendableClass::extend($name, $extend);
}
}

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

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

45
stubs/init/Pest.php Normal file
View File

@ -0,0 +1,45 @@
<?php
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/
// uses(Tests\TestCase::class)->in('Feature');
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
function something()
{
// ..
}

18
stubs/init/phpunit.xml Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>

View File

@ -17,6 +17,12 @@
✓ it gets executed before each test
✓ it gets executed before each test once again
PASS Tests\Features\Coverage
✓ it has plugin
✓ it adds coverage if --coverage exist
✓ it adds coverage if --min exist
✓ it generates coverage based on file input
PASS Tests\Features\Datasets
✓ it throws exception if dataset does not exist
✓ it throws exception if dataset already exist
@ -51,12 +57,348 @@
✓ it creates unique test case names with ('Name 2', Pest\Plugin Object (), true)
✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), true) #3
✓ it creates unique test case names - count
✓ lazy multiple datasets with (1) / (3)
✓ lazy multiple datasets with (1) / (4)
✓ lazy multiple datasets with (2) / (3)
✓ lazy multiple datasets with (2) / (4)
✓ lazy multiple datasets did the job right
✓ eager multiple datasets with (1) / (3)
✓ eager multiple datasets with (1) / (4)
✓ eager multiple datasets with (2) / (3)
✓ eager multiple datasets with (2) / (4)
✓ eager multiple datasets did the job right
✓ lazy registered multiple datasets with (1) / (1)
✓ lazy registered multiple datasets with (1) / (2)
✓ lazy registered multiple datasets with (2) / (1)
✓ lazy registered multiple datasets with (2) / (2)
✓ lazy registered multiple datasets did the job right
✓ eager registered multiple datasets with (1) / (1)
✓ eager registered multiple datasets with (1) / (2)
✓ eager registered multiple datasets with (2) / (1)
✓ eager registered multiple datasets with (2) / (2)
✓ eager registered multiple datasets did the job right
✓ eager wrapped registered multiple datasets with (1) / (1)
✓ eager wrapped registered multiple datasets with (1) / (2)
✓ eager wrapped registered multiple datasets with (2) / (1)
✓ eager wrapped registered multiple datasets with (2) / (2)
✓ eager wrapped registered multiple datasets did the job right
✓ named multiple datasets with data set "one" / data set "three"
✓ named multiple datasets with data set "one" / data set "four"
✓ named multiple datasets with data set "two" / data set "three"
✓ named multiple datasets with data set "two" / data set "four"
✓ named multiple datasets did the job right
✓ more than two datasets with (1) / (3) / (5)
✓ more than two datasets with (1) / (3) / (6)
✓ more than two datasets with (1) / (4) / (5)
✓ more than two datasets with (1) / (4) / (6)
✓ more than two datasets with (2) / (3) / (5)
✓ more than two datasets with (2) / (3) / (6)
✓ more than two datasets with (2) / (4) / (5)
✓ more than two datasets with (2) / (4) / (6)
✓ more than two datasets did the job right
PASS Tests\Features\Exceptions
✓ it gives access the the underlying expectException
✓ it catch exceptions
✓ it catch exceptions and messages
PASS Tests\Features\Expect\HigherOrder\methods
✓ it can access methods
✓ it can access multiple methods
✓ it works with not
✓ it can accept arguments
✓ it works with each
✓ it works inside of each
✓ it works with sequence
✓ it can compose complex expectations
✓ it can handle nested method calls
PASS Tests\Features\Expect\HigherOrder\methodsAndProperties
✓ it can access 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
✓ it allows properties to be accessed from the value
✓ it can access multiple properties from the value
✓ it works with not
✓ it works with each
✓ it works inside of each
✓ it works with sequence
✓ it can compose complex expectations
✓ it works with objects
✓ it works with nested properties
PASS Tests\Features\Expect\each
✓ an exception is thrown if the the type is not iterable
✓ it expects on each item
✓ it chains expectations on each item
✓ opposite expectations on each item
✓ chained opposite and non-opposite expectations
✓ it can add expectations via "and"
✓ it accepts callables
PASS Tests\Features\Expect\extend
✓ it macros true is true
✓ it macros false is not true
✓ it macros true is true with argument
✓ it macros false is not true with argument
PASS Tests\Features\Expect\json
✓ it properly parses json string
✓ fails with broken json string
PASS Tests\Features\Expect\not
✓ not property calls
PASS Tests\Features\Expect\ray
✓ ray calls do not fail when ray is not installed
PASS Tests\Features\Expect\sequence
✓ an exception is thrown if the the type is not iterable
✓ allows for sequences of checks to be run on iterable data
✓ loops back to the start if it runs out of sequence items
✓ it works if the number of items in the iterable is smaller than the number of expectations
✓ it works with associative arrays
✓ it can be passed non-callable values
✓ it can be passed a mixture of value types
PASS Tests\Features\Expect\toBe
✓ strict comparisons
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeArray
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeBool
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeCallable
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeDirectory
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeEmpty
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeFalse
✓ strict comparisons
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeFile
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeFloat
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeGreatherThan
✓ passes
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeGreatherThanOrEqual
✓ passes
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeInfinite
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeInstanceOf
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeInt
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeIterable
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeJson
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeLessThan
✓ passes
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeLessThanOrEqual
✓ passes
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeNAN
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeNull
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeNumeric
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeObject
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeReadableDirectory
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeReadableFile
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeResource
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeScalar
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeString
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeTrue
✓ strict comparisons
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeWritableDirectory
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toBeWritableFile
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toContain
✓ passes strings
✓ passes arrays
✓ failures
✓ not failures
PASS Tests\Features\Expect\toEndWith
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toEqual
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toEqualCanonicalizing
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toEqualWithDelta
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toHaveCount
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toHaveKey
✓ pass
✓ pass with nested key
✓ pass with plain key with dots
✓ pass with value check
✓ pass with value check and nested key
✓ pass with value check and plain key with dots
✓ failures
✓ failures with nested key
✓ failures with plain key with dots
✓ fails with wrong value
✓ fails with wrong value and nested key
✓ fails with wrong value and plain key with dots
✓ not failures
✓ not failures with nested key
✓ not failures with plain key with dots
✓ not failures with correct value
✓ not failures with correct value and with nested key
✓ not failures with correct value and with plain key with dots
PASS Tests\Features\Expect\toHaveKeys
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toHaveProperty
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toMatch
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toMatchArray
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toMatchConstraint
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toMatchObject
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toStartWith
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Helpers
✓ it can set/get properties on $this
✓ it throws error if property do not exist
@ -68,6 +410,17 @@
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
… it is incompleted
… it is incompleted even with method calls like skip
… it is incompleted even with method calls like group
✓ it is not incompleted because of expect
✓ it is not incompleted because of assert
✓ it is not incompleted because of test with assertions
PASS Tests\Features\It
✓ it is a test
@ -78,6 +431,7 @@
✓ it will throw exception from call if no macro exists
PASS Tests\Features\PendingHigherOrderTests
✓ get 'foo'
✓ get 'foo' → get 'bar' → expect true → toBeTrue
✓ get 'foo' → expect true → toBeTrue
@ -122,6 +476,10 @@
PASS Tests\PHPUnit\CustomAffixes\AdditionalFileExtensionspec
✓ it runs file names like `AdditionalFileExtension.spec.php`
PASS Tests\PHPUnit\CustomAffixes\FolderWithAn\ExampleTest
✓ custom traits can be used
✓ trait applied in this file
PASS Tests\PHPUnit\CustomAffixes\ManyExtensionsclasstest
✓ it runs file names like `ManyExtensions.class.test.php`
@ -170,6 +528,9 @@
PASS Tests\Unit\Datasets
✓ it show only the names of named datasets in their description
✓ it show the actual dataset of non-named datasets in their description
✓ it show only the names of multiple named datasets in their description
✓ it show the actual dataset of multiple non-named datasets in their description
✓ it show the correct description for mixed named and not-named datasets
PASS Tests\Unit\Plugins\Version
✓ it outputs the version when --version is used
@ -220,5 +581,5 @@
✓ it is a test
✓ it uses correct parent class
Tests: 7 skipped, 120 passed
Tests: 4 incompleted, 7 skipped, 365 passed

View File

@ -8,11 +8,11 @@ beforeAll(function () use ($foo) {
});
it('gets executed before tests', function () use ($foo) {
expect($foo->bar)->toBe(1);
expect($foo)->bar->toBe(1);
$foo->bar = 'changed';
});
it('do not get executed before each test', function () use ($foo) {
expect($foo->bar)->toBe('changed');
expect($foo)->bar->toBe('changed');
});

View File

@ -0,0 +1,58 @@
<?php
use Pest\Plugins\Coverage as CoveragePlugin;
use Pest\Support\Coverage;
use Pest\TestSuite;
use Symfony\Component\Console\Output\ConsoleOutput;
it('has plugin')->assertTrue(class_exists(CoveragePlugin::class));
it('adds coverage if --coverage exist', function () {
$plugin = new CoveragePlugin(new ConsoleOutput());
$testSuite = TestSuite::getInstance();
expect($plugin->coverage)->toBeFalse();
$arguments = $plugin->handleArguments([]);
expect($arguments)->toEqual([]);
expect($plugin->coverage)->toBeFalse();
$arguments = $plugin->handleArguments(['--coverage']);
expect($arguments)->toEqual(['--coverage-php', Coverage::getPath()]);
expect($plugin->coverage)->toBeTrue();
});
it('adds coverage if --min exist', function () {
$plugin = new CoveragePlugin(new ConsoleOutput());
expect($plugin->coverageMin)->toEqual(0.0);
expect($plugin->coverage)->toBeFalse();
$plugin->handleArguments([]);
expect($plugin->coverageMin)->toEqual(0.0);
$plugin->handleArguments(['--min=2']);
expect($plugin->coverageMin)->toEqual(2.0);
$plugin->handleArguments(['--min=2.4']);
expect($plugin->coverageMin)->toEqual(2.4);
});
it('generates coverage based on file input', function () {
expect(Coverage::getMissingCoverage(new class() {
public function lineCoverageData(): array
{
return [
1 => ['foo'],
2 => ['bar'],
4 => [],
5 => [],
6 => [],
7 => null,
100 => null,
101 => ['foo'],
102 => [],
];
}
}))->toEqual([
'4..6', '102',
]);
});

View File

@ -137,3 +137,89 @@ it('creates unique test case names', function (string $name, Plugin $plugin, boo
it('creates unique test case names - count', function () use (&$counter) {
expect($counter)->toBe(6);
});
$datasets_a = [[1], [2]];
$datasets_b = [[3], [4]];
test('lazy multiple datasets', function ($text_a, $text_b) use ($state, $datasets_a, $datasets_b) {
$state->text .= $text_a . $text_b;
expect($datasets_a)->toContain([$text_a]);
expect($datasets_b)->toContain([$text_b]);
})->with($datasets_a, $datasets_b);
test('lazy multiple datasets did the job right', function () use ($state) {
expect($state->text)->toBe('12121212121213142324');
});
$state->text = '';
test('eager multiple datasets', function ($text_a, $text_b) use ($state, $datasets_a, $datasets_b) {
$state->text .= $text_a . $text_b;
expect($datasets_a)->toContain([$text_a]);
expect($datasets_b)->toContain([$text_b]);
})->with(function () use ($datasets_a) {
return $datasets_a;
})->with(function () use ($datasets_b) {
return $datasets_b;
});
test('eager multiple datasets did the job right', function () use ($state) {
expect($state->text)->toBe('1212121212121314232413142324');
});
test('lazy registered multiple datasets', function ($text_a, $text_b) use ($state, $datasets) {
$state->text .= $text_a . $text_b;
expect($datasets)->toContain([$text_a]);
expect($datasets)->toContain([$text_b]);
})->with('numbers.array')->with('numbers.array');
test('lazy registered multiple datasets did the job right', function () use ($state) {
expect($state->text)->toBe('121212121212131423241314232411122122');
});
test('eager registered multiple datasets', function ($text_a, $text_b) use ($state, $datasets) {
$state->text .= $text_a . $text_b;
expect($datasets)->toContain([$text_a]);
expect($datasets)->toContain([$text_b]);
})->with('numbers.array')->with('numbers.closure');
test('eager registered multiple datasets did the job right', function () use ($state) {
expect($state->text)->toBe('12121212121213142324131423241112212211122122');
});
test('eager wrapped registered multiple datasets', function ($text_a, $text_b) use ($state, $datasets) {
$state->text .= $text_a . $text_b;
expect($datasets)->toContain([$text_a]);
expect($datasets)->toContain([$text_b]);
})->with('numbers.closure.wrapped')->with('numbers.closure');
test('eager wrapped registered multiple datasets did the job right', function () use ($state) {
expect($state->text)->toBe('1212121212121314232413142324111221221112212211122122');
});
test('named multiple datasets', function ($text_a, $text_b) use ($state, $datasets_a, $datasets_b) {
$state->text .= $text_a . $text_b;
expect($datasets_a)->toContain([$text_a]);
expect($datasets_b)->toContain([$text_b]);
})->with([
'one' => [1],
'two' => [2],
])->with([
'three' => [3],
'four' => [4],
]);
test('named multiple datasets did the job right', function () use ($state) {
expect($state->text)->toBe('121212121212131423241314232411122122111221221112212213142324');
});
test('more than two datasets', function ($text_a, $text_b, $text_c) use ($state, $datasets_a, $datasets_b) {
$state->text .= $text_a . $text_b . $text_c;
expect($datasets_a)->toContain([$text_a]);
expect($datasets_b)->toContain([$text_b]);
expect([5, 6])->toContain($text_c);
})->with($datasets_a, $datasets_b)->with([5, 6]);
test('more than two datasets did the job right', function () use ($state) {
expect($state->text)->toBe('121212121212131423241314232411122122111221221112212213142324135136145146235236245246');
});

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

@ -0,0 +1,113 @@
<?php
it('can access methods', function () {
expect(new HasMethods())
->name()->toBeString()->toEqual('Has Methods');
});
it('can access multiple methods', function () {
expect(new HasMethods())
->name()->toBeString()->toEqual('Has Methods')
->quantity()->toBeInt()->toEqual(20);
});
it('works with not', function () {
expect(new HasMethods())
->name()->not->toEqual('world')->toEqual('Has Methods')
->quantity()->toEqual(20)->not()->toEqual('bar')->not->toBeNull;
});
it('can accept arguments', function () {
expect(new HasMethods())
->multiply(5, 4)->toBeInt->toEqual(20);
});
it('works with each', function () {
expect(new HasMethods())
->attributes()->toBeArray->each->not()->toBeNull
->attributes()->each(function ($attribute) {
$attribute->not->toBeNull();
});
});
it('works inside of each', function () {
expect(new HasMethods())
->books()->each(function ($book) {
$book->title->not->toBeNull->cost->toBeGreaterThan(19);
});
});
it('works with sequence', function () {
expect(new HasMethods())
->books()->sequence(
function ($book) { $book->title->toEqual('Foo')->cost->toEqual(20); },
function ($book) { $book->title->toEqual('Bar')->cost->toEqual(30); },
);
});
it('can compose complex expectations', function () {
expect(new HasMethods())
->toBeObject()
->name()->toEqual('Has Methods')->not()->toEqual('bar')
->quantity()->not->toEqual('world')->toEqual(20)->toBeInt
->multiply(3, 4)->not->toBeString->toEqual(12)
->attributes()->toBeArray()
->books()->toBeArray->each->not->toBeEmpty
->books()->sequence(
function ($book) { $book->title->toEqual('Foo')->cost->toEqual(20); },
function ($book) { $book->title->toEqual('Bar')->cost->toEqual(30); },
);
});
it('can handle nested method calls', function () {
expect(new HasMethods())
->newInstance()->newInstance()->name()->toEqual('Has Methods')->toBeString()
->newInstance()->name()->toEqual('Has Methods')->not->toBeInt
->name()->toEqual('Has Methods')
->books()->each->toBeArray();
});
class HasMethods
{
public function name()
{
return 'Has Methods';
}
public function quantity()
{
return 20;
}
public function multiply($x, $y)
{
return $x * $y;
}
public function attributes()
{
return [
'name' => $this->name(),
'quantity' => $this->quantity(),
];
}
public function books()
{
return [
[
'title' => 'Foo',
'cost' => 20,
],
[
'title' => 'Bar',
'cost' => 30,
],
];
}
public function newInstance()
{
return new static();
}
}

View File

@ -0,0 +1,76 @@
<?php
it('can access methods and properties', function () {
expect(new HasMethodsAndProperties())
->name->toEqual('Has Methods and Properties')->not()->toEqual('bar')
->multiply(3, 4)->not->toBeString->toEqual(12)
->posts->each(function ($post) {
$post->is_published->toBeTrue;
})->books()->toBeArray()
->posts->toBeArray->each->not->toBeEmpty
->books()->sequence(
function ($book) { $book->title->toEqual('Foo')->cost->toEqual(20); },
function ($book) { $book->title->toEqual('Bar')->cost->toEqual(30); },
);
});
it('can handle nested methods and properties', function () {
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)
->meta->toBeArray
->and(['foo' => 'bar'])
->toBeArray()
->foo->toEqual('bar');
expect(static::getCount())->toEqual(4);
});
class HasMethodsAndProperties
{
public $name = 'Has Methods and Properties';
public $meta = ['foo' => ['bar' => 'baz']];
public $posts = [
[
'is_published' => true,
'title' => 'Foo',
],
[
'is_published' => true,
'title' => 'Bar',
],
];
public function books()
{
return [
[
'title' => 'Foo',
'cost' => 20,
],
[
'title' => 'Bar',
'cost' => 30,
],
];
}
public function multiply($x, $y)
{
return $x * $y;
}
public function newInstance()
{
return new static();
}
}

View File

@ -0,0 +1,85 @@
<?php
it('allows properties to be accessed from the value', function () {
expect(['foo' => 1])->foo->toBeInt()->toEqual(1);
});
it('can access multiple properties from the value', function () {
expect(['foo' => 'bar', 'hello' => 'world'])
->foo->toBeString()->toEqual('bar')
->hello->toBeString()->toEqual('world');
});
it('works with not', function () {
expect(['foo' => 'bar', 'hello' => 'world'])
->foo->not->not->toEqual('bar')
->foo->not->toEqual('world')->toEqual('bar')
->hello->toEqual('world')->not()->toEqual('bar')->not->toBeNull;
});
it('works with each', function () {
expect(['numbers' => [1, 2, 3, 4], 'words' => ['hey', 'there']])
->numbers->toEqual([1, 2, 3, 4])->each->toBeInt->toBeLessThan(5)
->words->each(function ($word) {
$word->toBeString()->not->toBeInt();
});
});
it('works inside of each', function () {
expect(['books' => [['title' => 'Foo', 'cost' => 20], ['title' => 'Bar', 'cost' => 30]]])
->books->each(function ($book) {
$book->title->not->toBeNull->cost->toBeGreaterThan(19);
});
});
it('works with sequence', function () {
expect(['books' => [['title' => 'Foo', 'cost' => 20], ['title' => 'Bar', 'cost' => 30]]])
->books->sequence(
function ($book) { $book->title->toEqual('Foo')->cost->toEqual(20); },
function ($book) { $book->title->toEqual('Bar')->cost->toEqual(30); },
);
});
it('can compose complex expectations', function () {
expect(['foo' => 'bar', 'numbers' => [1, 2, 3, 4]])
->toContain('bar')->toBeArray()
->numbers->toEqual([1, 2, 3, 4])->not()->toEqual('bar')->each->toBeInt
->foo->not->toEqual('world')->toEqual('bar')
->numbers->toBeArray();
});
it('works with objects', function () {
expect(new HasProperties())
->name->toEqual('foo')->not->toEqual('world')
->posts->toHaveCount(2)->each(function ($post) { $post->is_published->toBeTrue(); })
->posts->sequence(
function ($post) { $post->title->toEqual('Foo'); },
function ($post) { $post->title->toEqual('Bar'); },
);
});
it('works with nested properties', function () {
expect(new HasProperties())
->nested->foo->bar->toBeString()->toEqual('baz')
->posts->toBeArray()->toHaveCount(2);
});
class HasProperties
{
public $name = 'foo';
public $posts = [
[
'is_published' => true,
'title' => 'Foo',
],
[
'is_published' => true,
'title' => 'Bar',
],
];
public $nested = [
'foo' => ['bar' => 'baz'],
];
}

View File

@ -0,0 +1,89 @@
<?php
use Pest\Expectation;
test('an exception is thrown if the the type is not iterable', function () {
expect('Foobar')->each()->toEqual('Foobar');
})->throws(BadMethodCallException::class, 'Expectation value is not iterable.');
it('expects on each item', function () {
expect([1, 1, 1])
->each()
->toEqual(1);
expect(static::getCount())->toBe(3); // + 1 assertion
expect([1, 1, 1])
->each
->toEqual(1);
expect(static::getCount())->toBe(7);
});
it('chains expectations on each item', function () {
expect([1, 1, 1])
->each()
->toBeInt()
->toEqual(1);
expect(static::getCount())->toBe(6); // + 1 assertion
expect([2, 2, 2])
->each
->toBeInt
->toEqual(2);
expect(static::getCount())->toBe(13);
});
test('opposite expectations on each item', function () {
expect([1, 2, 3])
->each()
->not()
->toEqual(4);
expect(static::getCount())->toBe(3);
expect([1, 2, 3])
->each()
->not->toBeString;
expect(static::getCount())->toBe(7);
});
test('chained opposite and non-opposite expectations', function () {
expect([1, 2, 3])
->each()
->not()
->toEqual(4)
->toBeInt();
expect(static::getCount())->toBe(6);
});
it('can add expectations via "and"', function () {
expect([1, 2, 3])
->each()
->toBeInt // + 3
->and([4, 5, 6])
->each
->toBeLessThan(7) // + 3
->not
->toBeLessThan(3)
->toBeGreaterThan(3) // + 3
->and('Hello World')
->toBeString // + 1
->toEqual('Hello World'); // + 1
expect(static::getCount())->toBe(14);
});
it('accepts callables', function () {
expect([1, 2, 3])->each(function ($number) {
expect($number)->toBeInstanceOf(Expectation::class);
expect($number->value)->toBeInt();
$number->toBeInt->not->toBeString;
});
expect(static::getCount())->toBe(12);
});

View File

@ -0,0 +1,29 @@
<?php
expect()->extend('toBeAMacroExpectation', function () {
$this->toBeTrue();
return $this;
});
expect()->extend('toBeAMacroExpectationWithArguments', function (bool $value) {
$this->toBe($value);
return $this;
});
it('macros true is true', function () {
expect(true)->toBeAMacroExpectation();
});
it('macros false is not true', function () {
expect(false)->not->toBeAMacroExpectation();
});
it('macros true is true with argument', function () {
expect(true)->toBeAMacroExpectationWithArguments(true);
});
it('macros false is not true with argument', function () {
expect(false)->not->toBeAMacroExpectationWithArguments(true);
});

View File

@ -0,0 +1,14 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('it properly parses json string', function () {
expect('{"name":"Nuno"}')
->json()
->name
->toBe('Nuno');
});
test('fails with broken json string', function () {
expect('{":"Nuno"}')->json();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,10 @@
<?php
test('not property calls', function () {
expect(true)
->toBeTrue()
->not()->toBeFalse()
->not->toBeFalse
->and(false)
->toBeFalse();
});

View File

@ -0,0 +1,5 @@
<?php
test('ray calls do not fail when ray is not installed', function () {
expect(true)->ray()->toBe(true);
});

View File

@ -0,0 +1,62 @@
<?php
test('an exception is thrown if the the type is not iterable', function () {
expect('Foobar')->each->sequence();
})->throws(BadMethodCallException::class, 'Expectation value is not iterable.');
test('allows for sequences of checks to be run on iterable data', function () {
expect([1, 2, 3])
->sequence(
function ($expectation) { $expectation->toBeInt()->toEqual(1); },
function ($expectation) { $expectation->toBeInt()->toEqual(2); },
function ($expectation) { $expectation->toBeInt()->toEqual(3); },
);
expect(static::getCount())->toBe(6);
});
test('loops back to the start if it runs out of sequence items', function () {
expect([1, 2, 3, 1, 2, 3, 1, 2])
->sequence(
function ($expectation) { $expectation->toBeInt()->toEqual(1); },
function ($expectation) { $expectation->toBeInt()->toEqual(2); },
function ($expectation) { $expectation->toBeInt()->toEqual(3); },
);
expect(static::getCount())->toBe(16);
});
test('it works if the number of items in the iterable is smaller than the number of expectations', function () {
expect([1, 2])
->sequence(
function ($expectation) { $expectation->toBeInt()->toEqual(1); },
function ($expectation) { $expectation->toBeInt()->toEqual(2); },
function ($expectation) { $expectation->toBeInt()->toEqual(3); },
);
expect(static::getCount())->toBe(4);
});
test('it works with associative arrays', function () {
expect(['foo' => 'bar', 'baz' => 'boom'])
->sequence(
function ($expectation, $key) { $expectation->toEqual('bar'); $key->toEqual('foo'); },
function ($expectation, $key) { $expectation->toEqual('boom'); $key->toEqual('baz'); },
);
});
test('it can be passed non-callable values', function () {
expect(['foo', 'bar', 'baz'])->sequence('foo', 'bar', 'baz');
expect(static::getCount())->toBe(3);
});
test('it can be passed a mixture of value types', function () {
expect(['foo', 'bar', 'baz'])->sequence(
'foo',
function ($expectation) { $expectation->toEqual('bar')->toBeString(); },
'baz'
);
expect(static::getCount())->toBe(4);
});

View File

@ -0,0 +1,20 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
expect(true)->toBeTrue()->and(false)->toBeFalse();
test('strict comparisons', function () {
$nuno = new stdClass();
$dries = new stdClass();
expect($nuno)->toBe($nuno)->not->toBe($dries);
});
test('failures', function () {
expect(1)->toBe(2);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(1)->not->toBe(1);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([1, 2, 3])->toBeArray();
expect('1, 2, 3')->not->toBeArray();
});
test('failures', function () {
expect(null)->toBeArray();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(['a', 'b', 'c'])->not->toBeArray();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(true)->toBeBool();
expect(0)->not->toBeBool();
});
test('failures', function () {
expect(null)->toBeBool();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(false)->not->toBeBool();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,18 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(function () {})->toBeCallable();
expect(null)->not->toBeCallable();
});
test('failures', function () {
$hello = 5;
expect($hello)->toBeCallable();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(function () { return 42; })->not->toBeCallable();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,17 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
$temp = sys_get_temp_dir();
expect($temp)->toBeDirectory();
});
test('failures', function () {
expect('/random/path/whatever')->toBeDirectory();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('.')->not->toBeDirectory();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,18 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([])->toBeEmpty();
expect(null)->toBeEmpty();
});
test('failures', function () {
expect([1, 2])->toBeEmpty();
expect(' ')->toBeEmpty();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect([])->not->toBeEmpty();
expect(null)->not->toBeEmpty();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('strict comparisons', function () {
expect(false)->toBeFalse();
});
test('failures', function () {
expect('')->toBeFalse();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(false)->not->toBe(false);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,23 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
beforeEach(function () {
touch($this->tempFile = sys_get_temp_dir() . '/fake.file');
});
afterEach(function () {
unlink($this->tempFile);
});
test('pass', function () {
expect($this->tempFile)->toBeFile();
});
test('failures', function () {
expect('/random/path/whatever.file')->toBeFile();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect($this->tempFile)->not->toBeFile();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(1.0)->toBeFloat();
expect(1)->not->toBeFloat();
});
test('failures', function () {
expect(42)->toBeFloat();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(log(3))->not->toBeFloat();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('passes', function () {
expect(42)->toBeGreaterThan(41);
expect(4)->toBeGreaterThan(3.9);
});
test('failures', function () {
expect(4)->toBeGreaterThan(4);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(5)->not->toBeGreaterThan(4);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('passes', function () {
expect(42)->toBeGreaterThanOrEqual(41);
expect(4)->toBeGreaterThanOrEqual(4);
});
test('failures', function () {
expect(4)->toBeGreaterThanOrEqual(4.1);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(5)->not->toBeGreaterThanOrEqual(5);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(log(0))->toBeInfinite();
expect(log(1))->not->toBeInfinite();
});
test('failures', function () {
expect(asin(2))->toBeInfinite();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(INF)->not->toBeInfinite();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(new Exception())->toBeInstanceOf(Exception::class);
expect(new Exception())->not->toBeInstanceOf(RuntimeException::class);
});
test('failures', function () {
expect(new Exception())->toBeInstanceOf(RuntimeException::class);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(new Exception())->not->toBeInstanceOf(Exception::class);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(42)->toBeInt();
expect(42.0)->not->toBeInt();
});
test('failures', function () {
expect(42.0)->toBeInt();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(6 * 7)->not->toBeInt();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,23 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([])->toBeIterable();
expect(null)->not->toBeIterable();
});
test('failures', function () {
expect(42)->toBeIterable();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
function gen(): iterable
{
yield 1;
yield 2;
yield 3;
}
expect(gen())->not->toBeIterable();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,17 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('{"hello":"world"}')->toBeJson();
expect('foo')->not->toBeJson();
expect('{"hello"')->not->toBeJson();
});
test('failures', function () {
expect(':"world"}')->toBeJson();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('{"hello":"world"}')->not->toBeJson();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('passes', function () {
expect(41)->toBeLessThan(42);
expect(4)->toBeLessThan(5);
});
test('failures', function () {
expect(4)->toBeLessThan(4);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(5)->not->toBeLessThan(6);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('passes', function () {
expect(41)->toBeLessThanOrEqual(42);
expect(4)->toBeLessThanOrEqual(4);
});
test('failures', function () {
expect(4)->toBeLessThanOrEqual(3.9);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(5)->not->toBeLessThanOrEqual(5);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(asin(2))->toBeNan();
expect(log(0))->not->toBeNan();
});
test('failures', function () {
expect(1)->toBeNan();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(acos(1.5))->not->toBeNan();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(null)->toBeNull();
expect('')->not->toBeNull();
});
test('failures', function () {
expect('hello')->toBeNull();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(null)->not->toBeNull();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(42)->toBeNumeric();
expect('A')->not->toBeNumeric();
});
test('failures', function () {
expect(null)->toBeNumeric();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(6 * 7)->not->toBeNumeric();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect((object) ['a' => 1])->toBeObject();
expect(['a' => 1])->not->toBeObject();
});
test('failures', function () {
expect(null)->toBeObject();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect((object) 'ciao')->not->toBeObject();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(sys_get_temp_dir())->toBeReadableDirectory();
});
test('failures', function () {
expect('/random/path/whatever')->toBeReadableDirectory();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(sys_get_temp_dir())->not->toBeReadableDirectory();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,23 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
beforeEach(function () {
touch($this->tempFile = sys_get_temp_dir() . '/fake.file');
});
afterEach(function () {
unlink($this->tempFile);
});
test('pass', function () {
expect($this->tempFile)->toBeReadableFile();
});
test('failures', function () {
expect('/random/path/whatever.file')->toBeReadableFile();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect($this->tempFile)->not->toBeReadableFile();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,22 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
$resource = tmpfile();
afterAll(function () use ($resource) {
fclose($resource);
});
test('pass', function () use ($resource) {
expect($resource)->toBeResource();
expect(null)->not->toBeResource();
});
test('failures', function () {
expect(null)->toBeResource();
})->throws(ExpectationFailedException::class);
test('not failures', function () use ($resource) {
expect($resource)->not->toBeResource();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(1.1)->toBeScalar();
});
test('failures', function () {
expect(null)->toBeScalar();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(42)->not->toBeScalar();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('1.1')->toBeString();
expect(1.1)->not->toBeString();
});
test('failures', function () {
expect(null)->toBeString();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('42')->not->toBeString();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('strict comparisons', function () {
expect(true)->toBeTrue();
});
test('failures', function () {
expect('')->toBeTrue();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(false)->not->toBe(false);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(sys_get_temp_dir())->toBeWritableDirectory();
});
test('failures', function () {
expect('/random/path/whatever')->toBeWritableDirectory();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(sys_get_temp_dir())->not->toBeWritableDirectory();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,23 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
beforeEach(function () {
touch($this->tempFile = sys_get_temp_dir() . '/fake.file');
});
afterEach(function () {
unlink($this->tempFile);
});
test('pass', function () {
expect($this->tempFile)->toBeWritableFile();
});
test('failures', function () {
expect('/random/path/whatever.file')->toBeWritableFile();
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect($this->tempFile)->not->toBeWritableFile();
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,19 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('passes strings', function () {
expect([1, 2, 42])->toContain(42);
});
test('passes arrays', function () {
expect('Nuno')->toContain('Nu');
});
test('failures', function () {
expect([1, 2, 42])->toContain(3);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect([1, 2, 42])->not->toContain(42);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('username')->toEndWith('name');
});
test('failures', function () {
expect('username')->toEndWith('password');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('username')->not->toEndWith('name');
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('00123')->toEqual(123);
});
test('failures', function () {
expect(['a', 'b', 'c'])->toEqual(['a', 'b']);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('042')->not->toEqual(42);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([1, 2, 3])->toEqualCanonicalizing([3, 1, 2]);
expect(['g', 'a', 'z'])->not->toEqualCanonicalizing(['a', 'z']);
});
test('failures', function () {
expect([3, 2, 1])->toEqualCanonicalizing([1, 2]);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(['a', 'b', 'c'])->not->toEqualCanonicalizing(['b', 'a', 'c']);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(1.0)->toEqualWithDelta(1.3, .4);
});
test('failures', function () {
expect(1.0)->toEqualWithDelta(1.5, .1);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(1.0)->not->toEqualWithDelta(1.6, .7);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([1, 2, 3])->toHaveCount(3);
});
test('failures', function () {
expect([1, 2, 3])->toHaveCount(4);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect([1, 2, 3])->not->toHaveCount(3);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,69 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
$test_array = [
'a' => 1,
'b',
'c' => 'world',
'd' => [
'e' => 'hello',
],
'key.with.dots' => false,
];
test('pass')->expect($test_array)->toHaveKey('c');
test('pass with nested key')->expect($test_array)->toHaveKey('d.e');
test('pass with plain key with dots')->expect($test_array)->toHaveKey('key.with.dots');
test('pass with value check')->expect($test_array)->toHaveKey('c', 'world');
test('pass with value check and nested key')->expect($test_array)->toHaveKey('d.e', 'hello');
test('pass with value check and plain key with dots')->expect($test_array)->toHaveKey('key.with.dots', false);
test('failures', function () use ($test_array) {
expect($test_array)->toHaveKey('foo');
})->throws(ExpectationFailedException::class, "Failed asserting that an array has the key 'foo'");
test('failures with nested key', function () use ($test_array) {
expect($test_array)->toHaveKey('d.bar');
})->throws(ExpectationFailedException::class, "Failed asserting that an array has the key 'd.bar'");
test('failures with plain key with dots', function () use ($test_array) {
expect($test_array)->toHaveKey('missing.key.with.dots');
})->throws(ExpectationFailedException::class, "Failed asserting that an array has the key 'missing.key.with.dots'");
test('fails with wrong value', function () use ($test_array) {
expect($test_array)->toHaveKey('c', 'bar');
})->throws(ExpectationFailedException::class);
test('fails with wrong value and nested key', function () use ($test_array) {
expect($test_array)->toHaveKey('d.e', 'foo');
})->throws(ExpectationFailedException::class);
test('fails with wrong value and plain key with dots', function () use ($test_array) {
expect($test_array)->toHaveKey('key.with.dots', true);
})->throws(ExpectationFailedException::class);
test('not failures', function () use ($test_array) {
expect($test_array)->not->toHaveKey('c');
})->throws(ExpectationFailedException::class, "Expecting Array (...) not to have key 'c'");
test('not failures with nested key', function () use ($test_array) {
expect($test_array)->not->toHaveKey('d.e');
})->throws(ExpectationFailedException::class, "Expecting Array (...) not to have key 'd.e'");
test('not failures with plain key with dots', function () use ($test_array) {
expect($test_array)->not->toHaveKey('key.with.dots');
})->throws(ExpectationFailedException::class, "Expecting Array (...) not to have key 'key.with.dots'");
test('not failures with correct value', function () use ($test_array) {
expect($test_array)->not->toHaveKey('c', 'world');
})->throws(ExpectationFailedException::class);
test('not failures with correct value and with nested key', function () use ($test_array) {
expect($test_array)->not->toHaveKey('d.e', 'hello');
})->throws(ExpectationFailedException::class);
test('not failures with correct value and with plain key with dots', function () use ($test_array) {
expect($test_array)->not->toHaveKey('key.with.dots', false);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(['a' => 1, 'b', 'c' => 'world', 'foo' => ['bar' => 'baz']])->toHaveKeys(['a', 'c', 'foo.bar']);
});
test('failures', function () {
expect(['a' => 1, 'b', 'c' => 'world', 'foo' => ['bar' => 'baz']])->toHaveKeys(['a', 'd', 'foo.bar', 'hello.world']);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(['a' => 1, 'b', 'c' => 'world', 'foo' => ['bar' => 'baz']])->not->toHaveKeys(['foo.bar', 'c', 'z']);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,22 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
$obj = new stdClass();
$obj->foo = 'bar';
$obj->fooNull = null;
test('pass', function () use ($obj) {
expect($obj)->toHaveProperty('foo');
expect($obj)->toHaveProperty('foo', 'bar');
expect($obj)->toHaveProperty('fooNull');
expect($obj)->toHaveProperty('fooNull', null);
});
test('failures', function () use ($obj) {
expect($obj)->toHaveProperty('bar');
})->throws(ExpectationFailedException::class);
test('not failures', function () use ($obj) {
expect($obj)->not->toHaveProperty('foo');
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('Hello World')->toMatch('/^hello wo.*$/i');
});
test('failures', function () {
expect('Hello World')->toMatch('/^hello$/i');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('Hello World')->not->toMatch('/^hello wo.*$/i');
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,31 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
beforeEach(function () {
$this->user = [
'id' => 1,
'name' => 'Nuno',
'email' => 'enunomaduro@gmail.com',
];
});
test('pass', function () {
expect($this->user)->toMatchArray([
'name' => 'Nuno',
'email' => 'enunomaduro@gmail.com',
]);
});
test('failures', function () {
expect($this->user)->toMatchArray([
'name' => 'Not the same name',
'email' => 'enunomaduro@gmail.com',
]);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect($this->user)->not->toMatchArray([
'id' => 1,
]);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\Constraint\IsTrue;
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(true)->toMatchConstraint(new IsTrue());
});
test('failures', function () {
expect(false)->toMatchConstraint(new IsTrue());
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(true)->not->toMatchConstraint(new IsTrue());
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,31 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
beforeEach(function () {
$this->user = (object) [
'id' => 1,
'name' => 'Nuno',
'email' => 'enunomaduro@gmail.com',
];
});
test('pass', function () {
expect($this->user)->toMatchObject([
'name' => 'Nuno',
'email' => 'enunomaduro@gmail.com',
]);
});
test('failures', function () {
expect($this->user)->toMatchObject([
'name' => 'Not the same name',
'email' => 'enunomaduro@gmail.com',
]);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect($this->user)->not->toMatchObject([
'id' => 1,
]);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('username')->toStartWith('user');
});
test('failures', function () {
expect('username')->toStartWith('password');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('username')->not->toStartWith('user');
})->throws(ExpectationFailedException::class);

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

@ -0,0 +1,17 @@
<?php
test('incompleted');
it('is incompleted');
it('is incompleted even with method calls like skip')->skip(false);
it('is incompleted even with method calls like group')->group('wtv');
it('is not incompleted because of expect')->expect(true)->toBeTrue();
it('is not incompleted because of assert')->assertTrue(true);
it('is not incompleted because of test with assertions', function () {
expect(true)->toBeTrue();
});

View File

@ -1,7 +1,7 @@
<?php
it('is a test', function () {
$this->assertArrayHasKey('key', ['key' => 'foo']);
expect(['key' => 'foo'])->toHaveKey('key')->key->toBeString();
});
it('is a higher order message test')->expect(true)->toBeTrue();

View File

@ -26,5 +26,6 @@ trait Gettable
}
}
get('foo'); // not incomplete because closure is created...
get('foo')->get('bar')->expect(true)->toBeTrue();
get('foo')->expect(true)->toBeTrue();

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();

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
class MyCustomClassTest extends PHPUnit\Framework\TestCase
{
public function assertTrueIsTrue()
{
$this->assertTrue(true);
}
}
uses(MyCustomClassTest::class);
test('custom traits can be used', function () {
$this->assertTrueIsTrue();
});
test('trait applied in this file')->assertTrueIsTrue();

Some files were not shown because too many files have changed in this diff Show More