Использовал advanced template, пытаюсь сделать frontend, backend и api с авторизацией oauth2, решил начать с базового тест для oauth2.
Конфиг codeception для теста api примерно такой (functional.suite.yml):
Код: Выделить всё
class_name: ApiTester
modules:
enabled:
- PhpBrowser:
url: http://localhost:8080/api/web
- REST:
depends: PhpBrowser
url: http://localhost:8080/api/web
- Db:
dsn: mysql:host=localhost;dbname=mydb_test
user: mydb_test
password: mydb_test
config:
Yii2:
configFile: '../config/api/functional.php'
Код: Выделить всё
namespace: tests\codeception\api
actor: Tester
paths:
tests: .
log: _output
data: _data
helpers: _support
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
colors: true
memory_limit: 1024M
log: true
config:
# the entry script URL (with host info) for functional and acceptance tests
# PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
test_entry_url: http://localhost:8080/api/web/index-test.php
Код: Выделить всё
use tests\codeception\api\ApiTester;
use Faker\Provider\Internet;
use common\models\User;
$faker = Faker\Factory::create();
// Create test user.
$user = new User();
$user->username = $faker->userName;
$user->email = $faker->email;
$password = $faker->password;
$user->setPassword($password);
$user->save();
/* @var $scenario Codeception\Scenario */
$I = new ApiTester($scenario);
$I->wantTo('acquire access token by password');
$I->seeInDatabase('oauth_clients', [
'client_id' => 'testclient',
'client_secret' => 'testpass',
]);
// Create test oauth2 client
$postData = [
"grant_type" => "password",
"client_id" => "testclient",
"client_secret" => "testpass",
"username" => $user->username,
"password" => $password
];
$I->sendPOST('/oauth2/token', $postData);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContains('"access_token":"');
$I->seeResponseContains('"token_type":"Bearer"');
Есть ли какой-нибудь способ заставить модуль REST у codeception обращаться не в основную базу а в тестовую?
Либо подскажите какой-нибудь костыль, только желательно поэлегантней и поудобней, и желательно без запуска второго веб-сервера