Skip to content

Commit

Permalink
More new array syntax for fun and fixing an E_STRICT error
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 5, 2012
1 parent 8cee7d1 commit 1dbc4f4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Model/Datasource/Database/Type.php
Expand Up @@ -144,7 +144,7 @@ public function toDatabase($value, Driver $driver) {
* @param Driver $driver object from which database preferences and configuration will be extracted
* @return mixed
**/
public function toPHP($value, $driver) {
public function toPHP($value, Driver $driver) {
if (!empty(self::$_basicTypes[$this->_name])) {
$typeInfo = self::$_basicTypes[$this->_name];
$value = ($value === null) ? null : $value;
Expand All @@ -162,7 +162,7 @@ public function toPHP($value, $driver) {
* @param Driver $driver object from which database preferences and configuration will be extracted
* @return mixed
**/
public function toStatement($value, $driver) {
public function toStatement($value, Driver $driver) {
if (!empty(self::$_basicTypes[$this->_name])) {
$typeInfo = self::$_basicTypes[$this->_name];
return isset($typeInfo['pdo']) ? $typeInfo['pdo'] : PDO::PARAM_STR;
Expand Down
6 changes: 4 additions & 2 deletions lib/Cake/Model/Datasource/Database/Type/DateType.php
Expand Up @@ -2,18 +2,20 @@

namespace Cake\Model\Datasource\Database\Type;

use Cake\Model\Datasource\Database\Driver;

use \DateTime;

class DateType extends \Cake\Model\Datasource\Database\Type {

public function toDatabase($value, $driver) {
public function toDatabase($value, Driver $driver) {
if (is_string($value)) {
return $value;
}
return $value->format('Y-m-d');
}

public function toPHP($value, $driver) {
public function toPHP($value, Driver $driver) {
if ($value === null) {
return null;
}
Expand Down
70 changes: 35 additions & 35 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/ConnectionTest.php
Expand Up @@ -54,7 +54,7 @@ public function testConnect() {
* @return void
**/
public function testMissingDriver() {
$connection = new Connection(array('datasource' => '\Foo\InvalidDriver'));
$connection = new Connection(['datasource' => '\Foo\InvalidDriver']);
}

/**
Expand All @@ -64,8 +64,8 @@ public function testMissingDriver() {
* @return void
**/
public function testDisabledDriver() {
$mock = $this->getMock('\Cake\Model\Datasource\Database\Connection\Driver', array('enabled'), array(), 'DriverMock');
$connection = new Connection(array('datasource' => get_class($mock)));
$mock = $this->getMock('\Cake\Model\Datasource\Database\Connection\Driver', ['enabled'], [], 'DriverMock');
$connection = new Connection(['datasource' => get_class($mock)]);
}

/**
Expand All @@ -75,7 +75,7 @@ public function testDisabledDriver() {
* @return void
**/
public function testWrongCredentials() {
$connection = new Connection(array('database' => 'foobar') + Configure::read('Connections.test'));
$connection = new Connection(['database' => 'foobar'] + Configure::read('Connections.test'));
$connection->connect();
}

Expand Down Expand Up @@ -110,22 +110,22 @@ public function testPrepare() {
**/
public function testExecuteWithArguments() {
$sql = 'SELECT 1 + ?';
$statement = $this->connection->execute($sql, array(1));
$statement = $this->connection->execute($sql, [1]);
$this->assertCount(1, $statement);
$result = $statement->fetch();
$this->assertEquals(array(2), $result);
$this->assertEquals([2], $result);

$sql = 'SELECT 1 + ? + ? AS total';
$statement = $this->connection->execute($sql, array(2, 3));
$statement = $this->connection->execute($sql, [2, 3]);
$this->assertCount(1, $statement);
$result = $statement->fetch('assoc');
$this->assertEquals(array('total' => 6), $result);
$this->assertEquals(['total' => 6], $result);

$sql = 'SELECT 1 + :one + :two AS total';
$statement = $this->connection->execute($sql, array('one' => 2, 'two' => 3));
$statement = $this->connection->execute($sql, ['one' => 2, 'two' => 3]);
$this->assertCount(1, $statement);
$result = $statement->fetch('assoc');
$this->assertEquals(array('total' => 6), $result);
$this->assertEquals(['total' => 6], $result);
}

/**
Expand All @@ -135,15 +135,15 @@ public function testExecuteWithArguments() {
**/
public function testExecuteWithArgumentsAndTypes() {
$sql = 'SELECT ?';
$statement = $this->connection->execute($sql, array(new \DateTime('2012-01-01')), array('date'));
$statement = $this->connection->execute($sql, [new \DateTime('2012-01-01')], ['date']);
$result = $statement->fetch();
$this->assertEquals('2012-01-01', $result[0]);

$sql = 'SELECT ?, ?, ?';
$params = array(new \DateTime('2012-01-01 10:10:10'), '2000-01-01 10:10:10', 1.1);
$statement = $this->connection->execute($sql, $params, array('date', 'string', 'float'));
$params = [new \DateTime('2012-01-01 10:10:10'), '2000-01-01 10:10:10', 1.1];
$statement = $this->connection->execute($sql, $params, ['date', 'string', 'float']);
$result = $statement->fetch();
$this->assertEquals(array('2012-01-01', '2000-01-01 10:10:10', 1.1), $result);
$this->assertEquals(['2012-01-01', '2000-01-01 10:10:10', 1.1], $result);
}

/**
Expand All @@ -154,7 +154,7 @@ public function testExecuteWithArgumentsAndTypes() {
**/
public function testExecuteWithMissingType() {
$sql = 'SELECT ?';
$statement = $this->connection->execute($sql, array(new \DateTime('2012-01-01')), array('bar'));
$statement = $this->connection->execute($sql, [new \DateTime('2012-01-01')], ['bar']);
}

/**
Expand All @@ -167,7 +167,7 @@ public function testExecuteWithNoParams() {
$statement = $this->connection->execute($sql);
$result = $statement->fetch();
$this->assertCount(1, $result);
$this->assertEquals(array(1), $result);
$this->assertEquals([1], $result);
}

/**
Expand All @@ -178,11 +178,11 @@ public function testExecuteWithNoParams() {
public function testInsertWithMatchingTypes() {
$table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
$this->connection->execute($table);
$data = array('id' => '1', 'title' => 'a title', 'body' => 'a body');
$data = ['id' => '1', 'title' => 'a title', 'body' => 'a body'];
$result = $this->connection->insert(
'things',
$data,
array('id' => 'integer', 'title' => 'string', 'body' => 'string')
['id' => 'integer', 'title' => 'string', 'body' => 'string']
);
$this->assertInstanceOf('Cake\Model\Datasource\Database\Statement', $result);
$result = $this->connection->execute('SELECT * from things');
Expand All @@ -199,11 +199,11 @@ public function testInsertWithMatchingTypes() {
public function testInsertWithPositionalTypes() {
$table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
$this->connection->execute($table);
$data = array('id' => '1', 'title' => 'a title', 'body' => 'a body');
$data = ['id' => '1', 'title' => 'a title', 'body' => 'a body'];
$result = $this->connection->insert(
'things',
$data,
array('integer', 'string', 'string')
['integer', 'string', 'string']
);
$this->assertInstanceOf('Cake\Model\Datasource\Database\Statement', $result);
$result = $this->connection->execute('SELECT * from things');
Expand All @@ -220,11 +220,11 @@ public function testInsertWithPositionalTypes() {
protected function _insertTwoRecords() {
$table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
$this->connection->execute($table);
$data = array('id' => '1', 'title' => 'a title', 'body' => 'a body');
$data = ['id' => '1', 'title' => 'a title', 'body' => 'a body'];
$result = $this->connection->insert(
'things',
$data,
array('id' => 'integer', 'title' => 'string', 'body' => 'string')
['id' => 'integer', 'title' => 'string', 'body' => 'string']
);

$result->bindValue(1, '2', 'integer');
Expand Down Expand Up @@ -264,8 +264,8 @@ public function testUpdateWithoutConditionsNorTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
$body = 'changed the body!';
$this->connection->update('things', array('title' => $title, 'body' => $body));
$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', array($title, $body));
$this->connection->update('things', ['title' => $title, 'body' => $body]);
$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
$this->assertCount(2, $result);
}

Expand All @@ -278,8 +278,8 @@ public function testUpdateWithConditionsNoTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
$body = 'changed the body!';
$this->connection->update('things', array('title' => $title, 'body' => $body), array('id' => 2));
$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', array($title, $body));
$this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2]);
$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
$this->assertCount(1, $result);
}

Expand All @@ -292,8 +292,8 @@ public function testUpdateWithConditionsCombinedNoTypes() {
$this->_insertTwoRecords();
$title = 'changed the title!';
$body = 'changed the body!';
$this->connection->update('things', array('title' => $title, 'body' => $body), array('id' => 2, 'body is not null'));
$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', array($title, $body));
$this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2, 'body is not null']);
$result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
$this->assertCount(1, $result);
}

Expand All @@ -307,8 +307,8 @@ public function testUpdateWithTypes() {
$title = 'changed the title!';
$body = new \DateTime('2012-01-01');
$values = compact('title', 'body');
$this->connection->update('things', $values, array(), array('body' => 'date'));
$result = $this->connection->execute('SELECT * FROM things WHERE title = :title AND body = :body', $values, array('body' => 'date'));
$this->connection->update('things', $values, [], ['body' => 'date']);
$result = $this->connection->execute('SELECT * FROM things WHERE title = :title AND body = :body', $values, ['body' => 'date']);
$this->assertCount(2, $result);
$row = $result->fetch('assoc');
$this->assertEquals('2012-01-01', $row['body']);
Expand All @@ -326,8 +326,8 @@ public function testUpdateWithConditionsAndTypes() {
$title = 'changed the title!';
$body = new \DateTime('2012-01-01');
$values = compact('title', 'body');
$this->connection->update('things', $values, array('id' => '1-string-parsed-as-int'), array('body' => 'date', 'id' => 'integer'));
$result = $this->connection->execute('SELECT * FROM things WHERE title = :title AND body = :body', $values, array('body' => 'date'));
$this->connection->update('things', $values, ['id' => '1-string-parsed-as-int'], ['body' => 'date', 'id' => 'integer']);
$result = $this->connection->execute('SELECT * FROM things WHERE title = :title AND body = :body', $values, ['body' => 'date']);
$this->assertCount(1, $result);
$row = $result->fetch('assoc');
$this->assertEquals('2012-01-01', $row['body']);
Expand All @@ -352,15 +352,15 @@ public function testDeleteNoConditions() {
**/
public function testDeleteWithConditions() {
$this->_insertTwoRecords();
$this->connection->delete('things', array('id' => '1-rest-is-ommited'), array('id' => 'integer'));
$this->connection->delete('things', ['id' => '1-rest-is-ommited'], ['id' => 'integer']);
$result = $this->connection->execute('SELECT * FROM things');
$this->assertCount(1, $result);

$this->connection->delete('things', array('id' => '1-rest-is-ommited'), array('id' => 'integer'));
$this->connection->delete('things', ['id' => '1-rest-is-ommited'], ['id' => 'integer']);
$result = $this->connection->execute('SELECT * FROM things');
$this->assertCount(1, $result);

$this->connection->delete('things', array('id' => '2-rest-is-ommited'), array('id' => 'integer'));
$this->connection->delete('things', ['id' => '2-rest-is-ommited'], ['id' => 'integer']);
$result = $this->connection->execute('SELECT * FROM things');
$this->assertCount(0, $result);
}
Expand Down

0 comments on commit 1dbc4f4

Please sign in to comment.