I have such files:
/src/Api.php
<?php
namespace src;
class Api {
function apiCall()
{
return 'api_result';
}
}
<?php
include __DIR__.'/../vendor/autoload.php'; // composer autoload
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
'debug' => true,
'includePaths' => [__DIR__.'/../src'],
'cacheDir' => __DIR__ . '/aspectCache'
]);
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
settings:
bootstrap: _bootstrap.php
{
"require-dev": {
"codeception/aspect-mock": "*",
"codeception/codeception": "^2.3"
},
"autoload": {
"psr-4": {
"": "src/"
}
}
}
<?php
require_once "vendor/autoload.php";
use src\Api;
$api = new Api();
echo $api->apiCall();
echo 'test';
<?php
use AspectMock\Test as test;
use src\Api;
class FirstCest
{
public function frontpageWorks(AcceptanceTester $I)
{
$I->amOnPage('/');
test::double(Api::class, ['apiCall' => 'mock']);
$I->see('mocktest');
}
}
php codecept.phar run --steps -d
PhpBrowser makes request to your website over HTTP, so the mocks set in the test code have no effect over app code.
The mocks work in unit tests and in functional tests if you are using a framework module.