Skip to content

Commit

Permalink
Verify 2.0.0-rc1
Browse files Browse the repository at this point in the history
Verify 2.0.0 Release Candidate 1
  • Loading branch information
Gustavo Nieves committed Aug 31, 2020
2 parents ebf833c + b880ef3 commit 252fe61
Show file tree
Hide file tree
Showing 36 changed files with 2,902 additions and 1,943 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
vendor
.idea
.phpunit.result.cache
composer.lock
composer.phar
2 changes: 0 additions & 2 deletions .travis.yml
@@ -1,8 +1,6 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4

Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,14 @@
# Changelog

## 2.0

* Support for Chained Verifiers.
* The Verify API is now fully based on the PHPUnit public API.
* Improved IDE autocompletion depending on the type of data you want to verify
* Simplified data validations.
* Improved code quality, performance and maintainability.
* See **BC** details in the UPGRADE.md file.

## 1.5

* Support for full PHPUnit API `(42 new verifiers!)`
Expand Down
96 changes: 53 additions & 43 deletions README.md
Expand Up @@ -19,30 +19,36 @@ With [BDD][3] assertions influenced by [Chai][4], [Jasmine][5], and [RSpec][6] y
composer require codeception/verify --dev
```

> :arrow_up: **Upgrade from 1.x by following [the upgrade guide.][9]**

## Usage

Use in any test `verify` function instead of `$this->assert*` methods:

```php
use Codeception\Verify\Verify;

$user = User::find(1);

// equal
// equals
verify($user->getName())->equals('davert');
verify("user have 5 posts", $user->getNumPosts())->equals(5);
verify($user->getNumPosts())->notEquals(3);

verify($user->getNumPosts())
->equals(5, 'user have 5 posts')
->notEquals(3);

// contains
verify('first user is admin', $user->getRoles())->contains('admin');
verify("first user is not banned", $user->getRoles())->notContains('banned');
Verify::Array($user->getRoles())
->contains('admin', 'first user is admin')
->notContains('banned', 'first user is not banned');

// greater / less
$rate = $user->getRate();
verify('first user rate is 7', $rate)->equals(7);

verify($rate)->greaterThan(5);
verify($rate)->lessThan(10);
verify($rate)->lessOrEquals(7);
verify($rate)->greaterOrEquals(5);
// greater / less
verify($user->getRate())
->greaterThan(5)
->lessThan(10)
->equals(7, 'first user rate is 7');

// true / false / null
verify($user->isAdmin())->true();
Expand All @@ -51,68 +57,71 @@ verify($user->invitedBy)->null();
verify($user->getPosts())->notNull();

// empty
verify($user->getComments())->isEmpty();
verify($user->getComments())->empty();
verify($user->getRoles())->notEmpty();

// throws
verify($callback)->throws();
verify($callback)->throws(Exception::class);
verify($callback)->throws(Exception::class, 'exception message');
verify($callback)->throws(new Exception());
verify($callback)->throws(new Exception('message'));
Verify::Callable($callback)
->throws()
->throws(Exception::class)
->throws(Exception::class, 'exception message')
->throws(new Exception())
->throws(new Exception('message'));

// does not throw
verify($callback)->doesNotThrow();
verify($callback)->throws(Exception::class);
verify($callback)->doesNotThrow(new Exception());
Verify::Callable($callback)
->doesNotThrow()
->throws(Exception::class)
->doesNotThrow(new Exception());

// and many more !
```

> ##### :page_facing_up: See Verifiers full list [here.][7]
Shorthands for testing truth/fallacy:

```php
verify_that($user->isActivated());
verify_not($user->isBanned());
```

These two functions don't check for strict true/false matching, rather `empty` function is used.
`verify_that` checks that result is not empty value, `verify_not` does the opposite.
> :page_facing_up: **See Verifiers full list [here.][7]**
## Alternative Syntax

If you follow TDD/BDD you'd rather use `expect` instead of `verify`. Which are just an alias functions:
If you follow TDD/BDD you'd rather use `expect` or `verify_that` instead of `verify`. Which are just an alias function:

```php
expect("user have 5 posts", $user->getNumPosts())->equals(5);
expect_that($user->isActive());
expect_not($user->isBanned());
expect($user->getNumPosts())->equals(5, 'user have 5 posts');

