Merge pull request #123 from pestphp/feat/expect

feat: add `expect` function
This commit is contained in:
Nuno Maduro
2020-07-18 19:04:37 +02:00
committed by GitHub
69 changed files with 1321 additions and 2027 deletions

View File

@ -3,7 +3,6 @@
$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . DIRECTORY_SEPARATOR . 'tests')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'bin')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'compiled')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'scripts')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'stubs')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'src')

View File

File diff suppressed because it is too large Load Diff

View File

@ -31,8 +31,7 @@
},
"files": [
"src/globals.php",
"src/Pest.php",
"compiled/globals.php"
"src/Pest.php"
]
},
"autoload-dev": {
@ -65,7 +64,6 @@
"bin/pest"
],
"scripts": {
"compile": "@php ./scripts/compile.php",
"lint": "rector process src && php-cs-fixer fix -v",
"test:lint": "php-cs-fixer fix -v --dry-run && rector process src --dry-run",
"test:types": "phpstan analyse --ansi --memory-limit=0",

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest\Concerns;
use Closure;
use Pest\Expectation;
use Pest\Support\ExceptionTrace;
use Pest\TestSuite;
use PHPUnit\Util\Test;
@ -87,6 +88,16 @@ trait TestCase
parent::tearDownAfterClass();
}
/**
* Creates a new expectation.
*
* @param mixed $value
*/
public function expect($value): Expectation
{
return new Expectation($value);
}
/**
* Gets executed before the test.
*/

425
src/Expectation.php Normal file
View File

