| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\ORM;
- use Cake\ORM\Behavior;
- use Cake\TestSuite\TestCase;
- /**
- * Test Stub.
- */
- class TestBehavior extends Behavior {
- /**
- * Test for event bindings.
- */
- public function beforeFind() {
- }
- }
- /**
- * Test Stub.
- */
- class Test2Behavior extends Behavior {
- /**
- * Test for event bindings.
- */
- public function beforeFind() {
- }
- /**
- * Test finder
- */
- public function findFoo() {
- }
- /**
- * Test method
- */
- public function doSomething() {
- }
- }
- /**
- * Test3Behavior
- */
- class Test3Behavior extends Behavior {
- /**
- * Test for event bindings.
- */
- public function beforeFind() {
- }
- /**
- * Test finder
- */
- public function findFoo() {
- }
- /**
- * Test method
- */
- public function doSomething() {
- }
- /**
- * Test method to ensure it is ignored as a callable method.
- */
- public function verifyConfig() {
- return parent::verifyConfig();
- }
- /**
- * implementedEvents
- *
- * This class does pretend to implement beforeFind
- *
- * @return void
- */
- public function implementedEvents() {
- return ['Model.beforeFind' => 'beforeFind'];
- }
- /**
- * implementedFinders
- */
- public function implementedFinders() {
- }
- /**
- * implementedMethods
- */
- public function implementedMethods() {
- }
- /**
- * Expose protected method for testing
- *
- * Since this is public - it'll show up as callable which is a side-effect
- *
- * @return array
- */
- public function testReflectionCache() {
- return $this->_reflectionCache();
- }
- }
- /**
- * Behavior test case
- */
- class BehaviorTest extends TestCase {
- /**
- * Test the side effects of the constructor.
- *
- * @return void
- */
- public function testConstructor() {
- $table = $this->getMock('Cake\ORM\Table');
- $config = ['key' => 'value'];
- $behavior = new TestBehavior($table, $config);
- $this->assertEquals($config, $behavior->config());
- }
- public function testReflectionCache() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test3Behavior($table);
- $expected = [
- 'finders' => [
- 'foo' => 'findFoo'
- ],
- 'methods' => [
- 'doSomething' => 'doSomething',
- 'testReflectionCache' => 'testReflectionCache'
- ]
- ];
- $this->assertEquals($expected, $behavior->testReflectionCache());
- }
- /**
- * Test the default behavior of implementedEvents
- *
- * @return void
- */
- public function testImplementedEvents() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new TestBehavior($table);
- $expected = [
- 'Model.beforeFind' => 'beforeFind'
- ];
- $this->assertEquals($expected, $behavior->implementedEvents());
- }
- /**
- * Test that implementedEvents uses the priority setting.
- *
- * @return void
- */
- public function testImplementedEventsWithPriority() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new TestBehavior($table, ['priority' => 10]);
- $expected = [
- 'Model.beforeFind' => [
- 'priority' => 10,
- 'callable' => 'beforeFind'
- ]
- ];
- $this->assertEquals($expected, $behavior->implementedEvents());
- }
- /**
- * testImplementedMethods
- *
- * @return void
- */
- public function testImplementedMethods() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table);
- $expected = [
- 'doSomething' => 'doSomething'
- ];
- $this->assertEquals($expected, $behavior->implementedMethods());
- }
- /**
- * testImplementedMethodsAliased
- *
- * @return void
- */
- public function testImplementedMethodsAliased() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table, [
- 'implementedMethods' => [
- 'aliased' => 'doSomething'
- ]
- ]);
- $expected = [
- 'aliased' => 'doSomething'
- ];
- $this->assertEquals($expected, $behavior->implementedMethods());
- }
- /**
- * testImplementedMethodsDisabled
- *
- * @return void
- */
- public function testImplementedMethodsDisabled() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table, [
- 'implementedMethods' => []
- ]);
- $expected = [];
- $this->assertEquals($expected, $behavior->implementedMethods());
- }
- /**
- * testImplementedFinders
- *
- * @return void
- */
- public function testImplementedFinders() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table);
- $expected = [
- 'foo' => 'findFoo'
- ];
- $this->assertEquals($expected, $behavior->implementedFinders());
- }
- /**
- * testImplementedFindersAliased
- *
- * @return void
- */
- public function testImplementedFindersAliased() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table, [
- 'implementedFinders' => [
- 'aliased' => 'findFoo'
- ]
- ]);
- $expected = [
- 'aliased' => 'findFoo'
- ];
- $this->assertEquals($expected, $behavior->implementedFinders());
- }
- /**
- * testImplementedFindersDisabled
- *
- * @return void
- */
- public function testImplementedFindersDisabled() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table, [
- 'implementedFinders' => []
- ]);
- $expected = [];
- $this->assertEquals($expected, $behavior->implementedFinders());
- }
- /**
- * testVerifyConfig
- *
- * Don't expect an exception to be thrown
- *
- * @return void
- */
- public function testVerifyConfig() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table);
- $behavior->verifyConfig();
- $this->assertTrue(true, 'No exception thrown');
- }
- /**
- * testVerifyConfigImplementedFindersOverriden
- *
- * Simply don't expect an exception to be thrown
- *
- * @return void
- */
- public function testVerifyConfigImplementedFindersOverriden() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table, [
- 'implementedFinders' => [
- 'aliased' => 'findFoo'
- ]
- ]);
- $behavior->verifyConfig();
- $this->assertTrue(true, 'No exception thrown');
- }
- /**
- * testVerifyImplementedFindersInvalid
- *
- * @expectedException \Cake\Error\Exception
- * @expectedExceptionMessage The method findNotDefined is not callable on class Cake\Test\TestCase\ORM\Test2Behavior
- *
- * @return void
- */
- public function testVerifyImplementedFindersInvalid() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table, [
- 'implementedFinders' => [
- 'aliased' => 'findNotDefined'
- ]
- ]);
- $behavior->verifyConfig();
- }
- /**
- * testVerifyConfigImplementedMethodsOverriden
- *
- * Don't expect an exception to be thrown
- *
- * @return void
- */
- public function testVerifyConfigImplementedMethodsOverriden() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table);
- $behavior = new Test2Behavior($table, [
- 'implementedMethods' => [
- 'aliased' => 'doSomething'
- ]
- ]);
- $behavior->verifyConfig();
- $this->assertTrue(true, 'No exception thrown');
- }
- /**
- * testVerifyImplementedMethodsInvalid
- *
- * @expectedException \Cake\Error\Exception
- * @expectedExceptionMessage The method iDoNotExist is not callable on class Cake\Test\TestCase\ORM\Test2Behavior
- *
- * @return void
- */
- public function testVerifyImplementedMethodsInvalid() {
- $table = $this->getMock('Cake\ORM\Table');
- $behavior = new Test2Behavior($table, [
- 'implementedMethods' => [
- 'aliased' => 'iDoNotExist'
- ]
- ]);
- $behavior->verifyConfig();
- }
- }
|