Skip to content

Commit

Permalink
Adding a test for IndexAction and LookpupAction
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 26, 2015
1 parent 43d3d67 commit feca39b
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/App/Controller/BlogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BlogsController extends Controller
'Crud.Edit',
'Crud.View',
'Crud.Delete',
'Crud.Lookup',
'deleteAll' => [
'className' => 'Crud.Bulk/Delete',
],
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Action/IndexActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,25 @@ function ($event) {
$this->assertNotNull($this->viewVariable('items'));
$this->assertNotNull($this->viewVariable('success'));
}

/**
* Tests that it is posisble to modify the pagination query in beforePaginate
*
* @return void
*/
public function testModifyQueryInEvent()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
['priority' => 1000],
function () {
$this->_controller->Crud->on('beforePaginate', function ($event) {
$event->subject->query->where(['id <' => 2]);
});
}
);

$this->get('/blogs');
$this->assertContains('Page 1 of 1, showing 1 records out of 1 total', $this->_response->body());
}
}
107 changes: 107 additions & 0 deletions tests/TestCase/Action/LookupActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
namespace Crud\Test\TestCase\Action;

use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Crud\TestSuite\IntegrationTestCase;

/**
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
*/
class LookupActionTest extends IntegrationTestCase
{

/**
* fixtures property
*
* @var array
*/
public $fixtures = ['plugin.crud.blogs'];

/**
* Test with no extra options
*
* @return void
*/
public function testGet()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
['priority' => 1000],
function () {
$this->_subscribeToEvents($this->_controller);
}
);

$expected = [
'1' => '1st post',
'2' => '2nd post',
'3' => '3rd post',
];

$this->get('/blogs/lookup.json');
$this->assertEvents(['beforeLookup', 'afterLookup', 'beforeRender']);
$this->assertNotNull($this->viewVariable('viewVar'));
$this->assertNotNull($this->viewVariable('blogs'));
$this->assertNotNull($this->viewVariable('success'));
$this->assertEquals($expected, $this->viewVariable('blogs')->toArray());
}

/**
* Test changing the id field and value field
*
* @return void
*/
public function testGetWithCustomParams()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
['priority' => 1000],
function () {
$this->_subscribeToEvents($this->_controller);
}
);

$expected = [
'1st post' => '1',
'2nd post' => '2',
'3rd post' => '3',
];

$this->get('/blogs/lookup.json?id=name&value=id');
$this->assertEvents(['beforeLookup', 'afterLookup', 'beforeRender']);
$this->assertNotNull($this->viewVariable('viewVar'));
$this->assertNotNull($this->viewVariable('blogs'));
$this->assertNotNull($this->viewVariable('success'));
$this->assertEquals($expected, $this->viewVariable('blogs')->toArray());
}


/**
* Tests that the beforeLookup can be used to modify the query
*
* @return void
*/
public function testGetWithQueryModification()
{
$this->_eventManager->on(
'Dispatcher.beforeDispatch',
['priority' => 1000],
function () {
$this->_controller->Crud->on('beforeLookup', function ($event) {
$event->subject->query->where(['id <' => 2]);
});
}
);

$expected = [
'1' => '1st post',
];

$this->get('/blogs/lookup.json');
$this->assertNotNull($this->viewVariable('viewVar'));
$this->assertEquals($expected, $this->viewVariable('blogs')->toArray());
}
}

0 comments on commit feca39b

Please sign in to comment.