@ -0,0 +1,425 @@
<?php
declare(strict_types=1);
namespace Pest;
use PHPUnit\Framework\Assert;
/**
* @internal
*
* @property Expectation $not Creates the opposite expectation.
*/
final class Expectation
{
/**
* The expectation value.
*
* @var mixed
*/
private $value;
/**
* Creates a new expectation.
*
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Creates a new expectation.
*
* @param mixed $value
*/
public function and($value): Expectation
{
return new self($value);
}
/**
* Creates the opposite expectation for the value.
*/
public function not(): OppositeExpectation
{
return new OppositeExpectation($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 $value
*/
public function toBe($value): Expectation
{
Assert::assertSame($value, $this->value);
return $this;
}
/**
* Assert the value is empty.
*/
public function toBeEmpty(): Expectation
{
Assert::assertEmpty($this->value);
return $this;
}
/**
* Assert the value is true.
*/
public function toBeTrue(): Expectation
{
Assert::assertTrue($this->value);
return $this;
}
/**
* Assert the value is false.
*/
public function toBeFalse(): Expectation
{
Assert::assertFalse($this->value);
return $this;
}
/**
* Assert the value is greater than expected one.
*
* @param int|float $value
*/
public function toBeGreaterThan($value): Expectation
{
Assert::assertGreaterThan($value, $this->value);
return $this;
}
/**
* Assert that value is greater than or equal to the expected one.
*
* @param int|float $value
*/
public function toBeGreaterThanOrEqual($value): Expectation
{
Assert::assertGreaterThanOrEqual($value, $this->value);
return $this;
}
/**
* Assert that value is less than or equal to the expected one.
*
* @param int|float $value
*/
public function toBeLessThan($value): Expectation
{
Assert::assertLessThan($value, $this->value);
return $this;
}
/**
* Assert that value is less than the expected one.
*
* @param int|float $value
*/
public function toBeLessThanOrEqual($value): Expectation
{
Assert::assertLessThanOrEqual($value, $this->value);
return $this;
}
/**
* Assert that needles is an element of value.
*
* @param mixed $value
*/
public function toContain($value): Expectation
{
if (is_string($this->value)) {
Assert::assertStringContainsString($value, $this->value);
} else {
Assert::assertContains($value, $this->value);
}
return $this;
}
/**
* Assert that $count matches the number of elements of $value.
*/
public function toHaveCount(int $count): Expectation
{
Assert::assertCount($count, $this->value);
return $this;
}
/**
* Assert that the $value contains the property $name.
*/
public function toHaveProperty(string $name): Expectation
{
Assert::assertTrue(property_exists($this->value, $name));
return $this;
}
/**
* Asserts that two variables have the same value.
*
* @param mixed $value
*/
public function toEqual($value): Expectation
{
Assert::assertEquals($value, $this->value);
return $this;
}
/**
* Asserts that two variables have the same value.
* The contents of $expected and $actual are canonicalized before
* they are compared. For instance, when the two variables $value and
* $this->value are arrays, then these arrays are sorted before they are
* compared. When $value and $this->value are objects,
* each object is converted to an array containing all private,
* protected and public attributes.
*
* @param mixed $value
*/
public function toEqualCanonicalizing($value): Expectation
{
Assert::assertEqualsCanonicalizing($value, $this->value);
return $this;
}
/**
* Assert that the absolute difference between $value and $this->value
* is greater than $delta.
*
* @param mixed $value
*/
public function toEqualWithDelta($value, float $delta): Expectation
{
Assert::assertEqualsWithDelta($value, $this->value, $delta);
return $this;
}
/**
* Assert that the value infinite.
*/
public function toBeInfinite(): Expectation
{
Assert::assertInfinite($this->value);
return $this;
}
/**
* Assert that the value is an instance of $value.
*
* @param string $class
*/
public function toBeInstanceOf($class): Expectation
{
/* @phpstan-ignore-next-line */
Assert::assertInstanceOf($class, $this->value);
return $this;
}
/**
* Assert that the value is an array.
*/
public function toBeArray(): Expectation
{
Assert::assertIsArray($this->value);
return $this;
}
/**
* Assert that the value is of type bool.
*/
public function toBeBool(): Expectation
{
Assert::assertIsBool($this->value);
return $this;
}
/**
* Assert that the value is of type callable.
*/
public function toBeCallable(): Expectation
{
Assert::assertIsCallable($this->value);
return $this;
}
/**
* Assert that the value is type of float.
*/
public function toBeFloat(): Expectation
{
Assert::assertIsFloat($this->value);
return $this;
}
/**
* Assert that the value is type of int.
*/
public function toBeInt(): Expectation
{
Assert::assertIsInt($this->value);
return $this;
}
/**
* Assert that the value is type of iterable.
*/
public function toBeIterable(): Expectation
{
Assert::assertIsIterable($this->value);
return $this;
}
/**
* Assert that the value is type of numeric.
*/
public function toBeNumeric(): Expectation
{
Assert::assertIsNumeric($this->value);
return $this;
}
/**
* Assert that the value is type of object.
*/
public function toBeObject(): Expectation
{
Assert::assertIsObject($this->value);
return $this;
}
/**
* Assert that the value is type of resource.
*/
public function toBeResource(): Expectation
{
Assert::assertIsResource($this->value);
return $this;
}
/**
* Assert that the value is type of scalar.
*/
public function toBeScalar(): Expectation
{
Assert::assertIsScalar($this->value);
return $this;
}
/**
* Assert that the value is type of string.
*/
public function toBeString(): Expectation
{
Assert::assertIsString($this->value);
return $this;
}
/**
* Assert that the value is NAN.
*/
public function toBeNan(): Expectation
{
Assert::assertNan($this->value);
return $this;
}
/**
* Assert that the value is null.
*/
public function toBeNull(): Expectation
{
Assert::assertNull($this->value);
return $this;
}
/**
* Assert that the value array has the key.
*/
public function toHaveKey(string $key): Expectation
{
Assert::assertArrayHasKey($key, $this->value);
return $this;
}
/**
* Assert that the value is a directory.
*/
public function toBeDirectory(): Expectation
{
Assert::assertDirectoryExists($this->value);
return $this;
}
/**
* Assert that the value is a directory and is readable.
*/
public function toBeReadableDirectory(): Expectation
{
Assert::assertDirectoryIsReadable($this->value);
return $this;
}
/**
* Assert that the value is a directory and is writable.
*/
public function toBeWritableDirectory(): Expectation
{
Assert::assertDirectoryIsWritable($this->value);
return $this;
}
/**
* Dynamically calls methods on the class without any arguments.
*
* @return Expectation
*/
public function __get(string $name)
{
/* @phpstan-ignore-next-line */
return $this->{$name}();
}
}

View File

@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Pest;
use PHPUnit\Framework\ExpectationFailedException;
/**
* @internal
*
* @mixin Expectation
*/
final class OppositeExpectation
{
/**
* @var Expectation
*/
private $original;
/**
* Creates a new opposite expectation.
*/
public function __construct(Expectation $original)
{
$this->original = $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;
}
throw new ExpectationFailedException(sprintf('@todo'));
}
/**
* 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;
}
throw new ExpectationFailedException(sprintf('@todo'));
}
}

View File

@ -12,6 +12,8 @@ use Pest\TestSuite;
use SebastianBergmann\Exporter\Exporter;
/**
* @method \Pest\Expectation expect(mixed $value)
*
* @internal
*/
final class TestCall

View File

@ -3,6 +3,7 @@
declare(strict_types=1);
use Pest\Datasets;
use Pest\Expectation;
use Pest\PendingObjects\AfterEachCall;
use Pest\PendingObjects\BeforeEachCall;
use Pest\PendingObjects\TestCall;
@ -104,3 +105,15 @@ function afterAll(Closure $closure = null): void
{
TestSuite::getInstance()->afterAll->set($closure);
}
/**
* Creates a new expectation.
*
* @param mixed $value the Value
*
* @return Expectation
*/
function expect($value)
{
return test()->expect($value);
}

View File

@ -2,6 +2,176 @@
PASS Tests\CustomTestCase\ExecutedTest
✓ that gets executed
PASS Tests\Expect\not
✓ not property calls
PASS Tests\Expect\toBe
✓ expect true → toBeTrue → and false → toBeFalse
✓ strict comparisons
✓ failures
✓ not failures
PASS Tests\Expect\toBeArray
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeBool
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeCallable
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeDirectory
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeEmpty
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeFalse
✓ strict comparisons
✓ failures
✓ not failures
PASS Tests\Expect\toBeFloat
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeGreatherThan
✓ passes
✓ failures
✓ not failures
PASS Tests\Expect\toBeGreatherThanOrEqual
✓ passes
✓ failures
✓ not failures
PASS Tests\Expect\toBeInfinite
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeInstanceOf
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeInt
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeIterable
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeLessThan
✓ passes
✓ failures
✓ not failures
PASS Tests\Expect\toBeLessThanOrEqual
✓ passes
✓ failures
✓ not failures
PASS Tests\Expect\toBeNAN
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeNull
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeNumeric
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeObject
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeReadableDirectory
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeResource
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeScalar
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeString
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeTrue
✓ strict comparisons
✓ failures
✓ not failures
PASS Tests\Expect\toBeWritableDirectory
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toContain
✓ passes strings
✓ passes arrays
✓ failures
✓ not failures
PASS Tests\Expect\toEqual
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toEqualCanonicalizing
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toEqualWithDelta
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toHaveCount
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toHaveKey
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toHaveProperty
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\AfterAll
✓ deletes file after all
@ -83,8 +253,8 @@
✓ it has bar
PASS Tests\Features\PendingHigherOrderTests
✓ get 'foo' → get 'bar' → assertTrue true
✓ get 'foo' → assertTrue true
✓ get 'foo' → get 'bar' → expect true → toBeTrue
✓ get 'foo' → expect true → toBeTrue
WARN Tests\Features\Skip
✓ it do not skips
@ -167,5 +337,5 @@
WARN Tests\Visual\Success
- visual snapshot of test suite on success
Tests: 6 skipped, 96 passed
Time: 3.43s
Tests: 6 skipped, 198 passed
Time: 5.46s

View File

@ -8,7 +8,7 @@ trait PluginTrait
{
public function assertPluginTraitGotRegistered(): void
{
assertTrue(true);
$this->assertTrue(true);
}
}
@ -16,7 +16,7 @@ trait SecondPluginTrait
{
public function assertSecondPluginTraitGotRegistered(): void
{
assertTrue(true);
$this->assertTrue(true);
}
}

10
tests/Expect/not.php Normal file
View File

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

20
tests/Expect/toBe.php Normal file
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);

16
tests/Expect/toBeBool.php Normal file
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,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);

