GithubHelp home page GithubHelp logo

verify's Introduction

Verify

BDD Assertions for PHPUnit or Codeception

Latest Stable Version Total Downloads Build Status License

This is very tiny wrapper for PHPUnit assertions, that are aimed to make tests a bit more readable. With BDD assertions influenced by Chai, Jasmine, and RSpec your assertions would be a bit closer to natural language.

⚠️ This is the Verify 2.0 documentation, to see v1.x docs click here.

Installation

Requires PHP 7.4 or higher

composer require codeception/verify --dev

⬆️ Upgrade from 1.x by following the upgrade guide.

Usage

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

use Codeception\Verify\Verify;

$user = User::find(1);

// equals
verify($user->getName())->equals('davert');

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

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


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

// true / false / null
verify($user->isAdmin())->true();
verify($user->isBanned())->false();
verify($user->invitedBy)->null();
verify($user->getPosts())->notNull();

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

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

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

// and many more !

📄 See Verifiers full list here.

Alternative Syntax

If you follow TDD/BDD you'd rather use expect instead of verify:

expect($user->getNumPosts())
    ->notToBeNull()
    ->toBeInt()
    ->toEqual(5, 'user have 5 posts');

📄 See Expectations full list here.

Or verify_that which is just an alias function:

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

Extending

In order to add more assertions you can extend the abstract class 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(string $message = '')
    {
        Assert::assertTrue(true, $message);
    }

}

And use it!

$myVerify = new MyVerify;

$myVerify->success('it works!');

$myVerify::Mixed('this also')->notEquals('works');

License

Verify is open-sourced software licensed under the MIT License. © Codeception PHP Testing Framework

verify's People

Contributors

asppsa avatar blle-dxo avatar cmoralesweb avatar davertmik avatar ddinchev avatar glensc avatar grahamcampbell avatar greg-1-anderson avatar naktibalda avatar ragazzo avatar ricpelo avatar sergeyklay avatar stevekr avatar tavoniievez avatar vetrinus avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.