Giter VIP home page Giter VIP logo

verify's Introduction

Verify

BDD Assertions for PHPUnit or Codeception

Latest Stable Version Total Downloads Build Status License StandWithUkraine

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

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

verify's Issues

PHPStorm autocomplete doesn't work

I added Verify,Specify and Codeception as my External Libraries to my project so i have auto complete in my tests.

when doing for example:

except($something)->

PHPStorm don't provide methods which are in Verify class. Same is happening with verify.

Tag 0.3.2?

Thank you for merging #25. However, VersionEye will not pull license info out of the composer.json file of the dev-master release; it is apparently still looking at the most recent tagged version (0.3.1). Would you mind tagging an 0.3.2 release, so that we can make our VersionEye license report come out clean?

Very minor issue, to be sure, but appreciated if you have a chance.

Soft Assertion Support

Hi,

I've been wondering if we could use verify for soft assertion. It could be really nice to have if it can also do soft assertion for a long test script runs.

Thanks,
Lovie

Call to undefined function verify()

Steps:

  • The composer file:
"require": {
    "codeception/verify": "^0.2.7",
    "codeception/specify": "^0.4.2",
    "codeception/codeception": "^2.1"
}
  • Create a clean directory and run codecept bootstrap.
  • Create any unit test that try to use verify() (inside a specify).
  • The error message: Call to undefined function verify().

How should I use it?

Problem with newest versions

When using this library with phpunit 3.7 and codeception 1.8.7 all the statements must be setted inside a block like

if ($scenario->running()) {
}

because the getter functions are returning a maybe object.

If no change is possible, say it explicitly on the readme file.

PHP 8 support

Could you check if Verify is compatible with PHP 8 and if so update composer package requirements?

Verify/expect steps doesn't shows in codeception report

Hi there.
i'm trying to use 'verify' instead of codeception assertions.
But 'verify/expect' steps doesn't appears in codeception report if they are pass. And it's a bit annoying

Do you have any suggestions how to put them into report?
And of course I'd like to see the description from 'verify/expect' steps into report too.

expect callableNotToThrow does not modify assertion count

When testing that an exception is not thrown, the assertion count is not raised. This is true for both variants on the latest 2.2 release:

verify($callable)->callableDoesNotThrow(\Exception::class);
expect($callable)->callableNotToThrow(\Exception::class);

Here is a sample test:

public function testCallableDoesNotThrow()
{
    $testFunction = function() {};
    expect($testFunction)->callableNotToThrow(\Exception::class);
}

Class alias breaks code

Hi guys,

I followed some open and closed PRs related to class aliasing, which try to handle different PHPUnit versions. But in my case it breaks dev env when using the PHPUnit v4 version >= 4.8.35.

This code already takes care of what you are trying to achieve ...

If I remove the first three lines in function.php everything is working as expected, but I still can't wrap my around what is going on exactly.

I receive the error
PHP Fatal error: Class 'PHPUnit_Framework_Assert' not found in /vendor/phpunit/phpunit/src/ForwardCompatibility/Assert.php on line 16

I guess its because you are autoloading the namespaced version before composer applied the classmapping for the old style class and thus can't load the class you are trying to aliasing.

Maybe you have an idea what exactly is going on and also if your workaround code is still required. Maybe its not a good idea to interfere with applications autoloading in that way?

And not to forget: thanks for building Codeception, its a great tool! :-)

Thanks,
Kevin

lessThen

It should be lessThan. There are already two pull requests, #6 and #10. Please fix this.

Feature Enhancement: Return $actual, $expected, and $description

Currently the code works like this:

if ( ! $this->isFileExpectation ) {
        a::assertEquals($expected, $this->actual, $this->description);
    } else {
        a::assertFileEquals($expected, $this->actual, $this->description);
    }

With void/no return. But I'd like to be able to turn return those three values each time as an array. For example, I call the verify function from the web code on a testing page.

screen shot 2016-11-21 at 12 52 58 pm

I extended Verify and returned those values from each of the different callers. Now I can run tests on demand through the web.

expect()->stringToContainsString

ExpectAny::stringToContainsString* methods is incorrect

class ExpectAny {

...

public function stringToContainString($needle, string $message = ''): self
    {
        Expect::String($this->actual)->notToContainString($needle, $message);
        return $this;
    }

