Merge pull request #125 from ceceppa/feat/expect

Feat/expect
This commit is contained in:
Nuno Maduro
2020-07-14 21:13:42 +02:00
committed by GitHub
34 changed files with 1014 additions and 3 deletions

View File

@ -52,6 +52,347 @@ final class Expectation
return $this; return $this;
} }
/**
* Assert the value is empty.
*/
public function toBeEmpty(): Expectation
{
Assert::assertEmpty($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 $needle
*/
public function toContain($needle): Expectation
{
Assert::assertContains($needle, $this->value);
return $this;
}
/**
* Assert the value contains only variables of type.
*
* @param mixed $type
*/
public function toContainOnly($type): Expectation
{
Assert::assertContainsOnly($type, $this->value);
return $this;
}
/**
* Assert the value contains only instances of $instance.
*/
public function toContainOnlyInstancesOf(string $instance): Expectation
{
Assert::assertContainsOnlyInstancesOf($instance, $this->value);
return $this;
}
/**
* Assert that needles is a substring of value.
*/
public function toContainString(string $needle): Expectation
{
Assert::assertStringContainsString($needle, $this->value);
return $this;
}
/**
* Assert that needles is a substring of value, ignoring the
* difference in casing.
*/
public function toContainStringIgnoringCase(string $needle): Expectation
{
Assert::assertStringContainsStringIgnoringCase($needle, $this->value);
return $this;
}
/**
* Assert that $count matches the number of elements of $value.
*/
public function toCount(int $count): Expectation
{
Assert::assertCount($count, $this->value);
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 are equals, ignoring the casing
* for the comparison.
*
* @param mixed $value
*/
public function toEqualIgnoringCase($value): Expectation
{
Assert::assertEqualsIgnoringCase($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 mixed $value
*/
public function toBeInstanceOf($value): Expectation
{
Assert::assertInstanceOf($value, $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 NAN.
*/
public function toBeNull(): Expectation
{
Assert::assertNull($this->value);
return $this;
}
/** /**
* Dynamically calls methods on the class without any arguments. * Dynamically calls methods on the class without any arguments.
* *

View File

@ -2,9 +2,164 @@
PASS Tests\CustomTestCase\ExecutedTest PASS Tests\CustomTestCase\ExecutedTest
✓ that gets executed ✓ that gets executed
PASS Tests\Expect\ToBe PASS Tests\Expect\toBe
✓ strict comparisons ✓ strict comparisons
✓ failures ✓ 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\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\toBeResource
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeScalar
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toBeString
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toContain
✓ passes
✓ failures
✓ not failures
PASS Tests\Expect\toContainOnly
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toContainOnlyInstancesOf
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toContainString
✓ is case sensitive
✓ failures
✓ not failures
PASS Tests\Expect\toContainStringIgnoringCase
✓ ignore difference in casing
✓ failures
✓ not failures
PASS Tests\Expect\toCount
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toEqual
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toEqualCanonicalizing
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toEqualIgnoringCase
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toEqualWithDelta
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Features\AfterAll PASS Tests\Features\AfterAll
@ -172,5 +327,5 @@
WARN Tests\Visual\Success WARN Tests\Visual\Success
- visual snapshot of test suite on success - visual snapshot of test suite on success
Tests: 6 skipped, 99 passed Tests: 6 skipped, 192 passed
Time: 3.62s Time: 5.20s

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,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,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('passes', function () {
expect([1, 2, 42])->toContain(42);
});
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,17 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([1, 2, 3])->toContainOnly('int');
expect(['hello', 'world'])->toContainOnly('string');
});
test('failures', function () {
expect([1, 2, '3'])->toContainOnly('string');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect([1, 2, 3])->not->toContainOnly('int');
expect(['hello', 'world'])->not->toContainOnly('string');
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,23 @@
<?php
use Pest\Actions\AddsTests;
use Pest\Expectation;
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
$expected = [new Expectation('whatever')];
expect($expected)->toContainOnlyInstancesOf(Expectation::class);
});
test('failures', function () {
$expected = [new Expectation('whatever')];
expect($expected)->toContainOnlyInstancesOf(AddsTests::class);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
$expected = [new Expectation('whatever')];
expect($expected)->not->toContainOnlyInstancesOf(Expectation::class);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('is case sensitive', function () {
expect('hello world')->toContainString('world');
expect('hello world')->not->toContainString('World');
});
test('failures', function () {
expect('hello world')->toContainString('Hello');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('hello world')->not->toContainString('hello');
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,17 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('ignore difference in casing', function () {
expect('hello world')->toContainStringIgnoringCase('world');
expect('hello world')->toContainStringIgnoringCase('World');
});
test('failures', function () {
expect('hello world')->toContainStringIgnoringCase('hi');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('hello world')->not->toContainStringIgnoringCase('Hello');
expect('hello world')->not->toContainStringIgnoringCase('hello');
})->throws(ExpectationFailedException::class);

15
tests/Expect/toCount.php Normal file
View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect([1, 2, 3])->toCount(3);
});
test('failures', function () {
expect([1, 2, 3])->toCount(4);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect([1, 2, 3])->not->toCount(3);
})->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('00123')->toEqualCanonicalizing(123);
expect(['a', 'b', 'c'])->toEqualCanonicalizing(['c', 'a', 'b']);
});
test('failures', function () {
expect(['a', 'b', 'c'])->toEqualCanonicalizing(['a', 'b']);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('042')->not->toEqualCanonicalizing(42);
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,15 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('hello')->toEqualIgnoringCase('HELLO');
});
test('failures', function () {
expect('hello')->toEqualIgnoringCase('BAR');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('HELLO')->not->toEqualIgnoringCase('HelLo');
})->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);