EntityTest.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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. * Test that all original values are returned properly
  122. *
  123. * @return void
  124. */
  125. public function testExtractOriginalValues()
  126. {
  127. $entity = new Entity([
  128. 'id' => 1,
  129. 'title' => 'original',
  130. 'body' => 'no',
  131. 'null' => null,
  132. ], ['markNew' => true]);
  133. $entity->set('body', 'updated body');
  134. $result = $entity->getOriginalValues();
  135. $expected = [
  136. 'id' => 1,
  137. 'title' => 'original',
  138. 'body' => 'no',
  139. 'null' => null,
  140. ];
  141. $this->assertEquals($expected, $result);
  142. }
  143. /**
  144. * Tests setting a single property using a setter function
  145. *
  146. * @return void
  147. */
  148. public function testSetOneParamWithSetter()
  149. {
  150. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  151. ->setMethods(['_setName'])
  152. ->getMock();
  153. $entity->expects($this->once())->method('_setName')
  154. ->with('Jones')
  155. ->will($this->returnCallback(function ($name) {
  156. $this->assertEquals('Jones', $name);
  157. return 'Dr. ' . $name;
  158. }));
  159. $entity->set('name', 'Jones');
  160. $this->assertEquals('Dr. Jones', $entity->name);
  161. }
  162. /**
  163. * Tests setting multiple properties using a setter function
  164. *
  165. * @return void
  166. */
  167. public function testMultipleWithSetter()
  168. {
  169. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  170. ->setMethods(['_setName', '_setStuff'])
  171. ->getMock();
  172. $entity->accessible('*', true);
  173. $entity->expects($this->once())->method('_setName')
  174. ->with('Jones')
  175. ->will($this->returnCallback(function ($name) {
  176. $this->assertEquals('Jones', $name);
  177. return 'Dr. ' . $name;
  178. }));
  179. $entity->expects($this->once())->method('_setStuff')
  180. ->with(['a', 'b'])
  181. ->will($this->returnCallback(function ($stuff) {
  182. $this->assertEquals(['a', 'b'], $stuff);
  183. return ['c', 'd'];
  184. }));
  185. $entity->set(['name' => 'Jones', 'stuff' => ['a', 'b']]);
  186. $this->assertEquals('Dr. Jones', $entity->name);
  187. $this->assertEquals(['c', 'd'], $entity->stuff);
  188. }
  189. /**
  190. * Tests that it is possible to bypass the setters
  191. *
  192. * @return void
  193. */
  194. public function testBypassSetters()
  195. {
  196. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  197. ->setMethods(['_setName', '_setStuff'])
  198. ->getMock();
  199. $entity->accessible('*', true);
  200. $entity->expects($this->never())->method('_setName');
  201. $entity->expects($this->never())->method('_setStuff');
  202. $entity->set('name', 'Jones', ['setter' => false]);
  203. $this->assertEquals('Jones', $entity->name);
  204. $entity->set('stuff', 'Thing', ['setter' => false]);
  205. $this->assertEquals('Thing', $entity->stuff);
  206. $entity->set(['name' => 'foo', 'stuff' => 'bar'], ['setter' => false]);
  207. $this->assertEquals('bar', $entity->stuff);
  208. }
  209. /**
  210. * Tests that the constructor will set initial properties
  211. *
  212. * @return void
  213. */
  214. public function testConstructor()
  215. {
  216. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  217. ->setMethods(['set'])
  218. ->disableOriginalConstructor()
  219. ->getMock();
  220. $entity->expects($this->at(0))
  221. ->method('set')
  222. ->with(['a' => 'b', 'c' => 'd'], ['setter' => true, 'guard' => false]);
  223. $entity->expects($this->at(1))
  224. ->method('set')
  225. ->with(['foo' => 'bar'], ['setter' => false, 'guard' => false]);
  226. $entity->__construct(['a' => 'b', 'c' => 'd']);
  227. $entity->__construct(['foo' => 'bar'], ['useSetters' => false]);
  228. }
  229. /**
  230. * Tests that the constructor will set initial properties and pass the guard
  231. * option along
  232. *
  233. * @return void
  234. */
  235. public function testConstructorWithGuard()
  236. {
  237. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  238. ->setMethods(['set'])
  239. ->disableOriginalConstructor()
  240. ->getMock();
  241. $entity->expects($this->once())
  242. ->method('set')
  243. ->with(['foo' => 'bar'], ['setter' => true, 'guard' => true]);
  244. $entity->__construct(['foo' => 'bar'], ['guard' => true]);
  245. }
  246. /**
  247. * Tests getting properties with no custom getters
  248. *
  249. * @return void
  250. */
  251. public function testGetNoGetters()
  252. {
  253. $entity = new Entity(['id' => 1, 'foo' => 'bar']);
  254. $this->assertSame(1, $entity->get('id'));
  255. $this->assertSame('bar', $entity->get('foo'));
  256. }
  257. /**
  258. * Tests get with custom getter
  259. *
  260. * @return void
  261. */
  262. public function testGetCustomGetters()
  263. {
  264. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  265. ->setMethods(['_getName'])
  266. ->getMock();
  267. $entity->expects($this->any())
  268. ->method('_getName')
  269. ->with('Jones')
  270. ->will($this->returnCallback(function ($name) {
  271. return 'Dr. ' . $name;
  272. }));
  273. $entity->set('name', 'Jones');
  274. $this->assertEquals('Dr. Jones', $entity->get('name'));
  275. $this->assertEquals('Dr. Jones', $entity->get('name'));
  276. }
  277. /**
  278. * Tests get with custom getter
  279. *
  280. * @return void
  281. */
  282. public function testGetCustomGettersAfterSet()
  283. {
  284. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  285. ->setMethods(['_getName'])
  286. ->getMock();
  287. $entity->expects($this->any())
  288. ->method('_getName')
  289. ->will($this->returnCallback(function ($name) {
  290. return 'Dr. ' . $name;
  291. }));
  292. $entity->set('name', 'Jones');
  293. $this->assertEquals('Dr. Jones', $entity->get('name'));
  294. $this->assertEquals('Dr. Jones', $entity->get('name'));
  295. $entity->set('name', 'Mark');
  296. $this->assertEquals('Dr. Mark', $entity->get('name'));
  297. $this->assertEquals('Dr. Mark', $entity->get('name'));
  298. }
  299. /**
  300. * Tests that the get cache is cleared by unsetProperty.
  301. *
  302. * @return void
  303. */
  304. public function testGetCacheClearedByUnset()
  305. {
  306. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  307. ->setMethods(['_getName'])
  308. ->getMock();
  309. $entity->expects($this->any())->method('_getName')
  310. ->will($this->returnCallback(function ($name) {
  311. return 'Dr. ' . $name;
  312. }));
  313. $entity->set('name', 'Jones');
  314. $this->assertEquals('Dr. Jones', $entity->get('name'));
  315. $entity->unsetProperty('name');
  316. $this->assertEquals('Dr. ', $entity->get('name'));
  317. }
  318. /**
  319. * Test getting camelcased virtual fields.
  320. *
  321. * @return void
  322. */
  323. public function testGetCamelCasedProperties()
  324. {
  325. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  326. ->setMethods(['_getListIdName'])
  327. ->getMock();
  328. $entity->expects($this->any())->method('_getListIdName')
  329. ->will($this->returnCallback(function ($name) {
  330. return 'A name';
  331. }));
  332. $entity->virtualProperties(['ListIdName']);
  333. $this->assertSame('A name', $entity->list_id_name, 'underscored virtual field should be accessible');
  334. $this->assertSame('A name', $entity->listIdName, 'Camelbacked virtual field should be accessible');
  335. }
  336. /**
  337. * Test magic property setting with no custom setter
  338. *
  339. * @return void
  340. */
  341. public function testMagicSet()
  342. {
  343. $entity = new Entity();
  344. $entity->name = 'Jones';
  345. $this->assertEquals('Jones', $entity->name);
  346. $entity->name = 'George';
  347. $this->assertEquals('George', $entity->name);
  348. }
  349. /**
  350. * Tests magic set with custom setter function
  351. *
  352. * @return void
  353. */
  354. public function testMagicSetWithSetter()
  355. {
  356. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  357. ->setMethods(['_setName'])
  358. ->getMock();
  359. $entity->expects($this->once())->method('_setName')
  360. ->with('Jones')
  361. ->will($this->returnCallback(function ($name) {
  362. $this->assertEquals('Jones', $name);
  363. return 'Dr. ' . $name;
  364. }));
  365. $entity->name = 'Jones';
  366. $this->assertEquals('Dr. Jones', $entity->name);
  367. }
  368. /**
  369. * Tests magic set with custom setter function using a Title cased property
  370. *
  371. * @return void
  372. */
  373. public function testMagicSetWithSetterTitleCase()
  374. {
  375. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  376. ->setMethods(['_setName'])
  377. ->getMock();
  378. $entity->expects($this->once())
  379. ->method('_setName')
  380. ->with('Jones')
  381. ->will($this->returnCallback(function ($name) {
  382. $this->assertEquals('Jones', $name);
  383. return 'Dr. ' . $name;
  384. }));
  385. $entity->Name = 'Jones';
  386. $this->assertEquals('Dr. Jones', $entity->Name);
  387. }
  388. /**
  389. * Tests the magic getter with a custom getter function
  390. *
  391. * @return void
  392. */
  393. public function testMagicGetWithGetter()
  394. {
  395. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  396. ->setMethods(['_getName'])
  397. ->getMock();
  398. $entity->expects($this->once())->method('_getName')
  399. ->with('Jones')
  400. ->will($this->returnCallback(function ($name) {
  401. $this->assertSame('Jones', $name);
  402. return 'Dr. ' . $name;
  403. }));
  404. $entity->set('name', 'Jones');
  405. $this->assertEquals('Dr. Jones', $entity->name);
  406. }
  407. /**
  408. * Tests magic get with custom getter function using a Title cased property
  409. *
  410. * @return void
  411. */
  412. public function testMagicGetWithGetterTitleCase()
  413. {
  414. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  415. ->setMethods(['_getName'])
  416. ->getMock();
  417. $entity->expects($this->once())
  418. ->method('_getName')
  419. ->with('Jones')
  420. ->will($this->returnCallback(function ($name) {
  421. $this->assertEquals('Jones', $name);
  422. return 'Dr. ' . $name;
  423. }));
  424. $entity->set('Name', 'Jones');
  425. $this->assertEquals('Dr. Jones', $entity->Name);
  426. }
  427. /**
  428. * Test indirectly modifying internal properties
  429. *
  430. * @return void
  431. */
  432. public function testIndirectModification()
  433. {
  434. $entity = new Entity();
  435. $entity->things = ['a', 'b'];
  436. $entity->things[] = 'c';
  437. $this->assertEquals(['a', 'b', 'c'], $entity->things);
  438. }
  439. /**
  440. * Tests has() method
  441. *
  442. * @return void
  443. */
  444. public function testHas()
  445. {
  446. $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
  447. $this->assertTrue($entity->has('id'));
  448. $this->assertTrue($entity->has('name'));
  449. $this->assertFalse($entity->has('foo'));
  450. $this->assertFalse($entity->has('last_name'));
  451. $this->assertTrue($entity->has(['id']));
  452. $this->assertTrue($entity->has(['id', 'name']));
  453. $this->assertFalse($entity->has(['id', 'foo']));
  454. $this->assertFalse($entity->has(['id', 'nope']));
  455. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  456. ->setMethods(['_getThings'])
  457. ->getMock();
  458. $entity->expects($this->once())->method('_getThings')
  459. ->will($this->returnValue(0));
  460. $this->assertTrue($entity->has('things'));
  461. }
  462. /**
  463. * Tests unsetProperty one property at a time
  464. *
  465. * @return void
  466. */
  467. public function testUnset()
  468. {
  469. $entity = new Entity(['id' => 1, 'name' => 'bar']);
  470. $entity->unsetProperty('id');
  471. $this->assertFalse($entity->has('id'));
  472. $this->assertTrue($entity->has('name'));
  473. $entity->unsetProperty('name');
  474. $this->assertFalse($entity->has('id'));
  475. }
  476. /**
  477. * Unsetting a property should not mark it as dirty.
  478. *
  479. * @return void
  480. */
  481. public function testUnsetMakesClean()
  482. {
  483. $entity = new Entity(['id' => 1, 'name' => 'bar']);
  484. $this->assertTrue($entity->isDirty('name'));
  485. $entity->unsetProperty('name');
  486. $this->assertFalse($entity->isDirty('name'), 'Removed properties are not dirty.');
  487. }
  488. /**
  489. * Tests unsetProperty with multiple properties
  490. *
  491. * @return void
  492. */
  493. public function testUnsetMultiple()
  494. {
  495. $entity = new Entity(['id' => 1, 'name' => 'bar', 'thing' => 2]);
  496. $entity->unsetProperty(['id', 'thing']);
  497. $this->assertFalse($entity->has('id'));
  498. $this->assertTrue($entity->has('name'));
  499. $this->assertFalse($entity->has('thing'));
  500. }
  501. /**
  502. * Tests the magic __isset() method
  503. *
  504. * @return void
  505. */
  506. public function testMagicIsset()
  507. {
  508. $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
  509. $this->assertTrue(isset($entity->id));
  510. $this->assertTrue(isset($entity->name));
  511. $this->assertFalse(isset($entity->foo));
  512. $this->assertFalse(isset($entity->thing));
  513. }
  514. /**
  515. * Tests the magic __unset() method
  516. *
  517. * @return void
  518. */
  519. public function testMagicUnset()
  520. {
  521. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  522. ->setMethods(['unsetProperty'])
  523. ->getMock();
  524. $entity->expects($this->at(0))
  525. ->method('unsetProperty')
  526. ->with('foo');
  527. unset($entity->foo);
  528. }
  529. /**
  530. * Tests isset with array access
  531. *
  532. * @return void
  533. */
  534. public function testIssetArrayAccess()
  535. {
  536. $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
  537. $this->assertTrue(isset($entity['id']));
  538. $this->assertTrue(isset($entity['name']));
  539. $this->assertFalse(isset($entity['foo']));
  540. $this->assertFalse(isset($entity['thing']));
  541. }
  542. /**
  543. * Tests get property with array access
  544. *
  545. * @return void
  546. */
  547. public function testGetArrayAccess()
  548. {
  549. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  550. ->setMethods(['get'])
  551. ->getMock();
  552. $entity->expects($this->at(0))
  553. ->method('get')
  554. ->with('foo')
  555. ->will($this->returnValue('worked'));
  556. $entity->expects($this->at(1))
  557. ->method('get')
  558. ->with('bar')
  559. ->will($this->returnValue('worked too'));
  560. $this->assertEquals('worked', $entity['foo']);
  561. $this->assertEquals('worked too', $entity['bar']);
  562. }
  563. /**
  564. * Tests set with array access
  565. *
  566. * @return void
  567. */
  568. public function testSetArrayAccess()
  569. {
  570. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  571. ->setMethods(['set'])
  572. ->getMock();
  573. $entity->accessible('*', true);
  574. $entity->expects($this->at(0))
  575. ->method('set')
  576. ->with('foo', 1)
  577. ->will($this->returnSelf());
  578. $entity->expects($this->at(1))
  579. ->method('set')
  580. ->with('bar', 2)
  581. ->will($this->returnSelf());
  582. $entity['foo'] = 1;
  583. $entity['bar'] = 2;
  584. }
  585. /**
  586. * Tests unset with array access
  587. *
  588. * @return void
  589. */
  590. public function testUnsetArrayAccess()
  591. {
  592. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  593. ->setMethods(['unsetProperty'])
  594. ->getMock();
  595. $entity->expects($this->at(0))
  596. ->method('unsetProperty')
  597. ->with('foo');
  598. unset($entity['foo']);
  599. }
  600. /**
  601. * Tests that the method cache will only report the methods for the called class,
  602. * this is, calling methods defined in another entity will not cause a fatal error
  603. * when trying to call directly an inexistent method in another class
  604. *
  605. * @return void
  606. */
  607. public function testMethodCache()
  608. {
  609. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  610. ->setMethods(['_setFoo', '_getBar'])
  611. ->getMock();
  612. $entity2 = $this->getMockBuilder('\Cake\ORM\Entity')
  613. ->setMethods(['_setBar'])
  614. ->getMock();
  615. $entity->expects($this->once())->method('_setFoo');
  616. $entity->expects($this->once())->method('_getBar');
  617. $entity2->expects($this->once())->method('_setBar');
  618. $entity->set('foo', 1);
  619. $entity->get('bar');
  620. $entity2->set('bar', 1);
  621. }
  622. /**
  623. * Tests that long properties in the entity are inflected correctly
  624. *
  625. * @return void
  626. */
  627. public function testSetGetLongPropertyNames()
  628. {
  629. $entity = $this->getMockBUilder('\Cake\ORM\Entity')
  630. ->setMethods(['_getVeryLongProperty', '_setVeryLongProperty'])
  631. ->getMock();
  632. $entity->expects($this->once())->method('_getVeryLongProperty');
  633. $entity->expects($this->once())->method('_setVeryLongProperty');
  634. $entity->get('very_long_property');
  635. $entity->set('very_long_property', 1);
  636. }
  637. /**
  638. * Tests serializing an entity as json
  639. *
  640. * @return void
  641. */
  642. public function testJsonSerialize()
  643. {
  644. $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  645. $entity = new Entity($data);
  646. $this->assertEquals(json_encode($data), json_encode($entity));
  647. }
  648. /**
  649. * Tests serializing an entity as PHP
  650. *
  651. * @return void
  652. */
  653. public function testPhpSerialize()
  654. {
  655. $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  656. $entity = new Entity($data);
  657. $copy = unserialize(serialize($entity));
  658. $this->assertInstanceOf(Entity::class, $copy);
  659. $this->assertEquals($data, $copy->toArray());
  660. }
  661. /**
  662. * Tests that jsonSerialize is called recursively for contained entities
  663. *
  664. * @return void
  665. */
  666. public function testJsonSerializeRecursive()
  667. {
  668. $phone = $this->getMockBuilder(Entity::class)
  669. ->setMethods(['jsonSerialize'])
  670. ->getMock();
  671. $phone->expects($this->once())->method('jsonSerialize')->will($this->returnValue('12345'));
  672. $data = ['name' => 'James', 'age' => 20, 'phone' => $phone];
  673. $entity = new Entity($data);
  674. $expected = ['name' => 'James', 'age' => 20, 'phone' => '12345'];
  675. $this->assertEquals(json_encode($expected), json_encode($entity));
  676. }
  677. /**
  678. * Tests the extract method
  679. *
  680. * @return void
  681. */
  682. public function testExtract()
  683. {
  684. $entity = new Entity([
  685. 'id' => 1,
  686. 'title' => 'Foo',
  687. 'author_id' => 3
  688. ]);
  689. $expected = ['author_id' => 3, 'title' => 'Foo', ];
  690. $this->assertEquals($expected, $entity->extract(['author_id', 'title']));
  691. $expected = ['id' => 1];
  692. $this->assertEquals($expected, $entity->extract(['id']));
  693. $expected = [];
  694. $this->assertEquals($expected, $entity->extract([]));
  695. $expected = ['id' => 1, 'craziness' => null];
  696. $this->assertEquals($expected, $entity->extract(['id', 'craziness']));
  697. }
  698. /**
  699. * Tests isDirty() method on a newly created object
  700. *
  701. * @return void
  702. */
  703. public function testIsDirty()
  704. {
  705. $entity = new Entity([
  706. 'id' => 1,
  707. 'title' => 'Foo',
  708. 'author_id' => 3
  709. ]);
  710. $this->assertTrue($entity->dirty('id'));
  711. $this->assertTrue($entity->isDirty('id'));
  712. $this->assertTrue($entity->isDirty('title'));
  713. $this->assertTrue($entity->isDirty('author_id'));
  714. $this->assertTrue($entity->dirty());
  715. $this->assertTrue($entity->isDirty());
  716. $entity->dirty('id', false);
  717. $this->assertFalse($entity->dirty('id'));
  718. $this->assertTrue($entity->dirty('title'));
  719. $entity->setDirty('title', false);
  720. $this->assertFalse($entity->isDirty('title'));
  721. $this->assertTrue($entity->isDirty(), 'should be dirty, one field left');
  722. $entity->setDirty('author_id', false);
  723. $this->assertFalse($entity->isDirty(), 'all fields are clean.');
  724. }
  725. /**
  726. * Test setDirty().
  727. *
  728. * @return void
  729. */
  730. public function testSetDirty()
  731. {
  732. $entity = new Entity([
  733. 'id' => 1,
  734. 'title' => 'Foo',
  735. 'author_id' => 3
  736. ], ['markClean' => true]);
  737. $this->assertFalse($entity->isDirty());
  738. $this->assertSame($entity, $entity->setDirty('title', true));
  739. $this->assertSame($entity, $entity->setDirty('id', false));
  740. $entity->setErrors(['title' => ['badness']]);
  741. $entity->setDirty('title', true);
  742. $this->assertEmpty($entity->getErrors('title'), 'Making a field dirty clears errors.');
  743. }
  744. /**
  745. * Tests dirty() when altering properties values and adding new ones
  746. *
  747. * @return void
  748. */
  749. public function testDirtyChangingProperties()
  750. {
  751. $entity = new Entity([
  752. 'title' => 'Foo',
  753. ]);
  754. $entity->setDirty('title', false);
  755. $this->assertFalse($entity->isDirty('title'));
  756. $entity->set('title', 'Foo');
  757. $this->assertTrue($entity->isDirty('title'));
  758. $entity->set('title', 'Foo');
  759. $this->assertTrue($entity->isDirty('title'));
  760. $entity->set('something', 'else');
  761. $this->assertTrue($entity->isDirty('something'));
  762. }
  763. /**
  764. * Tests extract only dirty properties
  765. *
  766. * @return void
  767. */
  768. public function testExtractDirty()
  769. {
  770. $entity = new Entity([
  771. 'id' => 1,
  772. 'title' => 'Foo',
  773. 'author_id' => 3
  774. ]);
  775. $entity->setDirty('id', false);
  776. $entity->setDirty('title', false);
  777. $expected = ['author_id' => 3];
  778. $result = $entity->extract(['id', 'title', 'author_id'], true);
  779. $this->assertEquals($expected, $result);
  780. }
  781. /**
  782. * Tests the getDirty method
  783. *
  784. * @return void
  785. */
  786. public function testGetDirty()
  787. {
  788. $entity = new Entity([
  789. 'id' => 1,
  790. 'title' => 'Foo',
  791. 'author_id' => 3
  792. ]);
  793. $expected = [
  794. 'id',
  795. 'title',
  796. 'author_id'
  797. ];
  798. $result = $entity->getDirty();
  799. $this->assertSame($expected, $entity->getDirty());
  800. }
  801. /**
  802. * Tests the clean method
  803. *
  804. * @return void
  805. */
  806. public function testClean()
  807. {
  808. $entity = new Entity([
  809. 'id' => 1,
  810. 'title' => 'Foo',
  811. 'author_id' => 3
  812. ]);
  813. $this->assertTrue($entity->isDirty('id'));
  814. $this->assertTrue($entity->isDirty('title'));
  815. $this->assertTrue($entity->isDirty('author_id'));
  816. $entity->clean();
  817. $this->assertFalse($entity->isDirty('id'));
  818. $this->assertFalse($entity->isDirty('title'));
  819. $this->assertFalse($entity->isDirty('author_id'));
  820. }
  821. /**
  822. * Tests the isNew method
  823. *
  824. * @return void
  825. */
  826. public function testIsNew()
  827. {
  828. $data = [
  829. 'id' => 1,
  830. 'title' => 'Foo',
  831. 'author_id' => 3
  832. ];
  833. $entity = new Entity($data);
  834. $this->assertTrue($entity->isNew());
  835. $entity->isNew(true);
  836. $this->assertTrue($entity->isNew());
  837. $entity->isNew('derpy');
  838. $this->assertTrue($entity->isNew());
  839. $entity->isNew(false);
  840. $this->assertFalse($entity->isNew());
  841. }
  842. /**
  843. * Tests the constructor when passing the markClean option
  844. *
  845. * @return void
  846. */
  847. public function testConstructorWithClean()
  848. {
  849. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  850. ->setMethods(['clean'])
  851. ->disableOriginalConstructor()
  852. ->getMock();
  853. $entity->expects($this->never())->method('clean');
  854. $entity->__construct(['a' => 'b', 'c' => 'd']);
  855. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  856. ->setMethods(['clean'])
  857. ->disableOriginalConstructor()
  858. ->getMock();
  859. $entity->expects($this->once())->method('clean');
  860. $entity->__construct(['a' => 'b', 'c' => 'd'], ['markClean' => true]);
  861. }
  862. /**
  863. * Tests the constructor when passing the markClean option
  864. *
  865. * @return void
  866. */
  867. public function testConstructorWithMarkNew()
  868. {
  869. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  870. ->setMethods(['isNew', 'clean'])
  871. ->disableOriginalConstructor()
  872. ->getMock();
  873. $entity->expects($this->never())->method('clean');
  874. $entity->__construct(['a' => 'b', 'c' => 'd']);
  875. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  876. ->setMethods(['isNew'])
  877. ->disableOriginalConstructor()
  878. ->getMock();
  879. $entity->expects($this->once())->method('isNew');
  880. $entity->__construct(['a' => 'b', 'c' => 'd'], ['markNew' => true]);
  881. }
  882. /**
  883. * Test toArray method.
  884. *
  885. * @return void
  886. */
  887. public function testToArray()
  888. {
  889. $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  890. $entity = new Entity($data);
  891. $this->assertEquals($data, $entity->toArray());
  892. }
  893. /**
  894. * Test toArray recursive.
  895. *
  896. * @return void
  897. */
  898. public function testToArrayRecursive()
  899. {
  900. $data = ['id' => 1, 'name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  901. $user = new Extending($data);
  902. $comments = [
  903. new NonExtending(['user_id' => 1, 'body' => 'Comment 1']),
  904. new NonExtending(['user_id' => 1, 'body' => 'Comment 2']),
  905. ];
  906. $user->comments = $comments;
  907. $user->profile = new Entity(['email' => 'mark@example.com']);
  908. $expected = [
  909. 'id' => 1,
  910. 'name' => 'James',
  911. 'age' => 20,
  912. 'phones' => ['123', '457'],
  913. 'profile' => ['email' => 'mark@example.com'],
  914. 'comments' => [
  915. ['user_id' => 1, 'body' => 'Comment 1'],
  916. ['user_id' => 1, 'body' => 'Comment 2'],
  917. ]
  918. ];
  919. $this->assertEquals($expected, $user->toArray());
  920. }
  921. /**
  922. * Tests that an entity with entities and other misc types can be properly toArray'd
  923. *
  924. * @return void
  925. */
  926. public function testToArrayMixed()
  927. {
  928. $test = new Entity([
  929. 'id' => 1,
  930. 'foo' => [
  931. new Entity(['hi' => 'test']),
  932. 'notentity' => 1
  933. ]
  934. ]);
  935. $expected = [
  936. 'id' => 1,
  937. 'foo' => [
  938. ['hi' => 'test'],
  939. 'notentity' => 1
  940. ]
  941. ];
  942. $this->assertEquals($expected, $test->toArray());
  943. }
  944. /**
  945. * Test that get accessors are called when converting to arrays.
  946. *
  947. * @return void
  948. */
  949. public function testToArrayWithAccessor()
  950. {
  951. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  952. ->setMethods(['_getName'])
  953. ->getMock();
  954. $entity->accessible('*', true);
  955. $entity->set(['name' => 'Mark', 'email' => 'mark@example.com']);
  956. $entity->expects($this->any())
  957. ->method('_getName')
  958. ->will($this->returnValue('Jose'));
  959. $expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
  960. $this->assertEquals($expected, $entity->toArray());
  961. }
  962. /**
  963. * Test that toArray respects hidden properties.
  964. *
  965. * @return void
  966. */
  967. public function testToArrayHiddenProperties()
  968. {
  969. $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
  970. $entity = new Entity($data);
  971. $entity->hiddenProperties(['secret']);
  972. $this->assertEquals(['name' => 'mark', 'id' => 1], $entity->toArray());
  973. }
  974. /**
  975. * Tests setting hidden properties.
  976. *
  977. * @return void
  978. */
  979. public function testSetHidden()
  980. {
  981. $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
  982. $entity = new Entity($data);
  983. $entity->setHidden(['secret']);
  984. $result = $entity->getHidden();
  985. $this->assertSame(['secret'], $result);
  986. $entity->setHidden(['name']);
  987. $result = $entity->getHidden();
  988. $this->assertSame(['name'], $result);
  989. }
  990. /**
  991. * Tests setting hidden properties with merging.
  992. *
  993. * @return void
  994. */
  995. public function testSetHiddenWithMerge()
  996. {
  997. $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
  998. $entity = new Entity($data);
  999. $entity->setHidden(['secret'], true);
  1000. $result = $entity->getHidden();
  1001. $this->assertSame(['secret'], $result);
  1002. $entity->setHidden(['name'], true);
  1003. $result = $entity->getHidden();
  1004. $this->assertSame(['secret', 'name'], $result);
  1005. $entity->setHidden(['name'], true);
  1006. $result = $entity->getHidden();
  1007. $this->assertSame(['secret', 'name'], $result);
  1008. }
  1009. /**
  1010. * Test toArray includes 'virtual' properties.
  1011. *
  1012. * @return void
  1013. */
  1014. public function testToArrayVirtualProperties()
  1015. {
  1016. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  1017. ->setMethods(['_getName'])
  1018. ->getMock();
  1019. $entity->accessible('*', true);
  1020. $entity->expects($this->any())
  1021. ->method('_getName')
  1022. ->will($this->returnValue('Jose'));
  1023. $entity->set(['email' => 'mark@example.com']);
  1024. $entity->virtualProperties(['name']);
  1025. $expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
  1026. $this->assertEquals($expected, $entity->toArray());
  1027. $this->assertEquals(['name'], $entity->virtualProperties());
  1028. $entity->hiddenProperties(['name']);
  1029. $expected = ['email' => 'mark@example.com'];
  1030. $this->assertEquals($expected, $entity->toArray());
  1031. $this->assertEquals(['name'], $entity->hiddenProperties());
  1032. }
  1033. /**
  1034. * Tests setting virtual properties with merging.
  1035. *
  1036. * @return void
  1037. */
  1038. public function testSetVirtualWithMerge()
  1039. {
  1040. $data = ['virtual' => 'sauce', 'name' => 'mark', 'id' => 1];
  1041. $entity = new Entity($data);
  1042. $entity->setVirtual(['virtual']);
  1043. $result = $entity->getVirtual();
  1044. $this->assertSame(['virtual'], $result);
  1045. $entity->setVirtual(['name'], true);
  1046. $result = $entity->getVirtual();
  1047. $this->assertSame(['virtual', 'name'], $result);
  1048. $entity->setVirtual(['name'], true);
  1049. $result = $entity->getVirtual();
  1050. $this->assertSame(['virtual', 'name'], $result);
  1051. }
  1052. /**
  1053. * Tests the errors method
  1054. *
  1055. * @return void
  1056. */
  1057. public function testErrors()
  1058. {
  1059. $entity = new Entity();
  1060. $this->assertEmpty($entity->errors());
  1061. $this->assertSame($entity, $entity->errors('foo', 'bar'));
  1062. $this->assertEquals(['bar'], $entity->errors('foo'));
  1063. $this->assertEquals([], $entity->errors('boo'));
  1064. $entity['boo'] = [
  1065. 'something' => 'stupid',
  1066. 'and' => false
  1067. ];
  1068. $this->assertEquals([], $entity->errors('boo'));
  1069. $entity->errors('foo', 'other error');
  1070. $this->assertEquals(['bar', 'other error'], $entity->errors('foo'));
  1071. $entity->errors('bar', ['something', 'bad']);
  1072. $this->assertEquals(['something', 'bad'], $entity->errors('bar'));
  1073. $expected = ['foo' => ['bar', 'other error'], 'bar' => ['something', 'bad']];
  1074. $this->assertEquals($expected, $entity->errors());
  1075. $errors = ['foo' => ['something'], 'bar' => 'else', 'baz' => ['error']];
  1076. $this->assertSame($entity, $entity->errors($errors, null, true));
  1077. $errors['bar'] = ['else'];
  1078. $this->assertEquals($errors, $entity->errors());
  1079. }
  1080. /**
  1081. * Tests error getters and setters
  1082. *
  1083. * @return void
  1084. */
  1085. public function testGetAndSetErrors()
  1086. {
  1087. $entity = new Entity();
  1088. $this->assertEmpty($entity->getErrors());
  1089. $entity->setError('foo', 'bar');
  1090. $this->assertEquals(['bar'], $entity->errors('foo'));
  1091. $expected = [
  1092. 'foo' => ['bar']
  1093. ];
  1094. $result = $entity->getErrors();
  1095. $this->assertEquals($expected, $result);
  1096. }
  1097. /**
  1098. * Tests that it is possible to get errors for nested entities
  1099. *
  1100. * @return void
  1101. */
  1102. public function testErrorsDeep()
  1103. {
  1104. $user = new Entity();
  1105. $owner = new NonExtending();
  1106. $author = new Extending([
  1107. 'foo' => 'bar',
  1108. 'thing' => 'baz',
  1109. 'user' => $user,
  1110. 'owner' => $owner
  1111. ]);
  1112. $author->errors('thing', ['this is a mistake']);
  1113. $user->errors(['a' => ['error1'], 'b' => ['error2']]);
  1114. $owner->errors(['c' => ['error3'], 'd' => ['error4']]);
  1115. $expected = ['a' => ['error1'], 'b' => ['error2']];
  1116. $this->assertEquals($expected, $author->errors('user'));
  1117. $expected = ['c' => ['error3'], 'd' => ['error4']];
  1118. $this->assertEquals($expected, $author->errors('owner'));
  1119. $author->set('multiple', [$user, $owner]);
  1120. $expected = [
  1121. ['a' => ['error1'], 'b' => ['error2']],
  1122. ['c' => ['error3'], 'd' => ['error4']]
  1123. ];
  1124. $this->assertEquals($expected, $author->errors('multiple'));
  1125. $expected = [
  1126. 'thing' => $author->errors('thing'),
  1127. 'user' => $author->errors('user'),
  1128. 'owner' => $author->errors('owner'),
  1129. 'multiple' => $author->errors('multiple')
  1130. ];
  1131. $this->assertEquals($expected, $author->errors());
  1132. }
  1133. /**
  1134. * Test that errors can be read with a path.
  1135. *
  1136. * @return void
  1137. */
  1138. public function testErrorPathReading()
  1139. {
  1140. $assoc = new Entity();
  1141. $assoc2 = new NonExtending();
  1142. $entity = new Extending([
  1143. 'field' => 'value',
  1144. 'one' => $assoc,
  1145. 'many' => [$assoc2]
  1146. ]);
  1147. $entity->errors('wrong', 'Bad stuff');
  1148. $assoc->errors('nope', 'Terrible things');
  1149. $assoc2->errors('nope', 'Terrible things');
  1150. $this->assertEquals(['Bad stuff'], $entity->errors('wrong'));
  1151. $this->assertEquals(['Terrible things'], $entity->errors('many.0.nope'));
  1152. $this->assertEquals(['Terrible things'], $entity->errors('one.nope'));
  1153. $this->assertEquals(['nope' => ['Terrible things']], $entity->errors('one'));
  1154. $this->assertEquals([0 => ['nope' => ['Terrible things']]], $entity->errors('many'));
  1155. $this->assertEquals(['nope' => ['Terrible things']], $entity->errors('many.0'));
  1156. $this->assertEquals([], $entity->errors('many.0.mistake'));
  1157. $this->assertEquals([], $entity->errors('one.mistake'));
  1158. $this->assertEquals([], $entity->errors('one.1.mistake'));
  1159. $this->assertEquals([], $entity->errors('many.1.nope'));
  1160. }
  1161. /**
  1162. * Tests that changing the value of a property will remove errors
  1163. * stored for it
  1164. *
  1165. * @return void
  1166. */
  1167. public function testDirtyRemovesError()
  1168. {
  1169. $entity = new Entity(['a' => 'b']);
  1170. $entity->errors('a', 'is not good');
  1171. $entity->set('a', 'c');
  1172. $this->assertEmpty($entity->errors('a'));
  1173. $entity->errors('a', 'is not good');
  1174. $entity->dirty('a', true);
  1175. $this->assertEmpty($entity->errors('a'));
  1176. }
  1177. /**
  1178. * Tests that marking an entity as clean will remove errors too
  1179. *
  1180. * @return void
  1181. */
  1182. public function testCleanRemovesErrors()
  1183. {
  1184. $entity = new Entity(['a' => 'b']);
  1185. $entity->errors('a', 'is not good');
  1186. $entity->clean();
  1187. $this->assertEmpty($entity->errors());
  1188. }
  1189. /**
  1190. * Tests accessible() method as a getter and setter
  1191. *
  1192. * @return void
  1193. */
  1194. public function testAccessible()
  1195. {
  1196. $entity = new Entity();
  1197. $entity->accessible('*', false);
  1198. $this->assertFalse($entity->accessible('foo'));
  1199. $this->assertFalse($entity->accessible('bar'));
  1200. $this->assertSame($entity, $entity->accessible('foo', true));
  1201. $this->assertTrue($entity->accessible('foo'));
  1202. $this->assertFalse($entity->accessible('bar'));
  1203. $this->assertSame($entity, $entity->accessible('bar', true));
  1204. $this->assertTrue($entity->accessible('foo'));
  1205. $this->assertTrue($entity->accessible('bar'));
  1206. $this->assertSame($entity, $entity->accessible('foo', false));
  1207. $this->assertFalse($entity->accessible('foo'));
  1208. $this->assertTrue($entity->accessible('bar'));
  1209. $this->assertSame($entity, $entity->accessible('bar', false));
  1210. $this->assertFalse($entity->accessible('foo'));
  1211. $this->assertFalse($entity->accessible('bar'));
  1212. }
  1213. /**
  1214. * Tests that an array can be used to set
  1215. *
  1216. * @return void
  1217. */
  1218. public function testAccessibleAsArray()
  1219. {
  1220. $entity = new Entity();
  1221. $entity->accessible(['foo', 'bar', 'baz'], true);
  1222. $this->assertTrue($entity->accessible('foo'));
  1223. $this->assertTrue($entity->accessible('bar'));
  1224. $this->assertTrue($entity->accessible('baz'));
  1225. $entity->accessible('foo', false);
  1226. $this->assertFalse($entity->accessible('foo'));
  1227. $this->assertTrue($entity->accessible('bar'));
  1228. $this->assertTrue($entity->accessible('baz'));
  1229. $entity->accessible(['foo', 'bar', 'baz'], false);
  1230. $this->assertFalse($entity->accessible('foo'));
  1231. $this->assertFalse($entity->accessible('bar'));
  1232. $this->assertFalse($entity->accessible('baz'));
  1233. }
  1234. /**
  1235. * Tests that a wildcard can be used for setting accessible properties
  1236. *
  1237. * @return void
  1238. */
  1239. public function testAccessibleWildcard()
  1240. {
  1241. $entity = new Entity();
  1242. $entity->accessible(['foo', 'bar', 'baz'], true);
  1243. $this->assertTrue($entity->accessible('foo'));
  1244. $this->assertTrue($entity->accessible('bar'));
  1245. $this->assertTrue($entity->accessible('baz'));
  1246. $entity->accessible('*', false);
  1247. $this->assertFalse($entity->accessible('foo'));
  1248. $this->assertFalse($entity->accessible('bar'));
  1249. $this->assertFalse($entity->accessible('baz'));
  1250. $this->assertFalse($entity->accessible('newOne'));
  1251. $entity->accessible('*', true);
  1252. $this->assertTrue($entity->accessible('foo'));
  1253. $this->assertTrue($entity->accessible('bar'));
  1254. $this->assertTrue($entity->accessible('baz'));
  1255. $this->assertTrue($entity->accessible('newOne2'));
  1256. }
  1257. /**
  1258. * Tests that only accessible properties can be set
  1259. *
  1260. * @return void
  1261. */
  1262. public function testSetWithAccessible()
  1263. {
  1264. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  1265. $options = ['guard' => true];
  1266. $entity->accessible('*', false);
  1267. $entity->accessible('foo', true);
  1268. $entity->set('bar', 3, $options);
  1269. $entity->set('foo', 4, $options);
  1270. $this->assertEquals(2, $entity->get('bar'));
  1271. $this->assertEquals(4, $entity->get('foo'));
  1272. $entity->accessible('bar', true);
  1273. $entity->set('bar', 3, $options);
  1274. $this->assertEquals(3, $entity->get('bar'));
  1275. }
  1276. /**
  1277. * Tests that only accessible properties can be set
  1278. *
  1279. * @return void
  1280. */
  1281. public function testSetWithAccessibleWithArray()
  1282. {
  1283. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  1284. $options = ['guard' => true];
  1285. $entity->accessible('*', false);
  1286. $entity->accessible('foo', true);
  1287. $entity->set(['bar' => 3, 'foo' => 4], $options);
  1288. $this->assertEquals(2, $entity->get('bar'));
  1289. $this->assertEquals(4, $entity->get('foo'));
  1290. $entity->accessible('bar', true);
  1291. $entity->set(['bar' => 3, 'foo' => 5], $options);
  1292. $this->assertEquals(3, $entity->get('bar'));
  1293. $this->assertEquals(5, $entity->get('foo'));
  1294. }
  1295. /**
  1296. * Test that accessible() and single property setting works.
  1297. *
  1298. * @return void
  1299. */
  1300. public function testSetWithAccessibleSingleProperty()
  1301. {
  1302. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  1303. $entity->accessible('*', false);
  1304. $entity->accessible('title', true);
  1305. $entity->set(['title' => 'test', 'body' => 'Nope']);
  1306. $this->assertEquals('test', $entity->title);
  1307. $this->assertNull($entity->body);
  1308. $entity->body = 'Yep';
  1309. $this->assertEquals('Yep', $entity->body, 'Single set should bypass guards.');
  1310. $entity->set('body', 'Yes');
  1311. $this->assertEquals('Yes', $entity->body, 'Single set should bypass guards.');
  1312. }
  1313. /**
  1314. * Tests the entity's __toString method
  1315. *
  1316. * @return void
  1317. */
  1318. public function testToString()
  1319. {
  1320. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  1321. $this->assertEquals(json_encode($entity, JSON_PRETTY_PRINT), (string)$entity);
  1322. }
  1323. /**
  1324. * Tests __debugInfo
  1325. *
  1326. * @return void
  1327. */
  1328. public function testDebugInfo()
  1329. {
  1330. $entity = new Entity(['foo' => 'bar'], ['markClean' => true]);
  1331. $entity->somethingElse = 'value';
  1332. $entity->accessible('id', false);
  1333. $entity->accessible('name', true);
  1334. $entity->virtualProperties(['baz']);
  1335. $entity->setDirty('foo', true);
  1336. $entity->errors('foo', ['An error']);
  1337. $entity->invalid('foo', 'a value');
  1338. $entity->source('foos');
  1339. $result = $entity->__debugInfo();
  1340. $expected = [
  1341. 'foo' => 'bar',
  1342. 'somethingElse' => 'value',
  1343. 'baz' => null,
  1344. '[new]' => true,
  1345. '[accessible]' => ['*' => true, 'id' => false, 'name' => true],
  1346. '[dirty]' => ['somethingElse' => true, 'foo' => true],
  1347. '[original]' => [],
  1348. '[virtual]' => ['baz'],
  1349. '[errors]' => ['foo' => ['An error']],
  1350. '[invalid]' => ['foo' => 'a value'],
  1351. '[repository]' => 'foos'
  1352. ];
  1353. $this->assertSame($expected, $result);
  1354. }
  1355. /**
  1356. * Tests the source method
  1357. *
  1358. * @return void
  1359. */
  1360. public function testSource()
  1361. {
  1362. $entity = new Entity();
  1363. $this->assertNull($entity->source());
  1364. $entity->source('foos');
  1365. $this->assertEquals('foos', $entity->source());
  1366. }
  1367. /**
  1368. * Test the source getter
  1369. */
  1370. public function testGetAndSetSource()
  1371. {
  1372. $entity = new Entity();
  1373. $this->assertNull($entity->getSource());
  1374. $entity->setSource('foos');
  1375. $this->assertEquals('foos', $entity->getSource());
  1376. }
  1377. /**
  1378. * Provides empty values
  1379. *
  1380. * @return void
  1381. */
  1382. public function emptyNamesProvider()
  1383. {
  1384. return [[''], [null], [false]];
  1385. }
  1386. /**
  1387. * Tests that trying to get an empty property name throws exception
  1388. *
  1389. * @dataProvider emptyNamesProvider
  1390. * @return void
  1391. */
  1392. public function testEmptyProperties($property)
  1393. {
  1394. $this->expectException(\InvalidArgumentException::class);
  1395. $entity = new Entity();
  1396. $entity->get($property);
  1397. }
  1398. /**
  1399. * Tests that setting an empty property name does nothing
  1400. *
  1401. * @dataProvider emptyNamesProvider
  1402. * @return void
  1403. */
  1404. public function testSetEmptyPropertyName($property)
  1405. {
  1406. $this->expectException(\InvalidArgumentException::class);
  1407. $entity = new Entity();
  1408. $entity->set($property, 'bar');
  1409. }
  1410. /**
  1411. * Provides empty values
  1412. *
  1413. * @return void
  1414. */
  1415. public function testIsDirtyFromClone()
  1416. {
  1417. $entity = new Entity(
  1418. ['a' => 1, 'b' => 2],
  1419. ['markNew' => false, 'markClean' => true]
  1420. );
  1421. $this->assertFalse($entity->isNew());
  1422. $this->assertFalse($entity->dirty());
  1423. $cloned = clone $entity;
  1424. $cloned->isNew(true);
  1425. $this->assertTrue($cloned->dirty());
  1426. $this->assertTrue($cloned->dirty('a'));
  1427. $this->assertTrue($cloned->dirty('b'));
  1428. }
  1429. /**
  1430. * Tests getInvalid and setInvalid
  1431. *
  1432. * @return void
  1433. */
  1434. public function testGetSetInvalid()
  1435. {
  1436. $entity = new Entity();
  1437. $return = $entity->setInvalid([
  1438. 'title' => 'albert',
  1439. 'body' => 'einstein'
  1440. ]);
  1441. $this->assertSame($entity, $return);
  1442. $this->assertSame([
  1443. 'title' => 'albert',
  1444. 'body' => 'einstein'
  1445. ], $entity->getInvalid());
  1446. $set = $entity->setInvalid([
  1447. 'title' => 'nikola',
  1448. 'body' => 'tesla'
  1449. ]);
  1450. $this->assertSame([
  1451. 'title' => 'albert',
  1452. 'body' => 'einstein'
  1453. ], $set->getInvalid());
  1454. $overwrite = $entity->setInvalid([
  1455. 'title' => 'nikola',
  1456. 'body' => 'tesla'
  1457. ], true);
  1458. $this->assertSame($entity, $overwrite);
  1459. $this->assertSame([
  1460. 'title' => 'nikola',
  1461. 'body' => 'tesla'
  1462. ], $entity->getInvalid());
  1463. }
  1464. /**
  1465. * Tests getInvalidField
  1466. *
  1467. * @return void
  1468. */
  1469. public function testGetSetInvalidField()
  1470. {
  1471. $entity = new Entity();
  1472. $return = $entity->setInvalidField('title', 'albert');
  1473. $this->assertSame($entity, $return);
  1474. $this->assertSame('albert', $entity->getInvalidField('title'));
  1475. $overwrite = $entity->setInvalidField('title', 'nikola');
  1476. $this->assertSame($entity, $overwrite);
  1477. $this->assertSame('nikola', $entity->getInvalidField('title'));
  1478. }
  1479. /**
  1480. * Tests getInvalidFieldNull
  1481. *
  1482. * @return void
  1483. */
  1484. public function testGetInvalidFieldNull()
  1485. {
  1486. $entity = new Entity();
  1487. $this->assertNull($entity->getInvalidField('foo'));
  1488. }
  1489. }