    public function stringToContainStringIgnoringCase($needle, string $message = ''): self
    {
        Expect::String($this->actual)->notToContainStringIgnoringCase($needle, $message);
        return $this;
    }`

Tag new release

Would it be possible to tag a new release soon? I'm most interested in the additional assertions added in this commit: 39b5089 and would prefer to not have to use dev-master in my composer.json file to use them. Thank you!

Warning: Cannot declare class PHPUnit_Framework_Assert

Because of latest commit Fix ClassNotFound for PHPUnit > v6
with included in composer dependency codeception/base v.2.3 I get following warning:

Warning: Cannot declare class PHPUnit_Framework_Assert, because the name is already in use in ../vendor/codeception/base/shim.php on line 17

That happens, because file vendor/codeception/base/shim.php checks only for absence of class PHPUnit_Framework_TestCase, creating ALL of related class aliases for phpunit v.6

namespace {
    if (!class_exists('PHPUnit_Framework_TestCase') && class_exists('PHPUnit\Framework\TestCase')) {
        class_alias('PHPUnit\Framework\Assert', 'PHPUnit_Framework_Assert');
        class_alias('PHPUnit\Framework\AssertionFailedError', 'PHPUnit_Framework_AssertionFailedError');
        class_alias('PHPUnit\Framework\Constraint\Constraint', 'PHPUnit_Framework_Constraint');
        class_alias('PHPUnit\Framework\Constraint\LogicalNot', 'PHPUnit_Framework_Constraint_Not');
        class_alias('PHPUnit\Framework\DataProviderTestSuite', 'PHPUnit_Framework_TestSuite_DataProvider');
        class_alias('PHPUnit\Framework\Exception', 'PHPUnit_Framework_Exception');
        class_alias('PHPUnit\Framework\ExceptionWrapper', 'PHPUnit_Framework_ExceptionWrapper');
        class_alias('PHPUnit\Framework\ExpectationFailedException', 'PHPUnit_Framework_ExpectationFailedException');
        class_alias('PHPUnit\Framework\IncompleteTestError', 'PHPUnit_Framework_IncompleteTestError');
        class_alias('PHPUnit\Framework\SelfDescribing', 'PHPUnit_Framework_SelfDescribing');
        class_alias('PHPUnit\Framework\SkippedTestError', 'PHPUnit_Framework_SkippedTestError');
        class_alias('PHPUnit\Framework\Test', 'PHPUnit_Framework_Test');
        class_alias('PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase');
        class_alias('PHPUnit\Framework\TestFailure', 'PHPUnit_Framework_TestFailure');
        class_alias('PHPUnit\Framework\TestListener', 'PHPUnit_Framework_TestListener');
        class_alias('PHPUnit\Framework\TestResult', 'PHPUnit_Framework_TestResult');
        class_alias('PHPUnit\Framework\TestSuite', 'PHPUnit_Framework_TestSuite');
        class_alias('PHPUnit\Framework\Warning', 'PHPUnit_Framework_Warning');
        class_alias('PHPUnit\Runner\BaseTestRunner', 'PHPUnit_Runner_BaseTestRunner');
        class_alias('PHPUnit\Runner\Filter\Factory', 'PHPUnit_Runner_Filter_Factory');
        class_alias('PHPUnit\Runner\Filter\NameFilterIterator', 'PHPUnit_Runner_Filter_Test');
        class_alias('PHPUnit\Runner\Filter\IncludeGroupFilterIterator', 'PHPUnit_Runner_Filter_Group_Include');
        class_alias('PHPUnit\Runner\Filter\ExcludeGroupFilterIterator', 'PHPUnit_Runner_Filter_Group_Exclude');
        class_alias('PHPUnit\Runner\Version', 'PHPUnit_Runner_Version');
        class_alias('PHPUnit\TextUI\ResultPrinter', 'PHPUnit_TextUI_ResultPrinter');
        class_alias('PHPUnit\TextUI\TestRunner', 'PHPUnit_TextUI_TestRunner');
        class_alias('PHPUnit\Util\Log\JUnit', 'PHPUnit_Util_Log_JUnit');
        class_alias('PHPUnit\Util\Printer', 'PHPUnit_Util_Printer');
        class_alias('PHPUnit\Util\Test', 'PHPUnit_Util_Test');
        class_alias('PHPUnit\Util\TestDox\ResultPrinter', 'PHPUnit_Util_TestDox_ResultPrinter');
        class_alias('PHPUnit\Framework\BaseTestListener', 'PHPUnit_Framework_BaseTestListener');

        require_once __DIR__ . '/phpunit5-loggers.php'; // TAP and JSON loggers were removed in PHPUnit 6
    }
}

Not sure what solutions there can be applied: change check in codeception/base shim.php to look for each class separately, or to make this condition more advanced.

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.