Skip to content

Instantly share code, notes, and snippets.

@fightbulc
Last active December 28, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fightbulc/7581305 to your computer and use it in GitHub Desktop.
Save fightbulc/7581305 to your computer and use it in GitHub Desktop.
Setup Testing with Composer, Codeception, PHPUnit, AspectMock
<?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,
]);
<?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