Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active November 29, 2017 14:24
Show Gist options
  • Save danilobatistaqueiroz/fccf3c192851b2125ed9b497bf22d4cf to your computer and use it in GitHub Desktop.
Save danilobatistaqueiroz/fccf3c192851b2125ed9b497bf22d4cf to your computer and use it in GitHub Desktop.
Starting with mockery

Configuring Mockery

create the file: composer.json

{
    "require-dev": {
 	"mockery/mockery": "dev-master",
	"phpunit/phpunit": "6.4"
    }
}

run: composer install


Running the first test with Mockery

create the test file MockeryTest.php inside the test folder:

<?php

//Filename: MockeryTest.php
 
use PHPUnit\Framework\TestCase;
 
class MockeryTest extends TestCase {
 
    protected function tearDown() {
        \Mockery::close();
    }
  
    function testMockeryWorks() {
        $mock = \Mockery::mock('AClassToBeMocked');
        $mock->shouldReceive('someMethod')->once();
 
        $workerObject = new AClassToWorkWith;
        $workerObject->doSomethingWit($mock);
    }
}
 
class AClassToBeMocked {}
 
class AClassToWorkWith {
 
    function doSomethingWit($anotherClass) {
        return $anotherClass->someMethod();
    }
}

You can list the available tests:

./vendor/bin/phpunit --color --list-tests test

Now you can run the test:

./vendor/bin/phpunit --color test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment