diff --git a/src/Controller/Component/AjaxComponent.php b/src/Controller/Component/AjaxComponent.php index b89c7313..e0c4e60c 100644 --- a/src/Controller/Component/AjaxComponent.php +++ b/src/Controller/Component/AjaxComponent.php @@ -31,12 +31,12 @@ class AjaxComponent extends Component { public $respondAsAjax = false; - protected $_defaultConfig = array( + protected $_defaultConfig = [ 'viewClass' => 'Ajax.Ajax', 'autoDetect' => true, 'resolveRedirect' => true, 'flashKey' => 'Flash.flash' // Use "messages" for Tools plugin Flash component, set to false to disable - ); + ]; /** * Constructor. @@ -44,7 +44,7 @@ class AjaxComponent extends Component { * @param ComponentRegistry $collection * @param array $config */ - public function __construct(ComponentRegistry $collection, $config = array()) { + public function __construct(ComponentRegistry $collection, $config = []) { $this->Controller = $collection->getController(); $defaults = (array)Configure::read('Ajax') + $this->_defaultConfig; @@ -52,7 +52,7 @@ public function __construct(ComponentRegistry $collection, $config = array()) { parent::__construct($collection, $config); } - public function initialize(array $config = array()) { + public function initialize(array $config = []) { if (!$this->_config['autoDetect']) { return; } @@ -110,7 +110,7 @@ public function beforeRedirect(Event $event, $url, Response $response) { $this->Controller->autoRender = true; $this->Controller->set('_redirect', compact('url', 'status')); - $serializeKeys = array('_redirect', '_message'); + $serializeKeys = ['_redirect', '_message']; if (!empty($this->Controller->viewVars['_serialize'])) { $serializeKeys = array_merge($serializeKeys, $this->Controller->viewVars['_serialize']); } diff --git a/src/View/AjaxView.php b/src/View/AjaxView.php index 9d204d95..8f819637 100644 --- a/src/View/AjaxView.php +++ b/src/View/AjaxView.php @@ -26,10 +26,10 @@ class AjaxView extends View { * * @var array */ - protected $_passedVars = array( + protected $_passedVars = [ 'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name', 'theme', 'layoutPath', 'plugin', 'passedArgs', 'subDir', 'template', 'templatePath' - ); + ]; /** * The subdirectory. AJAX views are always in ajax. @@ -64,7 +64,7 @@ public function __construct( if ($this->subDir === null) { $this->subDir = 'ajax'; - $this->templatePath = str_replace(DS . 'json', '', $this->templatePath); + //$this->templatePath = str_replace(DS . 'json', '', $this->templatePath); } if (isset($response)) { @@ -84,10 +84,10 @@ public function __construct( * @return string The rendered view. */ public function render($view = null, $layout = null) { - $response = array( + $response = [ 'error' => null, 'content' => null, - ); + ]; if (!empty($this->viewVars['error'])) { $view = false; diff --git a/tests/Fixture/UsersFixture.php b/tests/Fixture/UsersFixture.php index 913b77d3..a5057645 100644 --- a/tests/Fixture/UsersFixture.php +++ b/tests/Fixture/UsersFixture.php @@ -13,24 +13,24 @@ class UsersFixture extends TestFixture { * * @var array */ - public $fields = array( + public $fields = [ 'id' => ['type' => 'integer'], 'name' => ['type' => 'string', 'null' => false], 'password' => ['type' => 'string', 'null' => false], 'role_id' => ['type' => 'integer', 'null' => true], '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]] - ); + ]; /** * Records property * * @var array */ - public $records = array( - array('id' => 1, 'role_id' => 1, 'password' => '123456', 'name' => 'User 1'), - array('id' => 2, 'role_id' => 2, 'password' => '123456', 'name' => 'User 2'), - array('id' => 3, 'role_id' => 1, 'password' => '123456', 'name' => 'User 3'), - array('id' => 4, 'role_id' => 3, 'password' => '123456', 'name' => 'User 4') - ); + public $records = [ + ['id' => 1, 'role_id' => 1, 'password' => '123456', 'name' => 'User 1'], + ['id' => 2, 'role_id' => 2, 'password' => '123456', 'name' => 'User 2'], + ['id' => 3, 'role_id' => 1, 'password' => '123456', 'name' => 'User 3'], + ['id' => 4, 'role_id' => 3, 'password' => '123456', 'name' => 'User 4'] + ]; } diff --git a/tests/TestCase/Controller/Component/AjaxComponentTest.php b/tests/TestCase/Controller/Component/AjaxComponentTest.php index 2ae44080..2ab4f688 100644 --- a/tests/TestCase/Controller/Component/AjaxComponentTest.php +++ b/tests/TestCase/Controller/Component/AjaxComponentTest.php @@ -17,9 +17,9 @@ */ class AjaxComponentTest extends TestCase { - public $fixtures = array( + public $fixtures = [ 'core.Sessions' - ); + ]; public function setUp() { parent::setUp(); @@ -62,7 +62,7 @@ public function testDefaults() { 'message' => 'A message', 'key' => 'flash', 'element' => 'Flash/custom', - 'params' => array() + 'params' => [] ] ]; $this->assertEquals($expected, $session); @@ -77,12 +77,12 @@ public function testDefaults() { $this->assertNull($session); $this->Controller->redirect('/'); - $this->assertSame(array(), $this->Controller->response->header()); + $this->assertSame([], $this->Controller->response->header()); - $expected = array( + $expected = [ 'url' => Router::url('/', true), 'status' => 302, - ); + ]; $this->assertEquals($expected, $this->Controller->viewVars['_redirect']); } @@ -97,7 +97,7 @@ public function testAutoDetectOnFalse() { $this->Controller = new AjaxComponentTestController(new Request(), new Response()); $this->Controller->components()->unload('Ajax'); - $this->Controller->components()->load('Ajax.Ajax', array('autoDetect' => false)); + $this->Controller->components()->load('Ajax.Ajax', ['autoDetect' => false]); $this->Controller->startupProcess(); $this->assertFalse($this->Controller->components()->Ajax->respondAsAjax); @@ -142,9 +142,9 @@ public function testToolsMultiMessages() { $this->Controller->components()->Flash->message('A message', 'success'); $session = $this->Controller->request->session()->read('FlashMessage'); - $expected = array( - 'success' => array('A message') - ); + $expected = [ + 'success' => ['A message'] + ]; $this->assertEquals($expected, $session); $event = new Event('Controller.beforeRender'); @@ -169,9 +169,9 @@ public function testSetVars() { $this->Controller->components()->unload('Ajax'); - $content = array('id' => 1, 'title' => 'title'); + $content = ['id' => 1, 'title' => 'title']; $this->Controller->set(compact('content')); - $this->Controller->set('_serialize', array('content')); + $this->Controller->set('_serialize', ['content']); $this->Controller->components()->load('Ajax.Ajax'); $this->assertNotEmpty($this->Controller->viewVars); @@ -190,21 +190,21 @@ public function testSetVarsWithRedirect() { $this->Controller = new AjaxComponentTestController(new Request(), new Response()); $this->Controller->startupProcess(); - $content = array('id' => 1, 'title' => 'title'); + $content = ['id' => 1, 'title' => 'title']; $this->Controller->set(compact('content')); - $this->Controller->set('_serialize', array('content')); + $this->Controller->set('_serialize', ['content']); // Let's try a permanent redirect $this->Controller->redirect('/', 301); - $this->assertSame(array(), $this->Controller->response->header()); + $this->assertSame([], $this->Controller->response->header()); - $expected = array( + $expected = [ 'url' => Router::url('/', true), 'status' => 301, - ); + ]; $this->assertEquals($expected, $this->Controller->viewVars['_redirect']); - $this->Controller->set(array('_message' => 'test')); + $this->Controller->set(['_message' => 'test']); $this->Controller->redirect('/'); $this->assertArrayHasKey('_message', $this->Controller->viewVars); @@ -212,11 +212,26 @@ public function testSetVarsWithRedirect() { $this->assertNotEmpty($this->Controller->viewVars['_serialize']); $this->assertTrue(in_array('content', $this->Controller->viewVars['_serialize'])); } + + public function testAjaxRendering() { + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; + + + } } // Use Controller instead of AppController to avoid conflicts class AjaxComponentTestController extends Controller { - public $components = array('Ajax.Ajax'); + public $components = ['Ajax.Ajax']; + + /** + * A test action + * + * @return void + */ + public function myTest() { + + } } diff --git a/tests/TestCase/View/AjaxViewTest.php b/tests/TestCase/View/AjaxViewTest.php index aedc648a..e0a0ba9a 100644 --- a/tests/TestCase/View/AjaxViewTest.php +++ b/tests/TestCase/View/AjaxViewTest.php @@ -49,16 +49,16 @@ public function setUp() { public function testSerialize() { $Request = new Request(); $Response = new Response(); - $items = array( - array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'), - array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'), - ); + $items = [ + ['title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'], + ['title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'], + ]; $View = new AjaxView($Request, $Response); - $View->set(array('items' => $items, '_serialize' => array('items'))); + $View->set(['items' => $items, '_serialize' => ['items']]); $result = $View->render(false); $this->assertSame('application/json', $Response->type()); - $expected = array('error' => null, 'content' => null, 'items' => $items); + $expected = ['error' => null, 'content' => null, 'items' => $items]; $expected = json_encode($expected); $this->assertTextEquals($expected, $result); } @@ -71,17 +71,17 @@ public function testSerialize() { public function testRenderWithSerialize() { $Request = new Request(); $Response = new Response(); - $items = array( - array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'), - array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'), - ); + $items = [ + ['title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'], + ['title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'], + ]; $View = new AjaxView($Request, $Response); - $View->set(array('items' => $items, '_serialize' => 'items')); + $View->set(['items' => $items, '_serialize' => 'items']); $View->viewPath = 'Items'; $result = $View->render('index'); $this->assertSame('application/json', $Response->type()); - $expected = array('error' => null, 'content' => 'My Index Test ctp', 'items' => $items); + $expected = ['error' => null, 'content' => 'My Index Test ctp', 'items' => $items]; $expected = json_encode($expected); $this->assertTextEquals($expected, $result); } @@ -94,17 +94,17 @@ public function testRenderWithSerialize() { public function testError() { $Request = new Request(); $Response = new Response(); - $items = array( - array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'), - array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'), - ); + $items = [ + ['title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'], + ['title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'], + ]; $View = new AjaxView($Request, $Response); - $View->set(array('error' => 'Some message', 'items' => $items, '_serialize' => array('error', 'items'))); + $View->set(['error' => 'Some message', 'items' => $items, '_serialize' => ['error', 'items']]); $View->viewPath = 'Items'; $result = $View->render('index'); $this->assertSame('application/json', $Response->type()); - $expected = array('error' => 'Some message', 'content' => null, 'items' => $items); + $expected = ['error' => 'Some message', 'content' => null, 'items' => $items]; $expected = json_encode($expected); $this->assertTextEquals($expected, $result); } @@ -124,9 +124,44 @@ public function testWithoutSubdir() { $result = $View->render('index'); $this->assertSame('application/json', $Response->type()); - $expected = array('error' => null, 'content' => 'My Index Test ctp'); + $expected = ['error' => null, 'content' => 'My Index Test ctp']; $expected = json_encode($expected); $this->assertTextEquals($expected, $result); } + public function _testRender() { + $Request = new Request(); + $Response = new Response(); + $Controller = new AjaxComponentTestController($Request, $Response); + + $Controller->viewBuilder()->className('Ajax.Ajax'); + $Controller->viewBuilder()->template('myTest'); + $Controller->viewBuilder()->templatePath('AjaxComponentTest'); + $Controller->myTest(); + + //$Controller->subDir = false; + $result = $Controller->render(); + + $this->assertSame('application/json', $Response->type()); + $expected = ['error' => null, 'content' => 'My Index Test ctp']; + $expected = json_encode($expected); + $this->assertTextEquals($expected, $result); + } + +} + +// Use Controller instead of AppController to avoid conflicts +class AjaxComponentTestController extends Controller { + + public $components = ['Ajax.Ajax']; + + /** + * A test action + * + * @return void + */ + public function myTest() { + + } + } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 818c437e..4c88b6d2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -33,7 +33,7 @@ Cake\Core\Configure::write('Config', [ 'adminEmail' => 'test@example.com', 'adminName' => 'Mark']); -Cake\Mailer\Email::config('default', array('transport' => 'Debug')); +Cake\Mailer\Email::config('default', ['transport' => 'Debug']); Cake\Mailer\Email::configTransport('Debug', [ 'className' => 'Debug' ]);