| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909 |
- <?php
- /**
- * PHP Version 5.4
- *
- * 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 CakePHP(tm) v 3.0.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- namespace Cake\Test\TestCase\ORM;
- use Cake\Core\Configure;
- use Cake\Database\ConnectionManager;
- use Cake\ORM\Table;
- use Cake\ORM\TableRegistry;
- /**
- * Used to test correct class is instantiated when using TableRegistry::get();
- */
- class UsersTable extends Table {
- }
- /**
- * Tests Table class
- *
- */
- class TableTest extends \Cake\TestSuite\TestCase {
- public $fixtures = [
- 'core.user', 'core.category', 'core.article', 'core.author',
- 'core.tag', 'core.articles_tag'
- ];
- public function setUp() {
- parent::setUp();
- $this->connection = ConnectionManager::get('test');
- Configure::write('App.namespace', 'TestApp');
- }
- public function tearDown() {
- parent::tearDown();
- TableRegistry::clear();
- }
- /**
- * Tests the table method
- *
- * @return void
- */
- public function testTableMethod() {
- $table = new Table(['table' => 'users']);
- $this->assertEquals('users', $table->table());
- $table = new UsersTable;
- $this->assertEquals('users', $table->table());
- $table = $this->getMockBuilder('\Cake\ORM\Table')
- ->setMethods(['find'])
- ->setMockClassName('SpecialThingTable')
- ->getMock();
- $this->assertEquals('special_things', $table->table());
- $table = new Table(['alias' => 'LoveBoat']);
- $this->assertEquals('love_boats', $table->table());
- $table->table('other');
- $this->assertEquals('other', $table->table());
- }
- /**
- * Tests the alias method
- *
- * @return void
- */
- public function testAliasMethod() {
- $table = new Table(['alias' => 'users']);
- $this->assertEquals('users', $table->alias());
- $table = new Table(['table' => 'stuffs']);
- $this->assertEquals('stuffs', $table->alias());
- $table = new UsersTable;
- $this->assertEquals('Users', $table->alias());
- $table = $this->getMockBuilder('\Cake\ORM\Table')
- ->setMethods(['find'])
- ->setMockClassName('SpecialThingTable')
- ->getMock();
- $this->assertEquals('SpecialThing', $table->alias());
- $table->alias('AnotherOne');
- $this->assertEquals('AnotherOne', $table->alias());
- }
- /**
- * Tests connection method
- *
- * @return void
- */
- public function testConnection() {
- $table = new Table(['table' => 'users']);
- $this->assertNull($table->connection());
- $table->connection($this->connection);
- $this->assertSame($this->connection, $table->connection());
- }
- /**
- * Tests primaryKey method
- *
- * @return void
- */
- public function testPrimaryKey() {
- $table = new Table(['table' => 'users']);
- $this->assertEquals('id', $table->primaryKey());
- $table->primaryKey('thingID');
- $this->assertEquals('thingID', $table->primaryKey());
- }
- /**
- * Tests that name will be selected as a displayField
- *
- * @return void
- */
- public function testDisplayFieldName() {
- $table = new Table([
- 'table' => 'users',
- 'schema' => [
- 'foo' => ['type' => 'string'],
- 'name' => ['type' => 'string']
- ]
- ]);
- $this->assertEquals('name', $table->displayField());
- }
- /**
- * Tests that title will be selected as a displayField
- *
- * @return void
- */
- public function testDisplayFieldTitle() {
- $table = new Table([
- 'table' => 'users',
- 'schema' => [
- 'foo' => ['type' => 'string'],
- 'title' => ['type' => 'string']
- ]
- ]);
- $this->assertEquals('title', $table->displayField());
- }
- /**
- * Tests that no displayField will fallback to primary key
- *
- * @return void
- */
- public function testDisplayFallback() {
- $table = new Table([
- 'table' => 'users',
- 'schema' => [
- 'id' => ['type' => 'string'],
- 'foo' => ['type' => 'string']
- ]
- ]);
- $this->assertEquals('id', $table->displayField());
- }
- /**
- * Tests that displayField can be changed
- *
- * @return void
- */
- public function testDisplaySet() {
- $table = new Table([
- 'table' => 'users',
- 'schema' => [
- 'id' => ['type' => 'string'],
- 'foo' => ['type' => 'string']
- ]
- ]);
- $this->assertEquals('id', $table->displayField());
- $table->displayField('foo');
- $this->assertEquals('foo', $table->displayField());
- }
- /**
- * Tests schema method
- *
- * @return void
- */
- public function testSchema() {
- $schema = $this->connection->schemaCollection()->describe('users');
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $this->assertEquals($schema, $table->schema());
- $table = new Table(['table' => 'stuff']);
- $table->schema($schema);
- $this->assertSame($schema, $table->schema());
- $table = new Table(['table' => 'another']);
- $schema = ['id' => ['type' => 'integer']];
- $table->schema($schema);
- $this->assertEquals(
- new \Cake\Database\Schema\Table('another', $schema),
- $table->schema()
- );
- }
- /**
- * Tests that all fields for a table are added by default in a find when no
- * other fields are specified
- *
- * @return void
- */
- public function testFindAllNoFieldsAndNoHydration() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $results = $table
- ->find('all')
- ->where(['id IN' => [1, 2]])
- ->order('id')
- ->hydrate(false)
- ->toArray();
- $expected = [
- [
- 'id' => 1,
- 'username' => 'mariano',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => new \DateTime('2007-03-17 01:16:23'),
- 'updated' => new \DateTime('2007-03-17 01:18:31'),
- ],
- [
- 'id' => 2,
- 'username' => 'nate',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => new \DateTime('2008-03-17 01:18:23'),
- 'updated' => new \DateTime('2008-03-17 01:20:31'),
- ],
- ];
- $this->assertEquals($expected, $results);
- }
- /**
- * Tests that it is possible to select only a few fields when finding over a table
- *
- * @return void
- */
- public function testFindAllSomeFieldsNoHydration() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $results = $table->find('all')
- ->select(['username', 'password'])
- ->hydrate(false)
- ->order('username')->toArray();
- $expected = [
- ['username' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'],
- ['username' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'],
- ['username' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'],
- ['username' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'],
- ];
- $this->assertSame($expected, $results);
- $results = $table->find('all')
- ->select(['foo' => 'username', 'password'])
- ->order('username')
- ->hydrate(false)
- ->toArray();
- $expected = [
- ['foo' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'],
- ['foo' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'],
- ['foo' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'],
- ['foo' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'],
- ];
- $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() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $query = $table->find('all')
- ->select(['id', 'username'])
- ->where(['created >=' => new \DateTime('2010-01-22 00:00')])
- ->hydrate(false)
- ->order('id');
- $expected = [
- ['id' => 3, 'username' => 'larry'],
- ['id' => 4, 'username' => 'garrett']
- ];
- $this->assertSame($expected, $query->toArray());
- $query->orWhere(['users.created' => new \DateTime('2008-03-17 01:18:23')]);
- $expected = [
- ['id' => 2, 'username' => 'nate'],
- ['id' => 3, 'username' => 'larry'],
- ['id' => 4, 'username' => 'garrett']
- ];
- $this->assertSame($expected, $query->toArray());
- }
- /**
- * Test that beforeFind events can mutate the query.
- *
- * @return void
- */
- public function testFindBeforeFindEventMutateQuery() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $table->getEventManager()->attach(function ($event, $query, $options) {
- $query->limit(1);
- }, 'Model.beforeFind');
- $result = $table->find('all')->execute();
- $this->assertCount(1, $result, 'Should only have 1 record, limit 1 applied.');
- }
- /**
- * Test that beforeFind events are fired and can stop the find and
- * return custom results.
- *
- * @return void
- */
- public function testFindBeforeFindEventOverrideReturn() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $expected = ['One', 'Two', 'Three'];
- $table->getEventManager()->attach(function ($event, $query, $options) use ($expected) {
- $query->setResult($expected);
- $event->stopPropagation();
- }, 'Model.beforeFind');
- $query = $table->find('all');
- $query->limit(1);
- $this->assertEquals($expected, $query->execute());
- }
- /**
- * Tests that belongsTo() creates and configures correctly the association
- *
- * @return void
- */
- public function testBelongsTo() {
- $options = ['foreignKey' => 'fake_id', 'conditions' => ['a' => 'b']];
- $table = new Table(['table' => 'dates']);
- $belongsTo = $table->belongsTo('user', $options);
- $this->assertInstanceOf('\Cake\ORM\Association\BelongsTo', $belongsTo);
- $this->assertSame($belongsTo, $table->association('user'));
- $this->assertEquals('user', $belongsTo->name());
- $this->assertEquals('fake_id', $belongsTo->foreignKey());
- $this->assertEquals(['a' => 'b'], $belongsTo->conditions());
- $this->assertSame($table, $belongsTo->source());
- }
- /**
- * Tests that hasOne() creates and configures correctly the association
- *
- * @return void
- */
- public function testHasOne() {
- $options = ['foreignKey' => 'user_id', 'conditions' => ['b' => 'c']];
- $table = new Table(['table' => 'users']);
- $hasOne = $table->hasOne('profile', $options);
- $this->assertInstanceOf('\Cake\ORM\Association\HasOne', $hasOne);
- $this->assertSame($hasOne, $table->association('profile'));
- $this->assertEquals('profile', $hasOne->name());
- $this->assertEquals('user_id', $hasOne->foreignKey());
- $this->assertEquals(['b' => 'c'], $hasOne->conditions());
- $this->assertSame($table, $hasOne->source());
- }
- /**
- * Tests that hasMany() creates and configures correctly the association
- *
- * @return void
- */
- public function testHasMany() {
- $options = [
- 'foreignKey' => 'author_id',
- 'conditions' => ['b' => 'c'],
- 'sort' => ['foo' => 'asc']
- ];
- $table = new Table(['table' => 'authors']);
- $hasMany = $table->hasMany('article', $options);
- $this->assertInstanceOf('\Cake\ORM\Association\HasMany', $hasMany);
- $this->assertSame($hasMany, $table->association('article'));
- $this->assertEquals('article', $hasMany->name());
- $this->assertEquals('author_id', $hasMany->foreignKey());
- $this->assertEquals(['b' => 'c'], $hasMany->conditions());
- $this->assertEquals(['foo' => 'asc'], $hasMany->sort());
- $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', 'connection' => $this->connection]);
- $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());
- }
- /**
- * Test basic multi row updates.
- *
- * @return void
- */
- public function testUpdateAll() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $fields = ['username' => 'mark'];
- $result = $table->updateAll($fields, ['id <' => 4]);
- $this->assertTrue($result);
- $result = $table->find('all')
- ->select(['username'])
- ->order(['id' => 'asc'])
- ->hydrate(false)
- ->toArray();
- $expected = array_fill(0, 3, $fields);
- $expected[] = ['username' => 'garrett'];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that exceptions from the Query bubble up.
- *
- * @expectedException Cake\Database\Exception
- */
- public function testUpdateAllFailure() {
- $table = $this->getMock(
- 'Cake\ORM\Table',
- ['_buildQuery'],
- [['table' => 'users']]
- );
- $query = $this->getMock('Cake\ORM\Query', ['executeStatement'], [$this->connection, null]);
- $table->expects($this->once())
- ->method('_buildQuery')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('executeStatement')
- ->will($this->throwException(new \Cake\Database\Exception('Not good')));
- $table->updateAll(['username' => 'mark'], []);
- }
- /**
- * Test deleting many records.
- *
- * @return void
- */
- public function testDeleteAll() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $result = $table->deleteAll(['id <' => 4]);
- $this->assertTrue($result);
- $result = $table->find('all')->toArray();
- $this->assertCount(1, $result, 'Only one record should remain');
- $this->assertEquals(4, $result[0]['id']);
- }
- /**
- * Test that exceptions from the Query bubble up.
- *
- * @expectedException Cake\Database\Exception
- */
- public function testDeleteAllFailure() {
- $table = $this->getMock(
- 'Cake\ORM\Table',
- ['_buildQuery'],
- [['table' => 'users', 'connection' => $this->connection]]
- );
- $query = $this->getMock('Cake\ORM\Query', ['executeStatement'], [$this->connection, null]);
- $table->expects($this->once())
- ->method('_buildQuery')
- ->will($this->returnValue($query));
- $query->expects($this->once())
- ->method('executeStatement')
- ->will($this->throwException(new \Cake\Database\Exception('Not good')));
- $table->deleteAll(['id >' => 4]);
- }
- /**
- * Tests that array options are passed to the query object using applyOptions
- *
- * @return void
- */
- public function testFindApplyOptions() {
- $table = $this->getMock(
- 'Cake\ORM\Table',
- ['_buildQuery'],
- [['table' => 'users', 'connection' => $this->connection]]
- );
- $query = $this->getMock('Cake\ORM\Query', [], [$this->connection, $table]);
- $table->expects($this->once())
- ->method('_buildQuery')
- ->will($this->returnValue($query));
- $options = ['fields' => ['a', 'b'], 'connections' => ['a >' => 1]];
- $query->expects($this->any())
- ->method('select')
- ->will($this->returnSelf());
- $query->expects($this->once())
- ->method('applyOptions')
- ->with($options);
- $table->find('all', $options);
- }
- /**
- * Tests find('list')
- *
- * @return void
- */
- public function testFindListNoHydration() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $table->displayField('username');
- $query = $table->find('list', ['fields' => ['id', 'username']])
- ->hydrate(false)
- ->order('id');
- $expected = [
- 1 => 'mariano',
- 2 => 'nate',
- 3 => 'larry',
- 4 => 'garrett'
- ];
- $this->assertSame($expected, $query->toArray());
- $query = $table->find('list', ['groupField' => 'odd'])
- ->select(['id', 'username', 'odd' => 'id % 2 = 0'])
- ->hydrate(false)
- ->order('id');
- $expected = [
- 0 => [
- 1 => 'mariano',
- 3 => 'larry'
- ],
- 1 => [
- 2 => 'nate',
- 4 => 'garrett'
- ]
- ];
- $this->assertSame($expected, $query->toArray());
- }
- /**
- * Tests find('threaded')
- *
- * @return void
- */
- public function testFindThreadedNoHydration() {
- $table = new Table([
- 'table' => 'categories',
- 'connection' => $this->connection,
- ]);
- $expected = [
- [
- 'id' => 1,
- 'parent_id' => 0,
- 'name' => 'Category 1',
- 'children' => [
- [
- 'id' => 2,
- 'parent_id' => 1,
- 'name' => 'Category 1.1',
- 'children' => [
- [
- 'id' => 7,
- 'parent_id' => 2,
- 'name' => 'Category 1.1.1',
- 'children' => []
- ],
- [
- 'id' => 8,
- 'parent_id' => '2',
- 'name' => 'Category 1.1.2',
- 'children' => []
- ]
- ],
- ],
- [
- 'id' => 3,
- 'parent_id' => '1',
- 'name' => 'Category 1.2',
- 'children' => []
- ],
- ]
- ],
- [
- 'id' => 4,
- 'parent_id' => 0,
- 'name' => 'Category 2',
- 'children' => []
- ],
- [
- 'id' => 5,
- 'parent_id' => 0,
- 'name' => 'Category 3',
- 'children' => [
- [
- 'id' => '6',
- 'parent_id' => '5',
- 'name' => 'Category 3.1',
- 'children' => []
- ]
- ]
- ]
- ];
- $results = $table->find('all')
- ->select(['id', 'parent_id', 'name'])
- ->hydrate(false)
- ->threaded()
- ->toArray();
- $this->assertEquals($expected, $results);
- }
- /**
- * Tests that finders can be called directly
- *
- * @return void
- */
- public function testCallingFindersDirectly() {
- $table = $this->getMock('\Cake\ORM\Table', ['find'], [], '', false);
- $query = $this->getMock('\Cake\ORM\Query', [], [$this->connection, $table]);
- $table->expects($this->once())
- ->method('find')
- ->with('list', [])
- ->will($this->returnValue($query));
- $this->assertSame($query, $table->list());
- $table = $this->getMock('\Cake\ORM\Table', ['find'], [], '', false);
- $table->expects($this->once())
- ->method('find')
- ->with('threaded', ['order' => ['name' => 'ASC']])
- ->will($this->returnValue($query));
- $this->assertSame($query, $table->threaded(['order' => ['name' => 'ASC']]));
- }
- /**
- * Tests that finders can be stacked
- *
- * @return void
- */
- public function testStackingFinders() {
- $table = $this->getMock('\Cake\ORM\Table', ['find', 'findList'], [], '', false);
- $params = [$this->connection, $table];
- $query = $this->getMock('\Cake\ORM\Query', ['addDefaultTypes'], $params);
- $table->expects($this->once())
- ->method('find')
- ->with('threaded', ['order' => ['name' => 'ASC']])
- ->will($this->returnValue($query));
- $table->expects($this->once())
- ->method('findList')
- ->with($query, ['keyPath' => 'id'])
- ->will($this->returnValue($query));
- $result = $table
- ->threaded(['order' => ['name' => 'ASC']])
- ->list(['keyPath' => 'id']);
- $this->assertSame($query, $result);
- }
- /**
- * Tests find('threaded') with hydrated results
- *
- * @return void
- */
- public function testFindThreadedHydrated() {
- $table = new Table([
- 'table' => 'categories',
- 'connection' => $this->connection,
- ]);
- $results = $table->find('all')
- ->threaded()
- ->select(['id', 'parent_id', 'name'])
- ->toArray();
- $this->assertEquals(1, $results[0]->id);
- $expected = [
- 'id' => 8,
- 'parent_id' => 2,
- 'name' => 'Category 1.1.2',
- 'children' => []
- ];
- $this->assertEquals($expected, $results[0]->children[0]->children[1]->toArray());
- }
- /**
- * Tests find('list') with hydrated records
- *
- * @return void
- */
- public function testFindListHydrated() {
- $table = new Table([
- 'table' => 'users',
- 'connection' => $this->connection,
- ]);
- $table->displayField('username');
- $query = $table
- ->find('list', ['fields' => ['id', 'username']])
- ->order('id');
- $expected = [
- 1 => 'mariano',
- 2 => 'nate',
- 3 => 'larry',
- 4 => 'garrett'
- ];
- $this->assertSame($expected, $query->toArray());
- $query = $table->find('list', ['groupField' => 'odd'])
- ->select(['id', 'username', 'odd' => 'id % 2 = 0'])
- ->hydrate(true)
- ->order('id');
- $expected = [
- 0 => [
- 1 => 'mariano',
- 3 => 'larry'
- ],
- 1 => [
- 2 => 'nate',
- 4 => 'garrett'
- ]
- ];
- $this->assertSame($expected, $query->toArray());
- }
- public function testEntityClassDefault() {
- $table = new Table();
- $this->assertEquals('\Cake\ORM\Entity', $table->entityClass());
- }
- /**
- * Tests that using a simple string for entityClass will try to
- * load the class from the App namespace
- *
- * @return void
- */
- public function testRepositoryClassInAPP() {
- $class = $this->getMockClass('\Cake\ORM\Entity');
- class_alias($class, 'TestApp\Model\Entity\TestUser');
- $table = new Table();
- $this->assertEquals('TestApp\Model\Entity\TestUser', $table->entityClass('TestUser'));
- }
- /**
- * Tests that using a simple string for entityClass will try to
- * load the class from the Plugin namespace when using plugin notation
- *
- * @return void
- */
- public function testRepositoryClassInPlugin() {
- $class = $this->getMockClass('\Cake\ORM\Entity');
- class_alias($class, 'MyPlugin\Model\Entity\SuperUser');
- $table = new Table();
- $this->assertEquals(
- 'MyPlugin\Model\Entity\SuperUser',
- $table->entityClass('MyPlugin.SuperUser')
- );
- }
- /**
- * Tests that using a simple string for entityClass will throw an exception
- * when the class does not exist in the namespace
- *
- * @expectedException Cake\ORM\Error\MissingEntityException
- * @expectedExceptionMessage Entity class FooUser could not be found.
- * @return void
- */
- public function testRepositoryClassNonExisting() {
- $table = new Table;
- $this->assertFalse($table->entityClass('FooUser'));
- }
- /**
- * Tests getting the entityClass based on conventions for the entity
- * namespace
- *
- * @return void
- */
- public function testRepositoryClassConventionForAPP() {
- $table = new \TestApp\Model\Repository\ArticleTable;
- $this->assertEquals('TestApp\Model\Entity\Article', $table->entityClass());
- }
- /**
- * Tests setting a entity class object using the setter method
- *
- * @return void
- */
- public function testSetEntityClass() {
- $table = new Table;
- $class = '\\' . $this->getMockClass('\Cake\ORM\Entity');
- $table->entityClass($class);
- $this->assertEquals($class, $table->entityClass());
- }
- /**
- * Proves that associations, even though they are lazy loaded, will fetch
- * records using the correct table class and hydrate with the correct entity
- *
- * @return void
- */
- public function testReciprocalBelongsToLoading() {
- $table = new \TestApp\Model\Repository\ArticleTable([
- 'connection' => $this->connection,
- ]);
- $result = $table->find('all')->contain(['author'])->first();
- $this->assertInstanceOf('TestApp\Model\Entity\Author', $result->author);
- }
- /**
- * Proves that associations, even though they are lazy loaded, will fetch
- * records using the correct table class and hydrate with the correct entity
- *
- * @return void
- */
- public function testReciprocalHasManyLoading() {
- $table = new \TestApp\Model\Repository\ArticleTable([
- 'connection' => $this->connection,
- ]);
- $result = $table->find('all')->contain(['author' => ['article']])->first();
- $this->assertCount(2, $result->author->article);
- foreach ($result->author->article as $article) {
- $this->assertInstanceOf('TestApp\Model\Entity\Article', $article);
- }
- }
- /**
- * Tests that the correct table and entity are loaded for the pivot association in
- * a belongsToMany setup
- *
- * @return void
- */
- public function testReciprocalBelongsToMany() {
- $table = new \TestApp\Model\Repository\ArticleTable([
- 'connection' => $this->connection,
- ]);
- $result = $table->find('all')->contain(['tag'])->first();
- $this->assertInstanceOf('TestApp\Model\Entity\Tag', $result->tags[0]);
- $this->assertInstanceOf('TestApp\Model\Entity\ArticlesTag', $result->tags[0]->extraInfo);
- }
- /**
- * Tests that it is possible to insert a new row using the save method
- *
- * @return void
- */
- public function testSaveNewEntity() {
- $entity = new \Cake\ORM\Entity([
- 'username' => 'superuser',
- 'password' => 'root',
- 'created' => new \DateTime('2013-10-10 00:00'),
- 'updated' => new \DateTime('2013-10-10 00:00')
- ]);
- $table = TableRegistry::get('user');
- $this->assertSame($entity, $table->save($entity));
- $this->assertEquals($entity->id, 5);
- $row = $table->find('all')->where(['id' => 5])->first();
- $this->assertEquals($entity->toArray(), $row->toArray());
- }
- }
|