EntityTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\Entity;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\Validation\Validator;
  19. use TestApp\Model\Entity\Extending;
  20. use TestApp\Model\Entity\NonExtending;
  21. /**
  22. * Entity test case.
  23. */
  24. class EntityTest extends TestCase
  25. {
  26. /**
  27. * Tests setting a single property in an entity without custom setters
  28. *
  29. * @return void
  30. */
  31. public function testSetOneParamNoSetters()
  32. {
  33. $entity = new Entity;
  34. $this->assertNull($entity->getOriginal('foo'));
  35. $entity->set('foo', 'bar');
  36. $this->assertEquals('bar', $entity->foo);
  37. $this->assertEquals('bar', $entity->getOriginal('foo'));
  38. $entity->set('foo', 'baz');
  39. $this->assertEquals('baz', $entity->foo);
  40. $this->assertEquals('bar', $entity->getOriginal('foo'));
  41. $entity->set('id', 1);
  42. $this->assertSame(1, $entity->id);
  43. $this->assertEquals(1, $entity->getOriginal('id'));
  44. $this->assertEquals('bar', $entity->getOriginal('foo'));
  45. }
  46. /**
  47. * Tests setting multiple properties without custom setters
  48. *
  49. * @return void
  50. */
  51. public function testSetMultiplePropertiesNoSetters()
  52. {
  53. $entity = new Entity;
  54. $entity->accessible('*', true);
  55. $entity->set(['foo' => 'bar', 'id' => 1]);
  56. $this->assertEquals('bar', $entity->foo);
  57. $this->assertSame(1, $entity->id);
  58. $entity->set(['foo' => 'baz', 'id' => 2, 'thing' => 3]);
  59. $this->assertEquals('baz', $entity->foo);
  60. $this->assertSame(2, $entity->id);
  61. $this->assertSame(3, $entity->thing);
  62. $this->assertEquals('bar', $entity->getOriginal('foo'));
  63. $this->assertEquals(1, $entity->getOriginal('id'));
  64. }
  65. /**
  66. * Tests setting a single property using a setter function
  67. *
  68. * @return void
  69. */
  70. public function testSetOneParamWithSetter()
  71. {
  72. $entity = $this->getMock('\Cake\ORM\Entity', ['_setName']);
  73. $entity->expects($this->once())->method('_setName')
  74. ->with('Jones')
  75. ->will($this->returnCallback(function ($name) {
  76. $this->assertEquals('Jones', $name);
  77. return 'Dr. ' . $name;
  78. }));
  79. $entity->set('name', 'Jones');
  80. $this->assertEquals('Dr. Jones', $entity->name);
  81. }
  82. /**
  83. * Tests setting multiple properties using a setter function
  84. *
  85. * @return void
  86. */
  87. public function testMultipleWithSetter()
  88. {
  89. $entity = $this->getMock('\Cake\ORM\Entity', ['_setName', '_setStuff']);
  90. $entity->accessible('*', true);
  91. $entity->expects($this->once())->method('_setName')
  92. ->with('Jones')
  93. ->will($this->returnCallback(function ($name) {
  94. $this->assertEquals('Jones', $name);
  95. return 'Dr. ' . $name;
  96. }));
  97. $entity->expects($this->once())->method('_setStuff')
  98. ->with(['a', 'b'])
  99. ->will($this->returnCallback(function ($stuff) {
  100. $this->assertEquals(['a', 'b'], $stuff);
  101. return ['c', 'd'];
  102. }));
  103. $entity->set(['name' => 'Jones', 'stuff' => ['a', 'b']]);
  104. $this->assertEquals('Dr. Jones', $entity->name);
  105. $this->assertEquals(['c', 'd'], $entity->stuff);
  106. }
  107. /**
  108. * Tests that it is possible to bypass the setters
  109. *
  110. * @return void
  111. */
  112. public function testBypassSetters()
  113. {
  114. $entity = $this->getMock('\Cake\ORM\Entity', ['_setName', '_setStuff']);
  115. $entity->accessible('*', true);
  116. $entity->expects($this->never())->method('_setName');
  117. $entity->expects($this->never())->method('_setStuff');
  118. $entity->set('name', 'Jones', ['setter' => false]);
  119. $this->assertEquals('Jones', $entity->name);
  120. $entity->set('stuff', 'Thing', ['setter' => false]);
  121. $this->assertEquals('Thing', $entity->stuff);
  122. $entity->set(['name' => 'foo', 'stuff' => 'bar'], ['setter' => false]);
  123. $this->assertEquals('bar', $entity->stuff);
  124. }
  125. /**
  126. * Tests that the constructor will set initial properties
  127. *
  128. * @return void
  129. */
  130. public function testConstructor()
  131. {
  132. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  133. ->setMethods(['set'])
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $entity->expects($this->at(0))
  137. ->method('set')
  138. ->with(['a' => 'b', 'c' => 'd'], ['setter' => true, 'guard' => false]);
  139. $entity->expects($this->at(1))
  140. ->method('set')
  141. ->with(['foo' => 'bar'], ['setter' => false, 'guard' => false]);
  142. $entity->__construct(['a' => 'b', 'c' => 'd']);
  143. $entity->__construct(['foo' => 'bar'], ['useSetters' => false]);
  144. }
  145. /**
  146. * Tests that the constructor will set initial properties and pass the guard
  147. * option along
  148. *
  149. * @return void
  150. */
  151. public function testConstructorWithGuard()
  152. {
  153. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  154. ->setMethods(['set'])
  155. ->disableOriginalConstructor()
  156. ->getMock();
  157. $entity->expects($this->once())
  158. ->method('set')
  159. ->with(['foo' => 'bar'], ['setter' => true, 'guard' => true]);
  160. $entity->__construct(['foo' => 'bar'], ['guard' => true]);
  161. }
  162. /**
  163. * Tests getting properties with no custom getters
  164. *
  165. * @return void
  166. */
  167. public function testGetNoGetters()
  168. {
  169. $entity = new Entity(['id' => 1, 'foo' => 'bar']);
  170. $this->assertSame(1, $entity->get('id'));
  171. $this->assertSame('bar', $entity->get('foo'));
  172. }
  173. /**
  174. * Tests get with custom getter
  175. *
  176. * @return void
  177. */
  178. public function testGetCustomGetters()
  179. {
  180. $entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
  181. $entity->expects($this->exactly(2))->method('_getName')
  182. ->with('Jones')
  183. ->will($this->returnCallback(function ($name) {
  184. $this->assertSame('Jones', $name);
  185. return 'Dr. ' . $name;
  186. }));
  187. $entity->set('name', 'Jones');
  188. $this->assertEquals('Dr. Jones', $entity->get('name'));
  189. $this->assertEquals('Dr. Jones', $entity->get('name'));
  190. }
  191. /**
  192. * Test magic property setting with no custom setter
  193. *
  194. * @return void
  195. */
  196. public function testMagicSet()
  197. {
  198. $entity = new Entity;
  199. $entity->name = 'Jones';
  200. $this->assertEquals('Jones', $entity->name);
  201. $entity->name = 'George';
  202. $this->assertEquals('George', $entity->name);
  203. }
  204. /**
  205. * Tests magic set with custom setter function
  206. *
  207. * @return void
  208. */
  209. public function testMagicSetWithSetter()
  210. {
  211. $entity = $this->getMock('\Cake\ORM\Entity', ['_setName']);
  212. $entity->expects($this->once())->method('_setName')
  213. ->with('Jones')
  214. ->will($this->returnCallback(function ($name) {
  215. $this->assertEquals('Jones', $name);
  216. return 'Dr. ' . $name;
  217. }));
  218. $entity->name = 'Jones';
  219. $this->assertEquals('Dr. Jones', $entity->name);
  220. }
  221. /**
  222. * Tests the magic getter with a custom getter function
  223. *
  224. * @return void
  225. */
  226. public function testMagicGetWithGetter()
  227. {
  228. $entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
  229. $entity->expects($this->once())->method('_getName')
  230. ->with('Jones')
  231. ->will($this->returnCallback(function ($name) {
  232. $this->assertSame('Jones', $name);
  233. return 'Dr. ' . $name;
  234. }));
  235. $entity->set('name', 'Jones');
  236. $this->assertEquals('Dr. Jones', $entity->name);
  237. }
  238. /**
  239. * Test indirectly modifying internal properties
  240. *
  241. * @return void
  242. */
  243. public function testIndirectModification()
  244. {
  245. $entity = new Entity;
  246. $entity->things = ['a', 'b'];
  247. $entity->things[] = 'c';
  248. $this->assertEquals(['a', 'b', 'c'], $entity->things);
  249. }
  250. /**
  251. * Tests has() method
  252. *
  253. * @return void
  254. */
  255. public function testHas()
  256. {
  257. $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
  258. $this->assertTrue($entity->has('id'));
  259. $this->assertTrue($entity->has('name'));
  260. $this->assertFalse($entity->has('foo'));
  261. $this->assertFalse($entity->has('last_name'));
  262. $this->assertTrue($entity->has(['id']));
  263. $this->assertTrue($entity->has(['id', 'name']));
  264. $this->assertFalse($entity->has(['id', 'foo']));
  265. $this->assertFalse($entity->has(['id', 'nope']));
  266. $entity = $this->getMock('\Cake\ORM\Entity', ['_getThings']);
  267. $entity->expects($this->once())->method('_getThings')
  268. ->will($this->returnValue(0));
  269. $this->assertTrue($entity->has('things'));
  270. }
  271. /**
  272. * Tests unsetProperty one property at a time
  273. *
  274. * @return void
  275. */
  276. public function testUnset()
  277. {
  278. $entity = new Entity(['id' => 1, 'name' => 'bar']);
  279. $entity->unsetProperty('id');
  280. $this->assertFalse($entity->has('id'));
  281. $this->assertTrue($entity->has('name'));
  282. $entity->unsetProperty('name');
  283. $this->assertFalse($entity->has('id'));
  284. }
  285. /**
  286. * Unsetting a property should not mark it as dirty.
  287. *
  288. * @return void
  289. */
  290. public function testUnsetMakesClean()
  291. {
  292. $entity = new Entity(['id' => 1, 'name' => 'bar']);
  293. $this->assertTrue($entity->dirty('name'));
  294. $entity->unsetProperty('name');
  295. $this->assertFalse($entity->dirty('name'), 'Removed properties are not dirty.');
  296. }
  297. /**
  298. * Tests unsetProperty whith multiple properties
  299. *
  300. * @return void
  301. */
  302. public function testUnsetMultiple()
  303. {
  304. $entity = new Entity(['id' => 1, 'name' => 'bar', 'thing' => 2]);
  305. $entity->unsetProperty(['id', 'thing']);
  306. $this->assertFalse($entity->has('id'));
  307. $this->assertTrue($entity->has('name'));
  308. $this->assertFalse($entity->has('thing'));
  309. }
  310. /**
  311. * Tests the magic __isset() method
  312. *
  313. * @return void
  314. */
  315. public function testMagicIsset()
  316. {
  317. $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
  318. $this->assertTrue(isset($entity->id));
  319. $this->assertTrue(isset($entity->name));
  320. $this->assertFalse(isset($entity->foo));
  321. $this->assertFalse(isset($entity->thing));
  322. }
  323. /**
  324. * Tests the magic __unset() method
  325. *
  326. * @return void
  327. */
  328. public function testMagicUnset()
  329. {
  330. $entity = $this->getMock('\Cake\ORM\Entity', ['unsetProperty']);
  331. $entity->expects($this->at(0))
  332. ->method('unsetProperty')
  333. ->with('foo');
  334. unset($entity->foo);
  335. }
  336. /**
  337. * Tests isset with array access
  338. *
  339. * @return void
  340. */
  341. public function testIssetArrayAccess()
  342. {
  343. $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
  344. $this->assertTrue(isset($entity['id']));
  345. $this->assertTrue(isset($entity['name']));
  346. $this->assertFalse(isset($entity['foo']));
  347. $this->assertFalse(isset($entity['thing']));
  348. }
  349. /**
  350. * Tests get property with array access
  351. *
  352. * @return void
  353. */
  354. public function testGetArrayAccess()
  355. {
  356. $entity = $this->getMock('\Cake\ORM\Entity', ['get']);
  357. $entity->expects($this->at(0))
  358. ->method('get')
  359. ->with('foo')
  360. ->will($this->returnValue('worked'));
  361. $entity->expects($this->at(1))
  362. ->method('get')
  363. ->with('bar')
  364. ->will($this->returnValue('worked too'));
  365. $this->assertEquals('worked', $entity['foo']);
  366. $this->assertEquals('worked too', $entity['bar']);
  367. }
  368. /**
  369. * Tests set with array access
  370. *
  371. * @return void
  372. */
  373. public function testSetArrayAccess()
  374. {
  375. $entity = $this->getMock('\Cake\ORM\Entity', ['set']);
  376. $entity->accessible('*', true);
  377. $entity->expects($this->at(0))
  378. ->method('set')
  379. ->with('foo', 1)
  380. ->will($this->returnSelf());
  381. $entity->expects($this->at(1))
  382. ->method('set')
  383. ->with('bar', 2)
  384. ->will($this->returnSelf());
  385. $entity['foo'] = 1;
  386. $entity['bar'] = 2;
  387. }
  388. /**
  389. * Tests unset with array access
  390. *
  391. * @return void
  392. */
  393. public function testUnsetArrayAccess()
  394. {
  395. $entity = $this->getMock('\Cake\ORM\Entity', ['unsetProperty']);
  396. $entity->expects($this->at(0))
  397. ->method('unsetProperty')
  398. ->with('foo');
  399. unset($entity['foo']);
  400. }
  401. /**
  402. * Tests that the method cache will only report the methods for the called class,
  403. * this is, calling methods defined in another entity will not cause a fatal error
  404. * when trying to call directly an inexistent method in another class
  405. *
  406. * @return void
  407. */
  408. public function testMethodCache()
  409. {
  410. $entity = $this->getMock('\Cake\ORM\Entity', ['_setFoo', '_getBar']);
  411. $entity2 = $this->getMock('\Cake\ORM\Entity', ['_setBar']);
  412. $entity->expects($this->once())->method('_setFoo');
  413. $entity->expects($this->once())->method('_getBar');
  414. $entity2->expects($this->once())->method('_setBar');
  415. $entity->set('foo', 1);
  416. $entity->get('bar');
  417. $entity2->set('bar', 1);
  418. }
  419. /**
  420. * Tests that long properties in the entity are inflected correctly
  421. *
  422. * @return void
  423. */
  424. public function testSetGetLongProperyNames()
  425. {
  426. $entity = $this->getMock('\Cake\ORM\Entity', ['_getVeryLongProperty', '_setVeryLongProperty']);
  427. $entity->expects($this->once())->method('_getVeryLongProperty');
  428. $entity->expects($this->once())->method('_setVeryLongProperty');
  429. $entity->get('very_long_property');
  430. $entity->set('very_long_property', 1);
  431. }
  432. /**
  433. * Tests serializing an entity as json
  434. *
  435. * @return void
  436. */
  437. public function testJsonSerialize()
  438. {
  439. $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  440. $entity = new Entity($data);
  441. $this->assertEquals(json_encode($data), json_encode($entity));
  442. }
  443. /**
  444. * Tests the extract method
  445. *
  446. * @return void
  447. */
  448. public function testExtract()
  449. {
  450. $entity = new Entity([
  451. 'id' => 1,
  452. 'title' => 'Foo',
  453. 'author_id' => 3
  454. ]);
  455. $expected = ['author_id' => 3, 'title' => 'Foo', ];
  456. $this->assertEquals($expected, $entity->extract(['author_id', 'title']));
  457. $expected = ['id' => 1];
  458. $this->assertEquals($expected, $entity->extract(['id']));
  459. $expected = [];
  460. $this->assertEquals($expected, $entity->extract([]));
  461. $expected = ['id' => 1, 'crazyness' => null];
  462. $this->assertEquals($expected, $entity->extract(['id', 'crazyness']));
  463. }
  464. /**
  465. * Tests dirty() method on a newly created object
  466. *
  467. * @return void
  468. */
  469. public function testDirty()
  470. {
  471. $entity = new Entity([
  472. 'id' => 1,
  473. 'title' => 'Foo',
  474. 'author_id' => 3
  475. ]);
  476. $this->assertTrue($entity->dirty('id'));
  477. $this->assertTrue($entity->dirty('title'));
  478. $this->assertTrue($entity->dirty('author_id'));
  479. $this->assertTrue($entity->dirty());
  480. $entity->dirty('id', false);
  481. $this->assertFalse($entity->dirty('id'));
  482. $this->assertTrue($entity->dirty('title'));
  483. $entity->dirty('title', false);
  484. $this->assertFalse($entity->dirty('title'));
  485. $this->assertTrue($entity->dirty());
  486. $entity->dirty('author_id', false);
  487. $this->assertFalse($entity->dirty());
  488. }
  489. /**
  490. * Tests dirty() when altering properties values and adding new ones
  491. *
  492. * @return void
  493. */
  494. public function testDirtyChangingProperties()
  495. {
  496. $entity = new Entity([
  497. 'title' => 'Foo',
  498. ]);
  499. $entity->dirty('title', false);
  500. $this->assertFalse($entity->dirty('title'));
  501. $entity->set('title', 'Foo');
  502. $this->assertTrue($entity->dirty('title'));
  503. $entity->set('title', 'Foo');
  504. $this->assertTrue($entity->dirty('title'));
  505. $entity->set('something', 'else');
  506. $this->assertTrue($entity->dirty('something'));
  507. }
  508. /**
  509. * Tests extract only dirty properties
  510. *
  511. * @return void
  512. */
  513. public function testExtractDirty()
  514. {
  515. $entity = new Entity([
  516. 'id' => 1,
  517. 'title' => 'Foo',
  518. 'author_id' => 3
  519. ]);
  520. $entity->dirty('id', false);
  521. $entity->dirty('title', false);
  522. $expected = ['author_id' => 3];
  523. $result = $entity->extract(['id', 'title', 'author_id'], true);
  524. $this->assertEquals($expected, $result);
  525. }
  526. /**
  527. * Tests the clean method
  528. *
  529. * @return void
  530. */
  531. public function testClean()
  532. {
  533. $entity = new Entity([
  534. 'id' => 1,
  535. 'title' => 'Foo',
  536. 'author_id' => 3
  537. ]);
  538. $this->assertTrue($entity->dirty('id'));
  539. $this->assertTrue($entity->dirty('title'));
  540. $this->assertTrue($entity->dirty('author_id'));
  541. $entity->clean();
  542. $this->assertFalse($entity->dirty('id'));
  543. $this->assertFalse($entity->dirty('title'));
  544. $this->assertFalse($entity->dirty('author_id'));
  545. }
  546. /**
  547. * Tests the isNew method
  548. *
  549. * @return void
  550. */
  551. public function testIsNew()
  552. {
  553. $data = [
  554. 'id' => 1,
  555. 'title' => 'Foo',
  556. 'author_id' => 3
  557. ];
  558. $entity = new Entity($data);
  559. $this->assertTrue($entity->isNew());
  560. $entity->isNew(true);
  561. $this->assertTrue($entity->isNew());
  562. $entity->isNew('derpy');
  563. $this->assertTrue($entity->isNew());
  564. $entity->isNew(false);
  565. $this->assertFalse($entity->isNew());
  566. }
  567. /**
  568. * Tests the constructor when passing the markClean option
  569. *
  570. * @return void
  571. */
  572. public function testConstructorWithClean()
  573. {
  574. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  575. ->setMethods(['clean'])
  576. ->disableOriginalConstructor()
  577. ->getMock();
  578. $entity->expects($this->never())->method('clean');
  579. $entity->__construct(['a' => 'b', 'c' => 'd']);
  580. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  581. ->setMethods(['clean'])
  582. ->disableOriginalConstructor()
  583. ->getMock();
  584. $entity->expects($this->once())->method('clean');
  585. $entity->__construct(['a' => 'b', 'c' => 'd'], ['markClean' => true]);
  586. }
  587. /**
  588. * Tests the constructor when passing the markClean option
  589. *
  590. * @return void
  591. */
  592. public function testConstructorWithMarkNew()
  593. {
  594. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  595. ->setMethods(['isNew'])
  596. ->disableOriginalConstructor()
  597. ->getMock();
  598. $entity->expects($this->never())->method('clean');
  599. $entity->__construct(['a' => 'b', 'c' => 'd']);
  600. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  601. ->setMethods(['isNew'])
  602. ->disableOriginalConstructor()
  603. ->getMock();
  604. $entity->expects($this->once())->method('isNew');
  605. $entity->__construct(['a' => 'b', 'c' => 'd'], ['markNew' => true]);
  606. }
  607. /**
  608. * Test toArray method.
  609. *
  610. * @return void
  611. */
  612. public function testToArray()
  613. {
  614. $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  615. $entity = new Entity($data);
  616. $this->assertEquals($data, $entity->toArray());
  617. }
  618. /**
  619. * Test toArray recursive.
  620. *
  621. * @return void
  622. */
  623. public function testToArrayRecursive()
  624. {
  625. $data = ['id' => 1, 'name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  626. $user = new Extending($data);
  627. $comments = [
  628. new NonExtending(['user_id' => 1, 'body' => 'Comment 1']),
  629. new NonExtending(['user_id' => 1, 'body' => 'Comment 2']),
  630. ];
  631. $user->comments = $comments;
  632. $user->profile = new Entity(['email' => 'mark@example.com']);
  633. $expected = [
  634. 'id' => 1,
  635. 'name' => 'James',
  636. 'age' => 20,
  637. 'phones' => ['123', '457'],
  638. 'profile' => ['email' => 'mark@example.com'],
  639. 'comments' => [
  640. ['user_id' => 1, 'body' => 'Comment 1'],
  641. ['user_id' => 1, 'body' => 'Comment 2'],
  642. ]
  643. ];
  644. $this->assertEquals($expected, $user->toArray());
  645. }
  646. /**
  647. * Test that get accessors are called when converting to arrays.
  648. *
  649. * @return void
  650. */
  651. public function testToArrayWithAccessor()
  652. {
  653. $entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
  654. $entity->accessible('*', true);
  655. $entity->set(['name' => 'Mark', 'email' => 'mark@example.com']);
  656. $entity->expects($this->any())
  657. ->method('_getName')
  658. ->will($this->returnValue('Jose'));
  659. $expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
  660. $this->assertEquals($expected, $entity->toArray());
  661. }
  662. /**
  663. * Test that toArray respects hidden properties.
  664. *
  665. * @return void
  666. */
  667. public function testToArrayHiddenProperties()
  668. {
  669. $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
  670. $entity = new Entity($data);
  671. $entity->hiddenProperties(['secret']);
  672. $this->assertEquals(['name' => 'mark', 'id' => 1], $entity->toArray());
  673. }
  674. /**
  675. * Test toArray includes 'virtual' properties.
  676. *
  677. * @return void
  678. */
  679. public function testToArrayVirtualProperties()
  680. {
  681. $entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
  682. $entity->accessible('*', true);
  683. $entity->expects($this->any())
  684. ->method('_getName')
  685. ->will($this->returnValue('Jose'));
  686. $entity->set(['email' => 'mark@example.com']);
  687. $entity->virtualProperties(['name']);
  688. $expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
  689. $this->assertEquals($expected, $entity->toArray());
  690. $this->assertEquals(['name'], $entity->virtualProperties());
  691. $entity->hiddenProperties(['name']);
  692. $expected = ['email' => 'mark@example.com'];
  693. $this->assertEquals($expected, $entity->toArray());
  694. $this->assertEquals(['name'], $entity->hiddenProperties());
  695. }
  696. /**
  697. * Tests the errors method
  698. *
  699. * @return void
  700. */
  701. public function testErrors()
  702. {
  703. $entity = new Entity;
  704. $this->assertEmpty($entity->errors());
  705. $this->assertSame($entity, $entity->errors('foo', 'bar'));
  706. $this->assertEquals(['bar'], $entity->errors('foo'));
  707. $this->assertEquals([], $entity->errors('boo'));
  708. $entity['boo'] = [
  709. 'someting' => 'stupid',
  710. 'and' => false
  711. ];
  712. $this->assertEquals([], $entity->errors('boo'));
  713. $entity->errors('foo', 'other error');
  714. $this->assertEquals(['bar', 'other error'], $entity->errors('foo'));
  715. $entity->errors('bar', ['something', 'bad']);
  716. $this->assertEquals(['something', 'bad'], $entity->errors('bar'));
  717. $expected = ['foo' => ['bar', 'other error'], 'bar' => ['something', 'bad']];
  718. $this->assertEquals($expected, $entity->errors());
  719. $errors = ['foo' => ['something'], 'bar' => 'else', 'baz' => ['error']];
  720. $this->assertSame($entity, $entity->errors($errors, null, true));
  721. $errors['bar'] = ['else'];
  722. $this->assertEquals($errors, $entity->errors());
  723. }
  724. /**
  725. * Tests that it is possible to get errors for nested entities
  726. *
  727. * @return void
  728. */
  729. public function testErrorsDeep()
  730. {
  731. $user = new Entity();
  732. $owner = new NonExtending();
  733. $author = new Extending([
  734. 'foo' => 'bar',
  735. 'thing' => 'baz',
  736. 'user' => $user,
  737. 'owner' => $owner
  738. ]);
  739. $author->errors('thing', ['this is a mistake']);
  740. $user->errors(['a' => ['error1'], 'b' => ['error2']]);
  741. $owner->errors(['c' => ['error3'], 'd' => ['error4']]);
  742. $expected = ['a' => ['error1'], 'b' => ['error2']];
  743. $this->assertEquals($expected, $author->errors('user'));
  744. $expected = ['c' => ['error3'], 'd' => ['error4']];
  745. $this->assertEquals($expected, $author->errors('owner'));
  746. $author->set('multiple', [$user, $owner]);
  747. $expected = [
  748. ['a' => ['error1'], 'b' => ['error2']],
  749. ['c' => ['error3'], 'd' => ['error4']]
  750. ];
  751. $this->assertEquals($expected, $author->errors('multiple'));
  752. $expected = [
  753. 'thing' => $author->errors('thing'),
  754. 'user' => $author->errors('user'),
  755. 'owner' => $author->errors('owner'),
  756. 'multiple' => $author->errors('multiple')
  757. ];
  758. $this->assertEquals($expected, $author->errors());
  759. }
  760. /**
  761. * Test that errors can be read with a path.
  762. *
  763. * @return void
  764. */
  765. public function testErrorPathReading()
  766. {
  767. $assoc = new Entity();
  768. $assoc2 = new NonExtending();
  769. $entity = new Extending([
  770. 'field' => 'value',
  771. 'one' => $assoc,
  772. 'many' => [$assoc2]
  773. ]);
  774. $entity->errors('wrong', 'Bad stuff');
  775. $assoc->errors('nope', 'Terrible things');
  776. $assoc2->errors('nope', 'Terrible things');
  777. $this->assertEquals(['Bad stuff'], $entity->errors('wrong'));
  778. $this->assertEquals(['Terrible things'], $entity->errors('many.0.nope'));
  779. $this->assertEquals(['Terrible things'], $entity->errors('one.nope'));
  780. $this->assertEquals(['nope' => ['Terrible things']], $entity->errors('one'));
  781. $this->assertEquals([0 => ['nope' => ['Terrible things']]], $entity->errors('many'));
  782. $this->assertEquals(['nope' => ['Terrible things']], $entity->errors('many.0'));
  783. $this->assertEquals([], $entity->errors('many.0.mistake'));
  784. $this->assertEquals([], $entity->errors('one.mistake'));
  785. $this->assertEquals([], $entity->errors('one.1.mistake'));
  786. $this->assertEquals([], $entity->errors('many.1.nope'));
  787. }
  788. /**
  789. * Tests that changing the value of a property will remove errors
  790. * stored for it
  791. *
  792. * @return void
  793. */
  794. public function testDirtyRemovesError()
  795. {
  796. $entity = new Entity(['a' => 'b']);
  797. $entity->errors('a', 'is not good');
  798. $entity->set('a', 'c');
  799. $this->assertEmpty($entity->errors('a'));
  800. $entity->errors('a', 'is not good');
  801. $entity->dirty('a', true);
  802. $this->assertEmpty($entity->errors('a'));
  803. }
  804. /**
  805. * Tests that marking an entity as clean will remove errors too
  806. *
  807. * @return void
  808. */
  809. public function testCleanRemovesErrors()
  810. {
  811. $entity = new Entity(['a' => 'b']);
  812. $entity->errors('a', 'is not good');
  813. $entity->clean();
  814. $this->assertEmpty($entity->errors());
  815. }
  816. /**
  817. * Tests accessible() method as a getter and setter
  818. *
  819. * @return void
  820. */
  821. public function testAccessible()
  822. {
  823. $entity = new Entity;
  824. $entity->accessible('*', false);
  825. $this->assertFalse($entity->accessible('foo'));
  826. $this->assertFalse($entity->accessible('bar'));
  827. $this->assertSame($entity, $entity->accessible('foo', true));
  828. $this->assertTrue($entity->accessible('foo'));
  829. $this->assertFalse($entity->accessible('bar'));
  830. $this->assertSame($entity, $entity->accessible('bar', true));
  831. $this->assertTrue($entity->accessible('foo'));
  832. $this->assertTrue($entity->accessible('bar'));
  833. $this->assertSame($entity, $entity->accessible('foo', false));
  834. $this->assertFalse($entity->accessible('foo'));
  835. $this->assertTrue($entity->accessible('bar'));
  836. $this->assertSame($entity, $entity->accessible('bar', false));
  837. $this->assertFalse($entity->accessible('foo'));
  838. $this->assertFalse($entity->accessible('bar'));
  839. }
  840. /**
  841. * Tests that an array can be used to set
  842. *
  843. * @return void
  844. */
  845. public function testAccessibleAsArray()
  846. {
  847. $entity = new Entity;
  848. $entity->accessible(['foo', 'bar', 'baz'], true);
  849. $this->assertTrue($entity->accessible('foo'));
  850. $this->assertTrue($entity->accessible('bar'));
  851. $this->assertTrue($entity->accessible('baz'));
  852. $entity->accessible('foo', false);
  853. $this->assertFalse($entity->accessible('foo'));
  854. $this->assertTrue($entity->accessible('bar'));
  855. $this->assertTrue($entity->accessible('baz'));
  856. $entity->accessible(['foo', 'bar', 'baz'], false);
  857. $this->assertFalse($entity->accessible('foo'));
  858. $this->assertFalse($entity->accessible('bar'));
  859. $this->assertFalse($entity->accessible('baz'));
  860. }
  861. /**
  862. * Tests that a wildcard can be used for setting accesible properties
  863. *
  864. * @return void
  865. */
  866. public function testAccessibleWildcard()
  867. {
  868. $entity = new Entity;
  869. $entity->accessible(['foo', 'bar', 'baz'], true);
  870. $this->assertTrue($entity->accessible('foo'));
  871. $this->assertTrue($entity->accessible('bar'));
  872. $this->assertTrue($entity->accessible('baz'));
  873. $entity->accessible('*', false);
  874. $this->assertFalse($entity->accessible('foo'));
  875. $this->assertFalse($entity->accessible('bar'));
  876. $this->assertFalse($entity->accessible('baz'));
  877. $this->assertFalse($entity->accessible('newOne'));
  878. $entity->accessible('*', true);
  879. $this->assertTrue($entity->accessible('foo'));
  880. $this->assertTrue($entity->accessible('bar'));
  881. $this->assertTrue($entity->accessible('baz'));
  882. $this->assertTrue($entity->accessible('newOne2'));
  883. }
  884. /**
  885. * Tests that only accessible properties can be set
  886. *
  887. * @return void
  888. */
  889. public function testSetWithAccessible()
  890. {
  891. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  892. $options = ['guard' => true];
  893. $entity->accessible('*', false);
  894. $entity->accessible('foo', true);
  895. $entity->set('bar', 3, $options);
  896. $entity->set('foo', 4, $options);
  897. $this->assertEquals(2, $entity->get('bar'));
  898. $this->assertEquals(4, $entity->get('foo'));
  899. $entity->accessible('bar', true);
  900. $entity->set('bar', 3, $options);
  901. $this->assertEquals(3, $entity->get('bar'));
  902. }
  903. /**
  904. * Tests that only accessible properties can be set
  905. *
  906. * @return void
  907. */
  908. public function testSetWithAccessibleWithArray()
  909. {
  910. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  911. $options = ['guard' => true];
  912. $entity->accessible('*', false);
  913. $entity->accessible('foo', true);
  914. $entity->set(['bar' => 3, 'foo' => 4], $options);
  915. $this->assertEquals(2, $entity->get('bar'));
  916. $this->assertEquals(4, $entity->get('foo'));
  917. $entity->accessible('bar', true);
  918. $entity->set(['bar' => 3, 'foo' => 5], $options);
  919. $this->assertEquals(3, $entity->get('bar'));
  920. $this->assertEquals(5, $entity->get('foo'));
  921. }
  922. /**
  923. * Test that accessible() and single property setting works.
  924. *
  925. * @return void
  926. */
  927. public function testSetWithAccessibleSingleProperty()
  928. {
  929. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  930. $entity->accessible('*', false);
  931. $entity->accessible('title', true);
  932. $entity->set(['title' => 'test', 'body' => 'Nope']);
  933. $this->assertEquals('test', $entity->title);
  934. $this->assertNull($entity->body);
  935. $entity->body = 'Yep';
  936. $this->assertEquals('Yep', $entity->body, 'Single set should bypass guards.');
  937. $entity->set('body', 'Yes');
  938. $this->assertEquals('Yes', $entity->body, 'Single set should bypass guards.');
  939. }
  940. /**
  941. * Tests the entity's __toString method
  942. *
  943. * @return void
  944. */
  945. public function testToString()
  946. {
  947. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  948. $this->assertEquals(json_encode($entity, JSON_PRETTY_PRINT), (string)$entity);
  949. }
  950. /**
  951. * Tests __debugInfo
  952. *
  953. * @return void
  954. */
  955. public function testDebugInfo()
  956. {
  957. $entity = new Entity(['foo' => 'bar'], ['markClean' => true]);
  958. $entity->accessible('name', true);
  959. $entity->virtualProperties(['baz']);
  960. $entity->dirty('foo', true);
  961. $entity->errors('foo', ['An error']);
  962. $entity->source('foos');
  963. $result = $entity->__debugInfo();
  964. $expected = [
  965. 'new' => true,
  966. 'accessible' => ['*' => true, 'name' => true],
  967. 'properties' => ['foo' => 'bar'],
  968. 'dirty' => ['foo' => true],
  969. 'original' => [],
  970. 'virtual' => ['baz'],
  971. 'errors' => ['foo' => ['An error']],
  972. 'repository' => 'foos'
  973. ];
  974. $this->assertSame($expected, $result);
  975. }
  976. /**
  977. * Tests the source method
  978. *
  979. * @return void
  980. */
  981. public function testSource()
  982. {
  983. $entity = new Entity;
  984. $this->assertNull($entity->source());
  985. $entity->source('foos');
  986. $this->assertEquals('foos', $entity->source());
  987. }
  988. /**
  989. * Provides empty values
  990. *
  991. * @return void
  992. */
  993. public function emptyNamesProvider()
  994. {
  995. return [[''], [null], [false]];
  996. }
  997. /**
  998. * Tests that trying to get an empty propery name throws exception
  999. *
  1000. * @dataProvider emptyNamesProvider
  1001. * @expectedException \InvalidArgumentException
  1002. * @return void
  1003. */
  1004. public function testEmptyProperties($property)
  1005. {
  1006. $entity = new Entity();
  1007. $entity->get($property);
  1008. }
  1009. /**
  1010. * Tests that setitng an empty property name does nothing
  1011. *
  1012. * @expectedException \InvalidArgumentException
  1013. * @dataProvider emptyNamesProvider
  1014. * @return void
  1015. */
  1016. public function testSetEmptyPropertyName($property)
  1017. {
  1018. $entity = new Entity();
  1019. $entity->set($property, 'bar');
  1020. }
  1021. }