Skip to content

Commit

Permalink
Clean up test a bit more.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 27, 2017
1 parent 128aa65 commit 65b9aea
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions tests/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -34,6 +34,11 @@ class HasOneTest extends TestCase
*/
public $fixtures = ['core.users', 'core.profiles'];

/**
* @var bool
*/
protected $listenerCalled = false;

/**
* Set up
*
Expand All @@ -44,6 +49,7 @@ public function setUp()
parent::setUp();
$this->user = TableRegistry::get('Users');
$this->profile = TableRegistry::get('Profiles');
$this->listenerCalled = false;
}

/**
Expand Down Expand Up @@ -298,31 +304,28 @@ public function testAttachToBeforeFind()
*/
public function testAttachToBeforeFindExtraOptions()
{
$query = $this->getMockBuilder('\Cake\ORM\Query')
->setMethods(['join', 'select'])
->setConstructorArgs([null, null])
->getMock();
$config = [
'foreignKey' => 'user_id',
'sourceTable' => $this->user,
'targetTable' => $this->profile,
];
$listener = $this->getMockBuilder('stdClass')
->setMethods(['__invoke'])
->getMock();
$this->profile->eventManager()->attach($listener, 'Model.beforeFind');
$association = new HasOne('Profiles', $config);
$this->listenerCalled = false;
$opts = new \ArrayObject(['something' => 'more']);
$listener->expects($this->once())->method('__invoke')
->with(
$this->isInstanceOf('\Cake\Event\Event'),
$this->isInstanceOf('\Cake\ORM\Query'),
$opts,
false
);
$this->profile->eventManager()->on(
'Model.beforeFind',
function ($event, $query, $options, $primary) use ($opts) {
$this->listenerCalled = true;
$this->assertInstanceOf('\Cake\Event\Event', $event);
$this->assertInstanceOf('\Cake\ORM\Query', $query);
$this->assertEquals($options, $opts);
$this->assertFalse($primary);
});
$association = new HasOne('Profiles', $config);
$query = $this->user->find();
$association->attachTo($query, ['queryBuilder' => function ($q) {
return $q->applyOptions(['something' => 'more']);
}]);
$this->assertTrue($this->listenerCalled, 'Event not fired');
}

/**
Expand Down

0 comments on commit 65b9aea

Please sign in to comment.