verify_that($user->getRate())->equals(7, 'first user rate is 7');
```

## Extending

In order to add more assertions you can override `Codeception\Verify` class:
In order to add more assertions you can extend the abstract class `Verify`:

```php
class MyVerify extends \Codeception\Verify {
use Codeception\Verify\Verify;
use PHPUnit\Framework\Assert;

class MyVerify extends Verify {

//you can type $actual to only receive a specific data type

public function __construct($actual = null)
{
parent::__construct($actual);
}

public function success()
public function success(string $message = '')
{
Assert::assertTrue(true, $message);
}

}
```

Set the class name to `Codeception\Verify::$override` property to `verify` function use it:
And use it!

```php
$myVerify = new MyVerify;

\Codeception\Verify::$override = MyVerify::class;
$myVerify->success('it works!');

// access overridden class
verify('it works')->success();
$myVerify::Mixed('this also')->notEquals('works');
```

## License
Expand All @@ -128,3 +137,4 @@ Verify is open-sourced software licensed under the [MIT][8] License.
[6]: http://rspec.info/
[7]: /docs/supported_verifiers.md
[8]: /LICENSE
[9]: /UPGRADE.md
74 changes: 74 additions & 0 deletions UPGRADE.md
@@ -0,0 +1,74 @@
UPGRADE FROM 1.X TO 2.X
=======================


PHP version
------

* Removed support for `PHP 7.1` & `PHP 7.2`.


Verify function
-------

In version `2.x`, `verifiers` can be used as classes. Each verifier class handles a specific type of data.

Thanks to this you can enjoy an autocompletion of your `IDE` much more intelligent than before...

That is why **we remove some global functions** that have a less intuitive behavior.

According to the above:

* `verify` no longer receives a `string $message` as a parameter, now each _**verifier**_ fulfills this function.
* `verify_not` was deleted. Use `verify()->empty` instead.
* `expect_that` and `expect_not` were deleted. Use `expect()->notEmpty` and `expect()->empty` instead.
* `expect_file` and `setIsFileExpectation` were deleted. Use `Verify::File()` instead.

Verifiers
-------

| Verify 1.x | Verify 2.x |
|-------------------------------------------------|-------------------------------------------------|
| `verify()->array` | `verify()->isArray` |
| `verify()->bool` | `verify()->isBool` |
| `verify()->callable` | `verify()->isCallable` |
| `verify()->float` | `verify()->isFloat` |
| `verify()->greaterOrEquals` | `verify()->greaterThanOrEqual` |
| `verify()->int` | `verify()->isInt` |
| `verify()->isEmpty` | `verify()->empty` |
| `verify()->isInstanceOf` | `verify()->instanceOf` |
| `verify()->isNotInstanceOf` | `verify()->notInstanceOf` |
| `verify()->lessOrEquals` | `verify()->lessThanOrEqual` |
| `verify()->notArray` | `verify()->isNotArray` |
| `verify()->notBool` | `verify()->isNotBool` |
| `verify()->notCallable` | `verify()->isNotCallable` |
| `verify()->notFloat` | `verify()->isNotFloat` |
| `verify()->notInt` | `verify()->isNotInt` |
| `verify()->notNumeric` | `verify()->isNotNumeric` |
| `verify()->notObject` | `verify()->isNotObject` |
| `verify()->notResource` | `verify()->isNotResource` |
| `verify()->notScalar` | `verify()->isNotScalar` |
| `verify()->notString` | `verify()->isNotString` |
| `verify()->numeric` | `verify()->isNumeric` |
| `verify()->object` | `verify()->isObject` |
| `verify()->resource` | `verify()->isResource` |
| `verify()->scalar` | `verify()->isScalar` |
| `verify()->string` | `verify()->isString` |
| `verify()->hasAttribute` | `Verify()->baseObjectHasAttribute` |
| `verify()->notHasAttribute` | `Verify()->baseObjectNotHasAttribute` |
| `verify()->throws` | `Verify()->callableThrows` |
| `verify()->doesNotThrow` | `Verify()->callableDoesNotThrow` |
| `verify()->hasStaticAttribute` | `Verify()->classHasStaticAttribute` |
| `verify()->notHasStaticAttribute` | `Verify()->classNotHasStaticAttribute` |
| `verify()->hasAttribute` | `Verify()->classHasAttribute` |
| `verify()->notHasAttribute` | `Verify()->classNotHasAttribute` |
| `verify()->notExists` | `Verify()->fileDoesNotExists` |
| `verify()->regExp` | `Verify()->stringMatchesRegExp` |
| `verify()->notRegExp` | `Verify()->stringDoesNotMatchRegExp` |
| `verify()->notStartsWith` | `Verify()->stringNotStartsWith` |


Extending
-------

* `Codeception\Verify::$override` was removed, extend from abstract `Codeception\Verify\Verify` class instead.
11 changes: 6 additions & 5 deletions composer.json
Expand Up @@ -13,14 +13,15 @@
}
],
"require": {
"php": ">= 7.1",
"php": "^7.3",
"ext-dom": "*",
"phpunit/phpunit": ">= 7.0",
"codeception/phpunit-wrapper": "^7.8.0 | ^8.1.2 | ^9.0.2"
"phpunit/phpunit": "^9.3"
},
"autoload": {
"files": ["src/Codeception/function.php"],
"psr-4":{
"files": [
"src/Codeception/bootstrap.php"
],
"psr-4": {
"Codeception\\": "src\\Codeception"
}
}
Expand Down

0 comments on commit 252fe61

Please sign in to comment.