EntityTest.php 39 KB

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