Skip to content

Commit

Permalink
Add SetValue TestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed May 4, 2015
1 parent 7ae28d1 commit ae64cba
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/App/Controller/BlogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class BlogsController extends Controller
'className' => 'Crud.Bulk/Toggle',
'field' => 'is_active',
],
'activateAll' => [
'className' => 'Crud.Bulk/SetValue',
'field' => 'is_active',
'value' => true,
],
],
'listeners' => [
'Crud.Api',
Expand Down
134 changes: 134 additions & 0 deletions tests/TestCase/Action/Bulk/SetValueActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
namespace Crud\Test\TestCase\Action\Bulk;

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 SetValueActionTest extends IntegrationTestCase
{

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

/**
* Table class to mock on
*
* @var string
*/
public $tableClass = 'Crud\Test\App\Model\Table\BlogsTable';

/**
* Data provider with all HTTP verbs
*
* @return array
*/
public function allHttpMethodProvider()
{
return [
['post'],
['put'],
];
}

/**
* Test the normal HTTP flow for all HTTP verbs
*
* @dataProvider allHttpMethodProvider
* @return void
*/
public function testAllRequestMethods($method)
{
$this->_eventManager->attach(
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);

$this->_controller->Flash
->expects($this->once())
->method('set')
->with(
'Set value successfully',
[
'element' => 'default',
'params' => ['class' => 'message success', 'original' => 'Set value successfully'],
'key' => 'flash'
]
);

$this->_subscribeToEvents($this->_controller);
},
'Dispatcher.beforeDispatch',
['priority' => 1000]
);

$this->{$method}('/blogs/activateAll', [
'id' => [
1 => 1,
2 => 2,
],
]);

$this->assertEvents(['beforeBulk', 'afterBulk', 'setFlash', 'beforeRedirect']);
$this->assertTrue($this->_subject->success);
$this->assertRedirect('/blogs');
}

/**
* Test the flow when the beforeDelete event is stopped
*
* @return void
*/
public function testStopDelete()
{
$this->_eventManager->attach(
function ($event) {
$this->_controller->Flash = $this->getMock(
'Cake\Controller\Component\Flash',
['set']
);

$this->_controller->Flash
->expects($this->once())
->method('set')
->with(
'Could not set value',
[
'element' => 'default',
'params' => ['class' => 'message error', 'original' => 'Could not set value'],
'key' => 'flash'
]
);

$this->_subscribeToEvents($this->_controller);

$this->_controller->Crud->on('beforeBulk', function ($event) {
$event->stopPropagation();
});
},
'Dispatcher.beforeDispatch',
['priority' => 1000]
);

$this->post('/blogs/activateAll', [
'id' => [
1 => 1,
2 => 2,
],
]);

$this->assertEvents(['beforeBulk', 'setFlash', 'beforeRedirect']);
$this->assertFalse($this->_subject->success);
$this->assertRedirect('/blogs');
}
}

0 comments on commit ae64cba

Please sign in to comment.