Compare commits

...

24 Commits

Author SHA1 Message Date
0fadf9a02c chore: update changelog 2020-09-21 20:31:32 +01:00
2b138ad76b Merge pull request #191 from owenvoke/feature/assert-regex
feat(expectations): add toMatch
2020-09-21 20:28:42 +01:00
e3e4815b55 chore(expectations): rename 'toMatchRegEx' to 'toMatch' 2020-09-21 20:20:21 +01:00
f76f353c32 chore: update snapshots 2020-09-21 20:19:01 +01:00
16b9f54dc3 feat(expectations): add toMatchRegEx 2020-09-21 20:18:58 +01:00
281166475e Merge pull request #190 from owenvoke/feature/assert-constraint
feat(expectations): add toMatchConstraint
2020-09-21 20:14:39 +01:00
23805cb5d6 chore: update snapshots 2020-09-16 19:03:21 +01:00
76d0f9cfc1 feat(expectations): add toMatchConstraint 2020-09-16 19:02:33 +01:00
5b083e4eb1 chore: update changelog 2020-09-16 10:35:43 +01:00
f48694b18a Merge pull request #187 from owenvoke/feature/assert-string
feat: add 'toStartWith' and 'toEndWith' expectations
2020-09-16 10:33:00 +01:00
8fa59ddbf0 Merge pull request #189 from owenvoke/feature/changelog-workflow
chore: don't run changelog workflow on forks
2020-09-16 09:26:17 +01:00
2619db4026 chore: don't run changelog workflow on forks 2020-09-16 08:26:49 +01:00
f3a71fb100 chore: update snapshots 2020-09-16 08:22:35 +01:00
04fafe742c feat(expectations): add toEndWith 2020-09-16 08:21:46 +01:00
cad8a41e6d feat(expectations): add toStartWith 2020-09-16 08:20:00 +01:00
1567923cda docs: updates changelog 2020-09-15 21:57:50 +02:00
c7116afcae tests: updates snapshots 2020-09-15 21:56:39 +02:00
4e184b2f90 Adds toMatchObject 2020-09-15 21:53:25 +02:00
9b5f664f00 Merge pull request #185 from owenvoke/feature/stubs
chore: update PHPUnit config stubs
2020-09-15 13:06:33 +02:00
0e89525ea8 chore: fix PHPUnit config 2020-09-15 10:45:17 +01:00
0b6cdf8f02 chore: fix PHPUnit config stubs 2020-09-15 10:44:59 +01:00
5f63d959e1 docs: updates changelog 2020-09-13 15:35:49 +02:00
be7fe41179 docs: updates changelog 2020-09-13 15:16:38 +02:00
204f343831 feat: adds toHaveKeys expectation 2020-09-13 15:15:37 +02:00
16 changed files with 277 additions and 16 deletions

View File

@ -13,6 +13,8 @@ jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.repository == 'pestphp/pest'
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Checkout website repository - name: Checkout website repository

View File

@ -6,6 +6,23 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] ## [Unreleased]
## [v0.3.6 (2020-09-21)](https://github.com/pestphp/pest/compare/v0.3.5...v0.3.6)
### Added
- `toMatch` expectation ([#191](https://github.com/pestphp/pest/pull/191))
- `toMatchConstraint` expectation ([#190](https://github.com/pestphp/pest/pull/190))
## [v0.3.5 (2020-09-16)](https://github.com/pestphp/pest/compare/v0.3.4...v0.3.5)
### Added
- `toStartWith` and `toEndWith` expectations ([#187](https://github.com/pestphp/pest/pull/187))
## [v0.3.4 (2020-09-15)](https://github.com/pestphp/pest/compare/v0.3.3...v0.3.4)
### Added
- `toMatchObject` expectation ([4e184b2](https://github.com/pestphp/pest/commit/4e184b2f906c318a5e9cd38fe693cdab5c48d8a2))
## [v0.3.3 (2020-09-13)](https://github.com/pestphp/pest/compare/v0.3.2...v0.3.3)
### Added
- `toHaveKeys` expectation ([204f343](https://github.com/pestphp/pest/commit/204f343831adc17bb3734553c24fac92d02f27c7))
## [v0.3.2 (2020-09-12)](https://github.com/pestphp/pest/compare/v0.3.1...v0.3.2) ## [v0.3.2 (2020-09-12)](https://github.com/pestphp/pest/compare/v0.3.1...v0.3.2)
### Added ### Added
- Support to PHPUnit 9.3.9, and 9.3.10 ([1318bf9](https://github.com/pestphp/pest/commit/97f98569bc86e8b87f8cde963fe7b4bf5399623b)) - Support to PHPUnit 9.3.9, and 9.3.10 ([1318bf9](https://github.com/pestphp/pest/commit/97f98569bc86e8b87f8cde963fe7b4bf5399623b))

View File

@ -8,8 +8,8 @@
<directory suffix=".php">./tests</directory> <directory suffix=".php">./tests</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<coverage> <coverage processUncoveredFiles="true">
<include processUncoveredFiles="true"> <include>
<directory suffix=".php">./src</directory> <directory suffix=".php">./src</directory>
</include> </include>
</coverage> </coverage>

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest; namespace Pest;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint;
/** /**
* @internal * @internal
@ -158,6 +159,26 @@ final class Expectation
return $this; 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. * Asserts that $count matches the number of elements of the value.
*/ */
@ -170,11 +191,20 @@ final class Expectation
/** /**
* Asserts that the value contains the property $name. * Asserts that the value contains the property $name.
*
* @param mixed $value
*/ */
public function toHaveProperty(string $name): Expectation public function toHaveProperty(string $name, $value = null): Expectation
{ {
$this->toBeObject();
Assert::assertTrue(property_exists($this->value, $name)); Assert::assertTrue(property_exists($this->value, $name));
if (func_num_args() > 1) {
/* @phpstan-ignore-next-line */
Assert::assertEquals($value, $this->value->{$name});
}
return $this; return $this;
} }
@ -376,14 +406,30 @@ final class Expectation
/** /**
* Asserts that the value array has the provided $key. * Asserts that the value array has the provided $key.
*
* @param string|int $key
*/ */
public function toHaveKey(string $key): Expectation public function toHaveKey($key): Expectation
{ {
Assert::assertArrayHasKey($key, $this->value); Assert::assertArrayHasKey($key, $this->value);
return $this; 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. * Asserts that the value is a directory.
*/ */
@ -444,6 +490,41 @@ final class Expectation
return $this; 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) {
$this->toHaveProperty($property, $value);
}
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;
}
/** /**
* Dynamically calls methods on the class without any arguments. * Dynamically calls methods on the class without any arguments.
* *

View File

@ -27,6 +27,26 @@ final class OppositeExpectation
$this->original = $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->throwExpectationFailedExpection('toHaveKey', [$key]);
}
return $this->original;
}
/** /**
* Handle dynamic method calls into the original expectation. * Handle dynamic method calls into the original expectation.
* *

View File

@ -6,5 +6,5 @@ namespace Pest;
function version(): string function version(): string
{ {
return '0.3.1'; return '0.3.6';
} }

View File

@ -12,10 +12,10 @@
<directory suffix="Test.php">./tests/Feature</directory> <directory suffix="Test.php">./tests/Feature</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter> <coverage processUncoveredFiles="true">
<whitelist processUncoveredFilesFromWhitelist="true"> <include>
<directory suffix=".php">./app</directory> <directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory> <directory suffix=".php">./src</directory>
</whitelist> </include>
</filter> </coverage>
</phpunit> </phpunit>

View File

@ -9,9 +9,9 @@
<directory suffix="Test.php">./tests</directory> <directory suffix="Test.php">./tests</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter> <coverage processUncoveredFiles="true">
<whitelist processUncoveredFilesFromWhitelist="true"> <include>
<directory suffix=".php">./app</directory> <directory suffix=".php">./app</directory>
</whitelist> </include>
</filter> </coverage>
</phpunit> </phpunit>

View File

@ -155,6 +155,11 @@
✓ passes strings ✓ passes strings
✓ passes arrays ✓ passes arrays
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toEndWith
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toEqual PASS Tests\Expect\toEqual
@ -180,11 +185,36 @@
PASS Tests\Expect\toHaveKey PASS Tests\Expect\toHaveKey
✓ pass ✓ pass
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toHaveKeys
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toHaveProperty PASS Tests\Expect\toHaveProperty
✓ pass ✓ pass
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toMatch
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toMatchConstraint
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toMatchObject
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toStartWith
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Features\AfterAll PASS Tests\Features\AfterAll
@ -353,5 +383,5 @@
✓ depends with defined arguments ✓ depends with defined arguments
✓ depends run test only once ✓ depends run test only once
Tests: 6 skipped, 208 passed Tests: 6 skipped, 226 passed

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(['a' => 1, 'b', 'c' => 'world'])->toHaveKeys(['a', 'c']);
});
test('failures', function () {
expect(['a' => 1, 'b', 'c' => 'world'])->toHaveKeys(['a', 'd']);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect(['a' => 1, 'hello' => 'world', 'c'])->not->toHaveKeys(['hello', 'c']);
})->throws(ExpectationFailedException::class);

View File

@ -2,11 +2,15 @@
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
$obj = new stdClass(); $obj = new stdClass();
$obj->foo = 'bar'; $obj->foo = 'bar';
$obj->fooNull = null;
test('pass', function () use ($obj) { test('pass', function () use ($obj) {
expect($obj)->toHaveProperty('foo'); expect($obj)->toHaveProperty('foo');
expect($obj)->toHaveProperty('foo', 'bar');
expect($obj)->toHaveProperty('fooNull');
expect($obj)->toHaveProperty('fooNull', null);
}); });
test('failures', function () use ($obj) { test('failures', function () use ($obj) {

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