Skip to content

Commit

Permalink
Adding test for belongsToMany and fixing small bug when instantiating
Browse files Browse the repository at this point in the history
association target tables
  • Loading branch information
lorenzo committed Jun 9, 2013
1 parent 4e9e635 commit ead92ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
12 changes: 3 additions & 9 deletions lib/Cake/ORM/Association.php
Expand Up @@ -156,10 +156,6 @@ public function __construct($name, array $options = []) {
$this->_name = $name;
$this->_options($options);

if (empty($this->_className)) {
$this->_className = $this->_name;
}

if (empty($this->_property)) {
$this->property($name);
}
Expand Down Expand Up @@ -213,11 +209,9 @@ public function target(Table $table = null) {
return $this->_targetTable = $table;
}

if ($table === null && $this->_className !== null) {
$this->_targetTable = Table::build(
$this->_name,
['className' => $this->_className]
);
if ($table === null) {
$className = $this->_className;
$this->_targetTable = Table::build($this->_name, compact('className'));
}
return $this->_targetTable;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -149,7 +149,7 @@ public function attachTo(Query $query, array $options = []) {

/**
* Return false as join conditions are defined in the pivot table
*
*
* @param array $options list of options passed to attachTo method
* @return boolean false
*/
Expand Down
46 changes: 46 additions & 0 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -146,13 +146,24 @@ public function testConfigAndBuild() {
$this->assertEquals(array_keys($schema), $table->schema()->columns());
}

/**
* Tests getting and setting a Table instance in the registry
*
* @return void
*/
public function testInstance() {
$this->assertNull(Table::instance('things'));
$table = new Table(['table' => 'things']);
Table::instance('things', $table);
$this->assertSame($table, Table::instance('things'));
}

/**
* Tests that all fields for a table are added by default in a find when no
* other fields are specified
*
* @return void
*/
public function testFindAllNoFields() {
$this->_createThingsTable();
$table = new Table(['table' => 'things', 'connection' => $this->connection]);
Expand All @@ -164,6 +175,11 @@ public function testFindAllNoFields() {
$this->assertSame($expected, $results);
}

/**
* Tests that it is possible to select only a few fields when finding over a table
*
* @return void
*/
public function testFindAllSomeFields() {
$this->_createThingsTable();
$table = new Table(['table' => 'things', 'connection' => $this->connection]);
Expand All @@ -182,6 +198,12 @@ public function testFindAllSomeFields() {
$this->assertSame($expected, $results);
}

/**
* Tests that the query will automatically casts complex conditions to the correct
* types when the columns belong to the default table
*
* @return void
*/
public function testFindAllConditionAutoTypes() {
$this->_createDatesTable();
$table = new Table(['table' => 'dates', 'connection' => $this->connection]);
Expand Down Expand Up @@ -257,4 +279,28 @@ public function testHasMany() {
$this->assertSame($table, $hasMany->source());
}

/**
* Tests that BelongsToMany() creates and configures correctly the association
*
* @return void
*/
public function testBelongsToMany() {
$options = [
'foreignKey' => 'thing_id',
'joinTable' => 'things_tags',
'conditions' => ['b' => 'c'],
'sort' => ['foo' => 'asc']
];
$table = new Table(['table' => 'authors']);
$belongsToMany = $table->belongsToMany('tag', $options);
$this->assertInstanceOf('\Cake\ORM\Association\BelongsToMany', $belongsToMany);
$this->assertSame($belongsToMany, $table->association('tag'));
$this->assertEquals('tag', $belongsToMany->name());
$this->assertEquals('thing_id', $belongsToMany->foreignKey());
$this->assertEquals(['b' => 'c'], $belongsToMany->conditions());
$this->assertEquals(['foo' => 'asc'], $belongsToMany->sort());
$this->assertSame($table, $belongsToMany->source());
$this->assertSame('things_tags', $belongsToMany->pivot()->table());
}

}

0 comments on commit ead92ba

Please sign in to comment.