GithubHelp home page GithubHelp logo

mockerymodule's Introduction

Stand With Ukraine

Codeception

Latest Stable Total Downloads StandWithUkraine Build status

Modern PHP Testing for everyone

Codeception is a modern full-stack testing framework for PHP. Inspired by BDD, it provides an absolutely new way of writing acceptance, functional and even unit tests.

Contributions

At Codeception we are glad to receive contributions from the community. If you want to send additions or fixes to the code or the documentation please check the Contributing guide.

At a Glance

Describe what you test and how you test it. Use PHP to write descriptions faster.

Run tests and see what actions were taken and what results were seen.

Sample test

$I->amOnPage('/');
$I->click('Pages');
$I->click('New');
$I->see('New Page');
$I->submitForm('form#new_page', ['title' => 'Movie Review']);
$I->see('page created'); // notice generated
$I->see('Movie Review','h1'); // head of page of is our title
$I->seeInCurrentUrl('pages/movie-review'); // slug is generated
$I->seeInDatabase('pages', ['title' => 'Movie Review']); // data is stored in database

For unit testing you can stay on classic PHPUnit tests, as Codeception can run them too.

Installation

Composer

php composer.phar require "codeception/codeception"

TODO: Document how to install the modules, e.g.

php composer.phar require "codeception/module-phpbrowser"

Phar

Download codecept.phar

Copy it into your project.

You can also make Codeception an executable and it put it into your $PATH, for instance:

wget https://codeception.com/codecept.phar

chmod +x codecept.phar

sudo mv codecept.phar /usr/local/bin/codecept

You can then run Codecept in the command line using: codecept bootstrap, codecept run, etc

Run CLI utility:

php codecept.phar

See also Installation | QuickStart

Getting Started

After you successfully installed Codeception, run this command:

codecept bootstrap

This will create a default directory structure and default test suites.

Documentation

View Documentation

The documentation source files can be found at https://github.com/Codeception/codeception.github.com/tree/master/docs/.

License

MIT

mockerymodule's People

Contributors

akimsko avatar arhell avatar davertmik avatar enumag avatar grahamcampbell avatar gsokol avatar kornrunner avatar naktibalda avatar yesdevnull 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mockerymodule's Issues

How can I mock hard dependencies with Mockery and Codeception?

According to the Mockery docs, I can mock hard dependencies by doing e.g.:

$mock = m::mock('overload:SomeNamespace\SomeClass');

However, this only works if the mocked class is not loaded somewhere else in the test suite.

Quoting from the docs:

To make this possible, we’ll tell PHPUnit to run the tests that have overloaded classes in separate processes and to not preserve global state. That way we’ll avoid having the overloaded class included more than once. Of course this has its downsides as these tests will run slower.

This means adding the @runTestsInSeparateProcesses and @preserveGlobalState disabled annotations. When I did so, I got this error:

PHPUnit_Framework_Exception: PHP Fatal error:  Class 'Codeception\TestCase\Test' not found in [snip]
Fatal error: Class 'Codeception\TestCase\Test' not found in [snip]

After some searching, I found this GitHub issue, which says that Codeception doesn't support the @runTestsInSeparateProcesses annotation.

Is there any way around this, or some other way to mock hard dependencies?

unsatisfied expectation terminates suite

When mockery expectations fail, the currently running suite terminates because of exception thrown. I think the good solution would be to catch Mockery\Exception\* and fail the current test only.

Tell me how to fail the current test from the place where Mockery::close() is called and I'll prepare a pull request. \Codeception\Module::fail() terminates the suite completely?

Fully automate the module without requiring changes to tests

Currently using this module requires changes in each test.
I have found an ugly way to alleviate that problem.

The code below was tested for Cest type tests only. It should not break for any other test type but might simply not work.

use Codeception\Lib\Di;
use Codeception\Module;
use Codeception\TestInterface;

class Mockery extends Module
{


    public function _before(TestInterface $test)
    {
        $meta = $test->getMetadata();
        $rc = new \ReflectionClass($meta);
        $prop = $rc->getProperty('services');
        $prop->setAccessible(true);
        $services = $prop->getValue($meta);
        /** @var Di $di */
        $di = $services['di'];

        $services['di'] = new class($di, $test) extends Di {
            public function __construct(Di $fallback, private readonly TestInterface $test)
            {
                parent::__construct($fallback);

            }


            public function injectDependencies(object $object, string $injectMethodName = self::DEFAULT_INJECT_METHOD_NAME, array $defaults = []): void
            {
                if ($object === $this->test) {
                    parent::injectDependencies(...func_get_args());
                    return;
                }
                // We check if the object is our test, if so we close mockery after.

                codecept_debug(['CALLING',
                    get_class($object),
                    $injectMethodName
                ]);
                try {
                    parent::injectDependencies(...func_get_args());
                    \Mockery::close();
                } catch (\Throwable $t) {
                    \Mockery::resetContainer();
                    throw $t;
                }
                \Mockery::close();
            }
        };
        $prop->setValue($meta, $services);
    }
}

Essentially we hijack the Di container and replace it with a custom implementation. Based on the fact that for Cest files the actual test is ran like this:

$this->getMetadata()->getService('di')->injectDependencies($this->testInstance, $methodName, $context);

We abuse this to insert a mockery closure after calling the test method.

While I'm happy to make a PR for this, I'm not sure it should be in an "official" repository. I probably don't have time to implement a proper fix where we add an event to allow modules to fail a test before it is finished.

Can't mock object methods with boolen return types

Unable to mock any methods that have a boolean return type. Need to do more research to see if it's breaking on other primitives.

Example:

public function toBeTested() : bool
{
   return true;
}

The mocked version does not include the bool return type and breaks.

Why dev Mockery?

I'd like to use the recently released stable Mockery 0.9 but this package doesn't allow me to do that because of this in composer.json.

    "require": {
        "mockery/mockery": "dev-master@dev"
    },

Is there a reason for it? Just @dev would be enough in my opinion.

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.