Extended test tools for CakePHP
app/Plugin/CakeExtendTest/Test/AppCakeTestCase.php
to app/Test
app/Plugin/CakeExtendTest/Test/AppControllerTestCase.php
to app/Test
app/Plugin/CakeExtendTest/Test/AllTestsTest.php
to app/Test/Case
AppCake TestCase.php
and AppController TestCase.php
files.cake CakeExtendTest.bake
. Analog
of command cake bake test
.Create a new object for testing:
$proxy = $this->createProxyObject($target);
$proxy->someProtectedMethod();
where:
$target
- target object;$proxy
- new object for testing.Create View test on controller test:
$opt = [
'method' => 'GET',
'return' => 'contents',
];
$url = 'some_controller/some_action/some_param';
$view = $this->testAction($url, $opt);
$numTableRows = $this->getNumberItemsByCssSelector($view, 'div#content div.container table > tbody > tr');
$expected = 2;
$this->assertData($expected, $numTableRows);
app/Plugin/CakeExtendTest/Test/TestConfig.php
to app/Test
.Edit config file and configure application for testing, e.g.:
$config['TestKey'] = ['SomeKey' => 'Some data...'];
app/Plugin/CakeExtendTest/Test/AppTestTrait.php
to app/Test
.Edit file AppTestTrait.php
. e.g.:
/**
* Information about the logged in user.
*
* @var array
*/
protected $userInfo = [
'user' => 'Хвощинский В.В.',
'role' => USER_ROLE_USER,
'prefix' => '',
'id' => '7',
'includedFields' => [
CAKE_LDAP_LDAP_ATTRIBUTE_OBJECT_GUID => '8c149661-7215-47de-b40e-35320a1ea508'
]
];
Add call setDefaultUserInfo()
in method setUp()
of test:
/**
* setUp method
*
* @return void
*/
public function setUp() {
$this->setDefaultUserInfo($this->userInfo);
parent::setUp();
....
}
For change user role on fly:
$userInfo = [
'role' => USER_ROLE_USER | USER_ROLE_ADMIN,
'prefix' => 'admin',
];
$this->applyUserInfo($userInfo);
AppCakeTestCase.php
and AppControllerTestCase.php
:
App::uses('AppTestTrait', 'CakeExtendTest.Test');
to App::uses('AppTestTrait', 'Test');
In controller test method add:
$this->testAction('/some_controller/some_action', $opt);
$this->checkFlashMessage('Action completed successfully');
$params = [
[
null, // $id
], // Params for step 1
[
100, // $id
], // Params for step 2
[
2, // $id
], // Params for step 3
];
$expected = [
false, // Result of step 1
[], // Result of step 2
[
'some data'
], // Result of step 3
];
$this->runClassMethodGroup('methodName', $params, $expected);