Last active
December 28, 2015 23:49
-
-
Save fightbulc/7581305 to your computer and use it in GitHub Desktop.
Setup Testing with Composer, Codeception, PHPUnit, AspectMock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$kernel = \AspectMock\Kernel::getInstance(); | |
$kernel->init([ | |
'debug' => TRUE, | |
// root of the app | |
'appDir' => __DIR__ . '/..', | |
// cached php files | |
'cacheDir' => __DIR__ . '/_data/cache', | |
// apply aspect on files in these directories | |
'includePaths' => [ | |
__DIR__ . '/../app', | |
__DIR__ . '/../vendor', | |
], | |
// exclude aspect (e.g. test files) | |
'excludePaths' => [ | |
__DIR__, | |
], | |
// intercept global functions e.g. time() | |
'interceptFunctions' => TRUE, | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# we are in: /tests/Test | |
namespace Test; | |
use App\User; | |
use App\UserService; | |
use AspectMock\Test; | |
class UserServiceTest extends \PHPUnit_Framework_TestCase | |
{ | |
protected function _after() | |
{ | |
// remove all registered test doubles | |
Test::clean(); | |
} | |
public function testCreateUserByName() | |
{ | |
// create class proxy | |
$user = Test::double('App\User', ['save' => FALSE]); | |
/** | |
* App\User instance will be created here | |
* Calls: setName($name), save() | |
* Normally save() returns TRUE | |
* | |
* @return save() // returns FALSE since we overridden it | |
*/ | |
$response = (new UserService())->createUserByName('tino'); | |
// class proxy lets us know if this happened | |
$user->verifyInvoked('save'); | |
// should succeed | |
$this->assertFalse($response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment