Skip to content

Commit

Permalink
Make Query take required arguments in its constructor.
Browse files Browse the repository at this point in the history
Calling the repository() method was not optional. Since the dependency
is required it makes the most sense as a constructor argument.
  • Loading branch information
markstory committed Aug 5, 2013
1 parent c6a5ab0 commit 34425c6
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 61 deletions.
9 changes: 9 additions & 0 deletions lib/Cake/ORM/Query.php
Expand Up @@ -90,6 +90,15 @@ class Query extends DatabaseQuery {
*/
protected $_results;

/**
* @param Cake\Database\Connection $connection
* @param Cake\ORM\Table $table
*/
public function __construct($connection, $table) {
$this->connection($connection);
$this->repository($table);
}

/**
* Returns the default table object that will be used by this query,
* that is, the table that will appear in the from clause.
Expand Down
3 changes: 1 addition & 2 deletions lib/Cake/ORM/Table.php
Expand Up @@ -523,8 +523,7 @@ public function findAll(Query $query, array $options = []) {
* @return \Cake\ORM\Query
*/
protected function _buildQuery() {
$query = new Query($this->connection());
return $query->repository($this);
return new Query($this->connection(), $this);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions lib/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -157,7 +157,7 @@ public function testPivotWithDefaultTableName() {
* @return void
*/
public function testAttachTo() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
Expand Down Expand Up @@ -207,7 +207,7 @@ public function testAttachTo() {
* @return void
*/
public function testAttachToNoFields() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
Expand Down Expand Up @@ -263,7 +263,7 @@ public function testEagerLoader() {
]);
$association = new BelongsToMany('Tag', $config);
$keys = [1, 2, 3, 4];
$query = $this->getMock('Cake\ORM\Query', ['execute', 'contain'], [null]);
$query = $this->getMock('Cake\ORM\Query', ['execute', 'contain'], [null, null]);
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
Expand Down Expand Up @@ -320,7 +320,7 @@ public function testEagerLoaderWithDefaults() {
$association = new BelongsToMany('Tag', $config);
$keys = [1, 2, 3, 4];
$methods = ['execute', 'contain', 'where', 'order'];
$query = $this->getMock('Cake\ORM\Query', $methods, [null]);
$query = $this->getMock('Cake\ORM\Query', $methods, [null, null]);
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
Expand Down Expand Up @@ -372,7 +372,7 @@ public function testEagerLoaderWithOverrides() {
$association = new BelongsToMany('Tag', $config);
$keys = [1, 2, 3, 4];
$methods = ['execute', 'contain', 'where', 'order', 'select'];
$query = $this->getMock('Cake\ORM\Query', $methods, [null]);
$query = $this->getMock('Cake\ORM\Query', $methods, [null, null]);
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
Expand Down Expand Up @@ -441,7 +441,7 @@ public function testEagerLoaderFieldsException() {
$association = new BelongsToMany('Tag', $config);
$keys = [1, 2, 3, 4];
$methods = ['execute', 'contain', 'where', 'order', 'select'];
$query = $this->getMock('Cake\ORM\Query', $methods, [null]);
$query = $this->getMock('Cake\ORM\Query', $methods, [null, null]);
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$query->expects($this->any())->method('contain')->will($this->returnSelf());
Expand Down Expand Up @@ -474,14 +474,14 @@ public function testEagerLoaderSubquery() {
]
]);
$association = new BelongsToMany('Tag', $config);
$parent = (new Query(null))
$parent = (new Query(null, null))
->join(['foo' => ['table' => 'foo', 'type' => 'inner', 'conditions' => []]])
->join(['bar' => ['table' => 'bar', 'type' => 'left', 'conditions' => []]]);

$query = $this->getMock(
'Cake\ORM\Query',
['execute', 'where', 'andWhere', 'order', 'select', 'contain'],
[null]
[null, null]
);

$this->tag->expects($this->once())->method('find')->with('all')
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Test/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -75,7 +75,7 @@ public function testCanBeJoined() {
* @return void
*/
public function testAttachTo() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'company_id',
'sourceTable' => $this->client,
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testAttachTo() {
* @return void
*/
public function testAttachToConfigOverride() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'company_id',
'sourceTable' => $this->client,
Expand Down Expand Up @@ -140,7 +140,7 @@ public function testAttachToConfigOverride() {
* @return void
*/
public function testAttachToNoFields() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->client,
'targetTable' => $this->company,
Expand Down
18 changes: 9 additions & 9 deletions lib/Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -108,7 +108,7 @@ public function testEagerLoader() {
];
$association = new HasMany('Article', $config);
$keys = [1, 2, 3, 4];
$query = $this->getMock('Cake\ORM\Query', ['execute'], [null]);
$query = $this->getMock('Cake\ORM\Query', ['execute'], [null, null]);
$this->article->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
Expand Down Expand Up @@ -152,7 +152,7 @@ public function testEagerLoaderWithDefaults() {
$query = $this->getMock(
'Cake\ORM\Query',
['execute', 'where', 'andWhere', 'order'],
[null]
[null, null]
);
$this->article->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
Expand Down Expand Up @@ -197,7 +197,7 @@ public function testEagerLoaderWithOverrides() {
$query = $this->getMock(
'Cake\ORM\Query',
['execute', 'where', 'andWhere', 'order', 'select', 'contain'],
[null]
[null, null]
);
$this->article->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
Expand Down Expand Up @@ -262,7 +262,7 @@ public function testEagerLoaderFieldsException() {
$query = $this->getMock(
'Cake\ORM\Query',
['execute'],
[null]
[null, null]
);
$this->article->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
Expand All @@ -281,14 +281,14 @@ public function testEagerLoaderSubquery() {
'targetTable' => $this->article,
];
$association = new HasMany('Article', $config);
$parent = (new Query(null))
$parent = (new Query(null, null))
->join(['foo' => ['table' => 'foo', 'type' => 'inner', 'conditions' => []]])
->join(['bar' => ['table' => 'bar', 'type' => 'left', 'conditions' => []]]);

$query = $this->getMock(
'Cake\ORM\Query',
['execute', 'where', 'andWhere', 'order', 'select', 'contain'],
[null]
[null, null]
);

$this->article->expects($this->once())->method('find')->with('all')
Expand Down Expand Up @@ -340,7 +340,7 @@ public function testEagerLoaderSubquery() {
* @return void
*/
public function testAttachTo() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
Expand Down Expand Up @@ -371,7 +371,7 @@ public function testAttachTo() {
* @return void
*/
public function testAttachToConfigOverride() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
Expand Down Expand Up @@ -405,7 +405,7 @@ public function testAttachToConfigOverride() {
* @return void
*/
public function testAttachToNoFields() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Test/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -75,7 +75,7 @@ public function testCanBeJoined() {
* @return void
*/
public function testAttachTo() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'user_id',
'sourceTable' => $this->user,
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testAttachTo() {
* @return void
*/
public function testAttachToConfigOverride() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'user_id',
'sourceTable' => $this->user,
Expand Down Expand Up @@ -142,7 +142,7 @@ public function testAttachToConfigOverride() {
* @return void
*/
public function testAttachToNoFields() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->user,
'targetTable' => $this->profile,
Expand Down

0 comments on commit 34425c6

Please sign in to comment.