Skip to content

Commit

Permalink
php5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Sep 30, 2015
1 parent e5c0eb2 commit 45d3765
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 57 deletions.
10 changes: 5 additions & 5 deletions src/Controller/Component/AjaxComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ 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.
*
* @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;
$config += $defaults;
parent::__construct($collection, $config);
}

public function initialize(array $config = array()) {
public function initialize(array $config = []) {
if (!$this->_config['autoDetect']) {
return;
}
Expand Down Expand Up @@ -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']);
}
Expand Down
10 changes: 5 additions & 5 deletions src/View/AjaxView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
];

}
53 changes: 34 additions & 19 deletions tests/TestCase/Controller/Component/AjaxComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*/
class AjaxComponentTest extends TestCase {

public $fixtures = array(
public $fixtures = [
'core.Sessions'
);
];

public function setUp() {
parent::setUp();
Expand Down Expand Up @@ -62,7 +62,7 @@ public function testDefaults() {
'message' => 'A message',
'key' => 'flash',
'element' => 'Flash/custom',
'params' => array()
'params' => []
]
];
$this->assertEquals($expected, $session);
Expand All @@ -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']);
}

Expand All @@ -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);
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -190,33 +190,48 @@ 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);

$this->assertNotEmpty($this->Controller->viewVars);
$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() {

}

}
73 changes: 54 additions & 19 deletions tests/TestCase/View/AjaxViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]', 'description' => 'Content one'),
array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => '[email protected]', 'description' => 'Content two'),
);
$items = [
['title' => 'Title One', 'link' => 'http://example.org/one', 'author' => '[email protected]', 'description' => 'Content one'],
['title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => '[email protected]', '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);
}
Expand All @@ -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' => '[email protected]', 'description' => 'Content one'),
array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => '[email protected]', 'description' => 'Content two'),
);
$items = [
['title' => 'Title One', 'link' => 'http://example.org/one', 'author' => '[email protected]', 'description' => 'Content one'],
['title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => '[email protected]', '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);
}
Expand All @@ -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' => '[email protected]', 'description' => 'Content one'),
array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => '[email protected]', 'description' => 'Content two'),
);
$items = [
['title' => 'Title One', 'link' => 'http://example.org/one', 'author' => '[email protected]', 'description' => 'Content one'],
['title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => '[email protected]', '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);
}
Expand All @@ -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() {

}

}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Cake\Core\Configure::write('Config', [
'adminEmail' => '[email protected]',
'adminName' => 'Mark']);
Cake\Mailer\Email::config('default', array('transport' => 'Debug'));
Cake\Mailer\Email::config('default', ['transport' => 'Debug']);
Cake\Mailer\Email::configTransport('Debug', [
'className' => 'Debug'
]);
Expand Down

0 comments on commit 45d3765

Please sign in to comment.