EntityTest.php 40 KB

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