-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a test for IndexAction and LookpupAction
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |