Hi there,
I am starting to write some integration tests for my CakePHP application and was wondering where to put a utility method used for multiple tests.
In my app, nearly every action needs to be authenticated.
In the cookbook, I have read that the session()
method of Cake\TestSuite\IntegrationTestCase
can be use to simulate a logged in user.
Currently, I have created a class App\Test\TestCase\AppIntegrationTestCase
that extends Cake\TestSuite\IntegrationTestCase
and putted my faked login()
function in there.
For example :
<?php
namespace App\Test\TestCase;
use Cake\TestSuite\IntegrationTestCase;
class AppIntegrationTestCase extends IntegrationTestCase
{
public function login()
{
$this->session([
'Auth' => [
'User' => [
'id' => 1,
'username' => 'testing'
]
]
]);
}
}
For my integration tests, I then extend App\Test\TestCase\AppIntegrationTestCase
rather than Cake\TestSuite\IntegrationTestCase
and use defined function. For example
<?php
namespace App\Test\TestCase\Controller;
use App\Controller\PostsController;
use App\Test\TestCase\AppIntegrationTestCase;
class PostsControllerTest extends AppIntegrationTestCase
{
public $fixtures = ['app.posts'];
public function testReadPosts()
{
$this->login();
$this->get('/posts');
$this->assertResponseOk();
$this->assertResponseContains('Latest blog posts');
}
}
It works but, it is the right way to go ?