16
tests/Expect/toBeInt.php Normal file
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,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);

16
tests/Expect/toBeNAN.php Normal file
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);

16
tests/Expect/toBeNull.php Normal file
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())->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,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);

15
tests/Expect/toBeTrue.php Normal file
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,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);

15
tests/Expect/toEqual.php Normal file
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,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect(['a' => 1, 'b', 'c' => 'world'])->toHaveKey('c');
});
test('failures', function () {
expect(['a' => 1, 'b', 'c' => 'world'])->toHaveKey('hello');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(['a' => 1, 'hello' => 'world', 'c'])->not->toHaveKey('hello');
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,18 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
$obj = new stdClass();
$obj->foo = 'bar';
test('pass', function () use ($obj) {
expect($obj)->toHaveProperty('foo');
});
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

@ -8,8 +8,8 @@ afterAll(function () use ($file) {
test('deletes file after all', function () use ($file) {
file_put_contents($file, 'foo');
assertFileExists($file);
$this->assertFileExists($file);
register_shutdown_function(function () use ($file) {
assertFileNotExists($file);
$this->assertFileNotExists($file);
});
});

View File

@ -11,10 +11,10 @@ afterEach(function () use ($state) {
});
it('does not get executed before the test', function () {
assertFalse(property_exists($this->state, 'bar'));
expect($this->state)->not->toHaveProperty('bar');
});
it('gets executed after the test', function () {
assertTrue(property_exists($this->state, 'bar'));
assertEquals(2, $this->state->bar);
expect($this->state)->toHaveProperty('bar');
expect($this->state->bar)->toBe(2);
});

View File

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

View File

@ -5,11 +5,11 @@ beforeEach(function () {
});
it('gets executed before each test', function () {
assertEquals($this->bar, 2);
expect($this->bar)->toBe(2);
$this->bar = 'changed';
});
it('gets executed before each test once again', function () {
assertEquals($this->bar, 2);
expect($this->bar)->toBe(2);
});

View File

@ -23,13 +23,13 @@ it('sets closures', function () {
yield [1];
});
assertEquals([[1]], iterator_to_array(Datasets::get('foo')()));
expect(iterator_to_array(Datasets::get('foo')()))->toBe([[1]]);
});
it('sets arrays', function () {
Datasets::set('bar', [[2]]);
assertEquals([[2]], Datasets::get('bar'));
expect(Datasets::get('bar'))->toBe([[2]]);
});
it('gets bound to test case object', function () {
@ -37,7 +37,7 @@ it('gets bound to test case object', function () {
})->with([['a'], ['b']]);
test('it truncates the description', function () {
assertTrue(true);
expect(true)->toBe(true);
// it gets tested by the integration test
})->with([str_repeat('Fooo', 10000000)]);
@ -48,51 +48,51 @@ $datasets = [[1], [2]];
test('lazy datasets', function ($text) use ($state, $datasets) {
$state->text .= $text;
assertTrue(in_array([$text], $datasets));
expect(in_array([$text], $datasets))->toBe(true);
})->with($datasets);
test('lazy datasets did the job right', function () use ($state) {
assertEquals('12', $state->text);
expect($state->text)->toBe('12');
});
$state->text = '';
test('eager datasets', function ($text) use ($state, $datasets) {
$state->text .= $text;
assertTrue(in_array([$text], $datasets));
expect($datasets)->toContain([$text]);
})->with(function () use ($datasets) {
return $datasets;
});
test('eager datasets did the job right', function () use ($state) {
assertEquals('1212', $state->text);
expect($state->text)->toBe('1212');
});
test('lazy registered datasets', function ($text) use ($state, $datasets) {
$state->text .= $text;
assertTrue(in_array([$text], $datasets));
expect($datasets)->toContain([$text]);
})->with('numbers.array');
test('lazy registered datasets did the job right', function () use ($state) {
assertEquals('121212', $state->text);
expect($state->text)->toBe('121212');
});
test('eager registered datasets', function ($text) use ($state, $datasets) {
$state->text .= $text;
assertTrue(in_array([$text], $datasets));
expect($datasets)->toContain([$text]);
})->with('numbers.closure');
test('eager registered datasets did the job right', function () use ($state) {
assertEquals('12121212', $state->text);
expect($state->text)->toBe('12121212');
});
test('eager wrapped registered datasets', function ($text) use ($state, $datasets) {
$state->text .= $text;
assertTrue(in_array([$text], $datasets));
expect($datasets)->toContain([$text]);
})->with('numbers.closure.wrapped');
test('eager registered wrapped datasets did the job right', function () use ($state) {
assertEquals('1212121212', $state->text);
expect($state->text)->toBe('1212121212');
});
class Bar
@ -105,13 +105,13 @@ $namedDatasets = [
];
test('lazy named datasets', function ($text) use ($state, $datasets) {
assertTrue(true);
expect(true)->toBeTrue();
})->with($namedDatasets);
$counter = 0;
it('creates unique test case names', function (string $name, Plugin $plugin, bool $bool) use (&$counter) {
assertTrue(true);
expect(true)->toBeTrue();
$counter++;
})->with([
['Name 1', new Plugin(), true],
@ -123,5 +123,5 @@ it('creates unique test case names', function (string $name, Plugin $plugin, boo
]);
it('creates unique test case names - count', function () use (&$counter) {
assertEquals(6, $counter);
expect($counter)->toBe(6);
});

View File

@ -3,38 +3,32 @@
$runCounter = 0;
test('first', function () use (&$runCounter) {
assertTrue(true);
expect(true)->toBeTrue();
$runCounter++;
return 'first';
});
test('second', function () use (&$runCounter) {
assertTrue(true);
expect(true)->toBeTrue();
$runCounter++;
return 'second';
});
test('depends', function () {
assertEquals(
['first', 'second'],
func_get_args()
);
expect(func_get_args())->toBe(['first', 'second']);
})->depends('first', 'second');
test('depends with ...params', function (string ...$params) {
assertEquals(
['first', 'second'],
$params
);
expect(func_get_args())->toBe($params);
})->depends('first', 'second');
test('depends with defined arguments', function (string $first, string $second) {
assertEquals('first', $first);
assertEquals('second', $second);
expect($first)->toBe('first');
expect($second)->toBe('second');
})->depends('first', 'second');
test('depends run test only once', function () use (&$runCounter) {
assertEquals(2, $runCounter);
expect($runCounter)->toBe(2);
})->depends('first', 'second');

View File

@ -7,7 +7,7 @@ function addUser()
it('can set/get properties on $this', function () {
addUser();
assertEquals('nuno', $this->user);
expect($this->user)->toBe('nuno');
});
it('throws error if property do not exist', function () {
@ -34,8 +34,7 @@ function mockUser()
it('allows to call underlying protected/private methods', function () {
$user = mockUser();
assertEquals('maduro', $user->getName());
expect($user->getName())->toBe('maduro');
});
it('throws error if method do not exist', function () {

View File

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

View File

@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
uses(Macroable::class);
beforeEach()->macro('bar', function () {
assertInstanceOf(TestCase::class, $this);
expect($this)->toBeInstanceOf(TestCase::class);
});
it('can call chained macro method')->bar();

View File

@ -1,11 +1,12 @@
<?php
use Pest\PendingObjects\TestCall;
use PHPUnit\Framework\TestCase;
uses(Gettable::class);
/**
* @return TestCase|Gettable
* @return TestCase|TestCall|Gettable
*/
function get(string $route)
{
@ -15,15 +16,15 @@ function get(string $route)
trait Gettable
{
/**
* @return TestCase|Gettable
* @return TestCase|TestCall|Gettable
*/
public function get(string $route)
{
assertNotEmpty($route);
expect($route)->not->toBeEmpty();
return $this;
}
}
get('foo')->get('bar')->assertTrue(true);
get('foo')->assertTrue(true);
get('foo')->get('bar')->expect(true)->toBeTrue();
get('foo')->expect(true)->toBeTrue();

View File

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

View File

@ -1,5 +1,7 @@
<?php
use function PHPUnit\Framework\assertFalse;
$foo = new stdClass();
$foo->beforeAll = false;
$foo->beforeEach = false;

View File

@ -10,6 +10,6 @@ class CustomTestCaseInSubFolder extends TestCase
{
public function assertCustomInSubFolderTrue()
{
assertTrue(true);
$this->assertTrue(true);
}
}

View File

@ -12,7 +12,7 @@ class MyCustomClass extends PHPUnit\Framework\TestCase
{
public function assertTrueIsTrue()
{
assertTrue(true);
$this->assertTrue(true);
}
}

View File

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

View File

@ -7,14 +7,14 @@ use PHPUnit\TextUI\DefaultResultPrinter;
it('sets defaults', function () {
$arguments = AddsDefaults::to(['bar' => 'foo']);
assertInstanceOf(Printer::class, $arguments['printer']);
assertEquals($arguments['bar'], 'foo');
expect($arguments['printer'])->toBeInstanceOf(Printer::class);
expect($arguments['bar'])->toBe('foo');
});
it('does not override options', function () {
$defaultResultPrinter = new DefaultResultPrinter();
assertEquals(AddsDefaults::to(['printer' => $defaultResultPrinter]), [
expect(AddsDefaults::to(['printer' => $defaultResultPrinter]))->tobe([
'printer' => $defaultResultPrinter,
]);
});

View File

@ -16,10 +16,10 @@ test('default php unit tests', function () {
$phpUnitTestCase = new class() extends PhpUnitTestCase {
};
$testSuite->addTest($phpUnitTestCase);
assertCount(1, $testSuite->tests());
expect($testSuite->tests())->toHaveCount(1);
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd()));
assertCount(1, $testSuite->tests());
expect($testSuite->tests())->toHaveCount(1);
});
it('removes warnings', function () use ($pestTestCase) {
@ -28,5 +28,5 @@ it('removes warnings', function () use ($pestTestCase) {
$testSuite->addTest($warningTestCase);
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd()));
assertCount(0, $testSuite->tests());
expect($testSuite->tests())->toHaveCount(0);
});

View File

@ -38,5 +38,5 @@ it('do not throws exception when `process isolation` is false', function () {
'configuration' => $filename,
]);
assertTrue(true);
expect(true)->toBeTrue();
});

View File

@ -8,7 +8,7 @@ it('outputs the version when --version is used', function () {
$plugin = new Version($output);
$plugin->handleArguments(['foo', '--version']);
assertStringContainsString('Pest 0.2.2', $output->fetch());
expect($output->fetch())->toContain('Pest 0.2.2');
});
it('do not outputs version when --version is not used', function () {
@ -16,5 +16,5 @@ it('do not outputs version when --version is not used', function () {
$plugin = new Version($output);
$plugin->handleArguments(['foo', 'bar']);
assertEquals('', $output->fetch());
expect($output->fetch())->toBe('');
});

View File

@ -7,5 +7,5 @@ it('gets file name from called file', function () {
return Backtrace::file();
};
assertEquals(__FILE__, $a());
expect($a())->toBe(__FILE__);
});

View File

@ -15,32 +15,32 @@ it('exists')
it('gets an instance', function () {
$this->container->add(Container::class, $this->container);
assertSame($this->container, $this->container->get(Container::class));
expect($this->container->get(Container::class))->toBe($this->container);
});
test('autowire', function () {
assertInstanceOf(Container::class, $this->container->get(Container::class));
expect($this->container->get(Container::class))->toBeInstanceOf(Container::class);
});
it('creates an instance and resolves parameters', function () {
$this->container->add(Container::class, $this->container);
$instance = $this->container->get(ClassWithDependency::class);
assertInstanceOf(ClassWithDependency::class, $instance);
expect($instance)->toBeInstanceOf(ClassWithDependency::class);
});
it('creates an instance and resolves also sub parameters', function () {
$this->container->add(Container::class, $this->container);
$instance = $this->container->get(ClassWithSubDependency::class);
assertInstanceOf(ClassWithSubDependency::class, $instance);
expect($instance)->toBeInstanceOf(ClassWithSubDependency::class);
});
it('can resolve builtin value types', function () {
$this->container->add('rootPath', getcwd());
$instance = $this->container->get(TestSuite::class);
assertInstanceOf(TestSuite::class, $instance);
expect($instance)->toBeInstanceOf(TestSuite::class);
});
it('cannot resolve a parameter without type', function () {

View File

@ -3,9 +3,10 @@
use Pest\Support\Reflection;
it('gets file name from closure', function () {
$fileName = Reflection::getFileNameFromClosure(function () {});
$fileName = Reflection::getFileNameFromClosure(function () {
});
assertEquals(__FILE__, $fileName);
expect($fileName)->toBe(__FILE__);
});
it('gets property values', function () {
@ -15,5 +16,5 @@ it('gets property values', function () {
$value = Reflection::getPropertyValue($class, 'foo');
assertEquals('bar', $value);
expect($value)->toBe('bar');
});

View File

@ -21,23 +21,15 @@ $snapshot = function ($name) {
};
test('allows to run a single test', function () use ($run, $snapshot) {
assertStringContainsString(
$snapshot('allows-to-run-a-single-test'),
$run('tests/Fixtures/DirectoryWithTests/ExampleTest.php'));
expect($run('tests/Fixtures/DirectoryWithTests/ExampleTest.php'))->toContain($snapshot('allows-to-run-a-single-test'));
})->skip(PHP_OS_FAMILY === 'Windows');
test('allows to run a directory', function () use ($run, $snapshot) {
assertStringContainsString(
$snapshot('allows-to-run-a-directory'),
$run('tests/Fixtures')
);
expect($run('tests/Fixtures'))->toContain($snapshot('allows-to-run-a-directory'));
})->skip(PHP_OS_FAMILY === 'Windows');
it('has ascii chars', function () use ($run, $snapshot) {
assertStringContainsString(
$snapshot('has-ascii-chars'),
$run('tests/Fixtures/DirectoryWithTests/ExampleTest.php', true)
);
expect($run('tests/Fixtures/DirectoryWithTests/ExampleTest.php', true))->toContain($snapshot('has-ascii-chars'));
})->skip(PHP_OS_FAMILY === 'Windows');
it('disable decorating printer when colors is set to never', function () use ($snapshot) {
@ -49,9 +41,5 @@ it('disable decorating printer when colors is set to never', function () use ($s
], dirname(__DIR__, 2));
$process->run();
$output = $process->getOutput();
assertStringContainsString(
$snapshot('disable-decorating-printer'),
$output
);
expect($output)->toContain($snapshot('disable-decorating-printer'));
})->skip(PHP_OS_FAMILY === 'Windows');

View File

@ -22,7 +22,8 @@ test('visual snapshot of test suite on success', function () {
$output = explode("\n", $output());
array_pop($output);
array_pop($output);
assertStringContainsString(implode("\n", $output), file_get_contents($snapshot));
expect(file_get_contents($snapshot))->toContain(implode("\n", $output));
}
})->skip(!getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE'))
->skip(PHP_OS_FAMILY === 'Windows');