EntityTest.php 30 KB

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