| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://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 for event bindings.
- */
- public function beforeRules()
- {
- }
- /**
- * Test for event bindings.
- */
- public function afterRules()
- {
- }
- /**
- * Test for event bindings.
- */
- public function buildRules()
- {
- }
- /**
- * Test for event bindings.
- */
- public function afterSaveCommit()
- {
- }
- /**
- * Test for event bindings.
- */
- public function afterDeleteCommit()
- {
- }
- }
- /**
- * Test Stub.
- */
- class Test2Behavior extends Behavior
- {
- protected $_defaultConfig = [
- 'implementedFinders' => [
- 'foo' => 'findFoo',
- ],
- 'implementedMethods' => [
- 'doSomething' => 'doSomething',
- ]
- ];
- /**
- * 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 array
- */
- 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->getMockBuilder('Cake\ORM\Table')->getMock();
- $config = ['key' => 'value'];
- $behavior = new TestBehavior($table, $config);
- $this->assertEquals($config, $behavior->getConfig());
- }
- /**
- * Test getting table instance.
- *
- * @return void
- */
- public function testGetTable()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new TestBehavior($table);
- $this->assertSame($table, $behavior->getTable());
- }
- public function testReflectionCache()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $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->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new TestBehavior($table);
- $expected = [
- 'Model.beforeFind' => 'beforeFind',
- 'Model.afterSaveCommit' => 'afterSaveCommit',
- 'Model.buildRules' => 'buildRules',
- 'Model.beforeRules' => 'beforeRules',
- 'Model.afterRules' => 'afterRules',
- 'Model.afterDeleteCommit' => 'afterDeleteCommit',
- ];
- $this->assertEquals($expected, $behavior->implementedEvents());
- }
- /**
- * Test that implementedEvents uses the priority setting.
- *
- * @return void
- */
- public function testImplementedEventsWithPriority()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new TestBehavior($table, ['priority' => 10]);
- $expected = [
- 'Model.beforeFind' => [
- 'priority' => 10,
- 'callable' => 'beforeFind'
- ],
- 'Model.afterSaveCommit' => [
- 'priority' => 10,
- 'callable' => 'afterSaveCommit'
- ],
- 'Model.beforeRules' => [
- 'priority' => 10,
- 'callable' => 'beforeRules'
- ],
- 'Model.afterRules' => [
- 'priority' => 10,
- 'callable' => 'afterRules'
- ],
- 'Model.buildRules' => [
- 'priority' => 10,
- 'callable' => 'buildRules'
- ],
- 'Model.afterDeleteCommit' => [
- 'priority' => 10,
- 'callable' => 'afterDeleteCommit'
- ],
- ];
- $this->assertEquals($expected, $behavior->implementedEvents());
- }
- /**
- * testImplementedMethods
- *
- * @return void
- */
- public function testImplementedMethods()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table);
- $expected = [
- 'doSomething' => 'doSomething'
- ];
- $this->assertEquals($expected, $behavior->implementedMethods());
- }
- /**
- * testImplementedMethodsAliased
- *
- * @return void
- */
- public function testImplementedMethodsAliased()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table, [
- 'implementedMethods' => [
- 'aliased' => 'doSomething'
- ]
- ]);
- $expected = [
- 'aliased' => 'doSomething'
- ];
- $this->assertEquals($expected, $behavior->implementedMethods());
- }
- /**
- * testImplementedMethodsDisabled
- *
- * @return void
- */
- public function testImplementedMethodsDisabled()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table, [
- 'implementedMethods' => []
- ]);
- $expected = [];
- $this->assertEquals($expected, $behavior->implementedMethods());
- }
- /**
- * testImplementedFinders
- *
- * @return void
- */
- public function testImplementedFinders()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table);
- $expected = [
- 'foo' => 'findFoo',
- ];
- $this->assertEquals($expected, $behavior->implementedFinders());
- }
- /**
- * testImplementedFindersAliased
- *
- * @return void
- */
- public function testImplementedFindersAliased()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table, [
- 'implementedFinders' => [
- 'aliased' => 'findFoo'
- ]
- ]);
- $expected = [
- 'aliased' => 'findFoo'
- ];
- $this->assertEquals($expected, $behavior->implementedFinders());
- }
- /**
- * testImplementedFindersDisabled
- *
- * @return void
- */
- public function testImplementedFindersDisabled()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table, [
- 'implementedFinders' => []
- ]);
- $this->assertEquals([], $behavior->implementedFinders());
- }
- /**
- * testVerifyConfig
- *
- * Don't expect an exception to be thrown
- *
- * @return void
- */
- public function testVerifyConfig()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table);
- $behavior->verifyConfig();
- $this->assertTrue(true, 'No exception thrown');
- }
- /**
- * testVerifyConfigImplementedFindersOverridden
- *
- * Simply don't expect an exception to be thrown
- *
- * @return void
- */
- public function testVerifyConfigImplementedFindersOverridden()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table, [
- 'implementedFinders' => [
- 'aliased' => 'findFoo'
- ]
- ]);
- $behavior->verifyConfig();
- $this->assertTrue(true, 'No exception thrown');
- }
- /**
- * testVerifyImplementedFindersInvalid
- *
- *
- * @return void
- */
- public function testVerifyImplementedFindersInvalid()
- {
- $this->expectException(\Cake\Core\Exception\Exception::class);
- $this->expectExceptionMessage('The method findNotDefined is not callable on class Cake\Test\TestCase\ORM\Test2Behavior');
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table, [
- 'implementedFinders' => [
- 'aliased' => 'findNotDefined'
- ]
- ]);
- $behavior->verifyConfig();
- }
- /**
- * testVerifyConfigImplementedMethodsOverridden
- *
- * Don't expect an exception to be thrown
- *
- * @return void
- */
- public function testVerifyConfigImplementedMethodsOverridden()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table);
- $behavior = new Test2Behavior($table, [
- 'implementedMethods' => [
- 'aliased' => 'doSomething'
- ]
- ]);
- $behavior->verifyConfig();
- $this->assertTrue(true, 'No exception thrown');
- }
- /**
- * testVerifyImplementedMethodsInvalid
- *
- *
- * @return void
- */
- public function testVerifyImplementedMethodsInvalid()
- {
- $this->expectException(\Cake\Core\Exception\Exception::class);
- $this->expectExceptionMessage('The method iDoNotExist is not callable on class Cake\Test\TestCase\ORM\Test2Behavior');
- $table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
- $behavior = new Test2Behavior($table, [
- 'implementedMethods' => [
- 'aliased' => 'iDoNotExist'
- ]
- ]);
- $behavior->verifyConfig();
- }
- }
|