PHP code example of codeception / aspect-mock

1. Go to this page and download the library: Download codeception/aspect-mock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codeception / aspect-mock example snippets

 php


function testTableName()
{
	$this->assertSame('users', UserModel::tableName());	
	$userModel = test::double('UserModel', ['tableName' => 'my_users']);
	$this->assertSame('my_users', UserModel::tableName());
	$userModel->verifyInvoked('tableName');	
}
 php


class UserService {
    function createUserByName($name) {
    	$user = new User;
    	$user->setName($name);
    	$user->save();
    }
}
 php


function testUserCreate()
{
	$user = test::double('User', ['save' => null]);
	$service = new UserService;
	$service->createUserByName('davert');
	$this->assertSame('davert', $user->getName());
	$user->verifyInvoked('save');
}
 php


// User extends ActiveRecord
function testUserCreate()
{
	$AR = test::double('ActiveRecord', ['save' => null]));
	test::double('User', ['findByNameAndEmail' => new User(['name' => 'jon'])])); 
	$user = User::findByNameAndEmail('jon','[email protected]'); // magic method
	$this->assertSame('jon', $user->getName());
	$user->save(['name' => 'miles']); // ActiveRecord->save did not hit database
	$AR->verifyInvoked('save');
	$this->assertSame('miles', $user->getName());
}
 php


namespace demo;
test::func('demo', 'time', 'now');
$this->assertSame('now', time());
 php


function testSimpleStubAndMock()
{
	$user = test::double(new User, ['getName' => 'davert']);
	$this->assertSame('davert', $user->getName());
	$user->verifyInvoked('getName');
	$user->verifyInvokedOnce('getName');
	$user->verifyNeverInvoked('setName');
	$user->verifyInvokedMultipleTimes('setName',1);
}
 php

$user->verifyMethodInvoked('setName', ['davert']);

php composer.phar update
 php


 \AspectMock\Kernel::getInstance();
$kernel->init([
    'debug' => true,
    '
 php


 \AspectMock\Kernel::getInstance();
$kernel->init([
    'debug' => true,
    '
 php


include __DIR__.'/../vendor/autoload.php'; // composer autoload

$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
    'debug' => true,
    'includePaths' => [__DIR__.'/../src']
]);
 php


$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
    'appDir'    => __DIR__ . '/../../',
    'cacheDir'  => '/tmp/myapp',
    '
 xml
<phpunit bootstrap="bootstrap.php" backupGlobals="false">
 php


use AspectMock\Test as test;

class UserTest extends \PHPUnit_Framework_TestCase
{
    protected function tearDown()
    {
        test::clean(); // remove all registered test doubles
    }

    public function testDoubleClass()
    {
        $user = test::double('demo\UserModel', ['save' => null]);
        \demo\UserModel::tableName();
        \demo\UserModel::tableName();
        $user->verifyInvokedMultipleTimes('tableName',2);
    }
 php


namespace Codeception\Module;

class CodeHelper extends \Codeception\Module
{
	function _after(\Codeception\TestCase $test)
	{
		\AspectMock\Test::clean();
	}
}