Skip to content

Commit

Permalink
Improving and unit testing displayField method
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Sep 15, 2013
1 parent 74ed36c commit 70a6d81
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 12 deletions.
50 changes: 40 additions & 10 deletions lib/Cake/ORM/Table.php
Expand Up @@ -95,6 +95,13 @@ class Table {
*/
protected $_primaryKey = 'id';

/**
* The name of the field that represents a human readable representation of a row
*
* @var string
*/
protected $_displayField;

/**
* The list of associations for this table. Indexed by association name,
* values are Association object instances.
Expand Down Expand Up @@ -338,6 +345,28 @@ public function primaryKey($key = null) {
return $this->_primaryKey;
}

/**
* Returns the display field or sets a new one
*
* @param string $key sets a new name to be used as display field
* @return string
*/
public function displayField($key = null) {
if ($key !== null) {
$this->_displayField = $key;
}
if ($this->_displayField === null) {
$schema = $this->schema();
if ($schema->column('title')) {
$this->_displayField = 'title';
}
if ($schema->column('name')) {
$this->_displayField = 'name';
}
}
return $this->_displayField;
}

/**
* Returns a association objected configured for the specified alias if any
*
Expand Down Expand Up @@ -536,19 +565,20 @@ public function findAll(Query $query, array $options = []) {
* @return \Cake\ORM\Query
*/
public function findList(Query $query, array $options = []) {
$columns = [];
$mapper = function($key, $row, $mapReduce) use (&$columns) {
if (empty($columns)) {
$columns = array_slice(array_keys($row), 0, 3);
}

list($rowKey, $rowVal) = $columns;
if (!isset($columns[2])) {
$options += [
'idField' => $this->primaryKey(),
'valueField' => $this->displayField(),
'groupField' => false
];
$mapper = function($key, $row, $mapReduce) use ($options) {
$rowKey = $options['idField'];
$rowVal = $options['valueField'];
if (!($options['groupField'])) {
$mapReduce->emit($row[$rowVal], $row[$rowKey]);
return;
}

$key = $row[$columns[2]];
$key = $row[$options['groupField']];
$mapReduce->emitIntermediate($key, [$row[$rowKey] => $row[$rowVal]]);
};

Expand Down Expand Up @@ -603,7 +633,7 @@ public function findThreaded(Query $query, array $options = []) {
$mapReduce->emit($row->getArrayCopy());
});
}

return $query;
}

Expand Down
109 changes: 107 additions & 2 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -190,6 +190,72 @@ public function testPrimaryKey() {
$this->assertEquals('thingID', $table->primaryKey());
}

/**
* Tests that name will be selected as a displayField
*
* @return void
*/
public function testDisplayFieldName() {
$table = new Table([
'table' => 'users',
'schema' => [
'foo' => ['type' => 'string'],
'name' => ['type' => 'string']
]
]);
$this->assertEquals('name', $table->displayField());
}

/**
* Tests that title will be selected as a displayField
*
* @return void
*/
public function testDisplayFieldTitle() {
$table = new Table([
'table' => 'users',
'schema' => [
'foo' => ['type' => 'string'],
'title' => ['type' => 'string']
]
]);
$this->assertEquals('title', $table->displayField());
}

/**
* Tests that no displayField will fallback to primary key
*
* @return void
*/
public function testDisplayFallback() {
$table = new Table([
'table' => 'users',
'schema' => [
'id' => ['type' => 'string'],
'foo' => ['type' => 'string']
]
]);
$this->assertEquals('id', $table->displayField());
}

/**
* Tests that displayField can be changed
*
* @return void
*/
public function testDisplaySet() {
$table = new Table([
'table' => 'users',
'schema' => [
'id' => ['type' => 'string'],
'foo' => ['type' => 'string']
]
]);
$this->assertEquals('id', $table->displayField());
$table->displayField('foo');
$this->assertEquals('foo', $table->displayField());
}

/**
* Tests schema method
*
Expand Down Expand Up @@ -514,7 +580,9 @@ public function testFindApplyOptions() {
*/
public function testFindList() {
$table = new Table(['table' => 'users', 'connection' => $this->connection]);
$query = $table->find('list', ['fields' => ['id', 'username']])->order('id');
$table->displayField('username');
$query = $table->find('list', ['fields' => ['id', 'username']])
->order('id');
$expected = [
1 => 'mariano',
2 => 'nate',
Expand All @@ -523,7 +591,7 @@ public function testFindList() {
];
$this->assertSame($expected, $query->toArray());

$query = $table->find('list')
$query = $table->find('list', ['groupField' => 'odd'])
->select(['id', 'username', 'odd' => 'id % 2 = 0'])
->order('id');
$expected = [
Expand Down Expand Up @@ -678,4 +746,41 @@ public function testFindThreadedHydrated() {
$this->assertEquals($expected, $results[0]->children[0]->children[1]->toArray());
}

/**
* Tests find('list') with hydrated records
*
* @return void
*/
public function testFindListHydrated() {
$table = new Table(['table' => 'users', 'connection' => $this->connection]);
$table->displayField('username');
$query = $table
->find('list', ['fields' => ['id', 'username']])
->hydrate(true)
->order('id');
$expected = [
1 => 'mariano',
2 => 'nate',
3 => 'larry',
4 => 'garrett'
];
$this->assertSame($expected, $query->toArray());

$query = $table->find('list', ['groupField' => 'odd'])
->select(['id', 'username', 'odd' => 'id % 2 = 0'])
->hydrate(true)
->order('id');
$expected = [
0 => [
1 => 'mariano',
3 => 'larry'
],
1 => [
2 => 'nate',
4 => 'garrett'
]
];
$this->assertSame($expected, $query->toArray());
}

}

0 comments on commit 70a6d81

Please sign in to comment.