EntityTest.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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->setAccess('*', 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->setAccess('*', 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->setAccess('*', 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->setVirtual(['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->assertArrayHasKey('id', $entity);
  538. $this->assertArrayHasKey('name', $entity);
  539. $this->assertArrayNotHasKey('foo', $entity);
  540. $this->assertArrayNotHasKey('thing', $entity);
  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->setAccess('*', 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->isDirty('id'));
  711. $this->assertTrue($entity->isDirty('title'));
  712. $this->assertTrue($entity->isDirty('author_id'));
  713. $this->assertTrue($entity->isDirty());
  714. $entity->setDirty('id', false);
  715. $this->assertFalse($entity->isDirty('id'));
  716. $this->assertTrue($entity->isDirty('title'));
  717. $entity->setDirty('title', false);
  718. $this->assertFalse($entity->isDirty('title'));
  719. $this->assertTrue($entity->isDirty(), 'should be dirty, one field left');
  720. $entity->setDirty('author_id', false);
  721. $this->assertFalse($entity->isDirty(), 'all fields are clean.');
  722. }
  723. /**
  724. * Test setDirty().
  725. *
  726. * @return void
  727. */
  728. public function testSetDirty()
  729. {
  730. $entity = new Entity([
  731. 'id' => 1,
  732. 'title' => 'Foo',
  733. 'author_id' => 3,
  734. ], ['markClean' => true]);
  735. $this->assertFalse($entity->isDirty());
  736. $this->assertSame($entity, $entity->setDirty('title'));
  737. $this->assertSame($entity, $entity->setDirty('id', false));
  738. $entity->setErrors(['title' => ['badness']]);
  739. $entity->setDirty('title', true);
  740. $this->assertEmpty($entity->getErrors('title'), 'Making a field dirty clears errors.');
  741. }
  742. /**
  743. * Tests dirty() when altering properties values and adding new ones
  744. *
  745. * @return void
  746. */
  747. public function testDirtyChangingProperties()
  748. {
  749. $entity = new Entity([
  750. 'title' => 'Foo',
  751. ]);
  752. $entity->setDirty('title', false);
  753. $this->assertFalse($entity->isDirty('title'));
  754. $entity->set('title', 'Foo');
  755. $this->assertTrue($entity->isDirty('title'));
  756. $entity->set('title', 'Foo');
  757. $this->assertTrue($entity->isDirty('title'));
  758. $entity->set('something', 'else');
  759. $this->assertTrue($entity->isDirty('something'));
  760. }
  761. /**
  762. * Tests extract only dirty properties
  763. *
  764. * @return void
  765. */
  766. public function testExtractDirty()
  767. {
  768. $entity = new Entity([
  769. 'id' => 1,
  770. 'title' => 'Foo',
  771. 'author_id' => 3,
  772. ]);
  773. $entity->setDirty('id', false);
  774. $entity->setDirty('title', false);
  775. $expected = ['author_id' => 3];
  776. $result = $entity->extract(['id', 'title', 'author_id'], true);
  777. $this->assertEquals($expected, $result);
  778. }
  779. /**
  780. * Tests the getDirty method
  781. *
  782. * @return void
  783. */
  784. public function testGetDirty()
  785. {
  786. $entity = new Entity([
  787. 'id' => 1,
  788. 'title' => 'Foo',
  789. 'author_id' => 3,
  790. ]);
  791. $expected = [
  792. 'id',
  793. 'title',
  794. 'author_id',
  795. ];
  796. $result = $entity->getDirty();
  797. $this->assertSame($expected, $entity->getDirty());
  798. }
  799. /**
  800. * Tests the clean method
  801. *
  802. * @return void
  803. */
  804. public function testClean()
  805. {
  806. $entity = new Entity([
  807. 'id' => 1,
  808. 'title' => 'Foo',
  809. 'author_id' => 3,
  810. ]);
  811. $this->assertTrue($entity->isDirty('id'));
  812. $this->assertTrue($entity->isDirty('title'));
  813. $this->assertTrue($entity->isDirty('author_id'));
  814. $entity->clean();
  815. $this->assertFalse($entity->isDirty('id'));
  816. $this->assertFalse($entity->isDirty('title'));
  817. $this->assertFalse($entity->isDirty('author_id'));
  818. }
  819. /**
  820. * Tests the isNew method
  821. *
  822. * @return void
  823. */
  824. public function testIsNew()
  825. {
  826. $data = [
  827. 'id' => 1,
  828. 'title' => 'Foo',
  829. 'author_id' => 3,
  830. ];
  831. $entity = new Entity($data);
  832. $this->assertTrue($entity->isNew());
  833. $entity->setNew(true);
  834. $this->assertTrue($entity->isNew());
  835. $entity->setNew(false);
  836. $this->assertFalse($entity->isNew());
  837. }
  838. /**
  839. * Tests the constructor when passing the markClean option
  840. *
  841. * @return void
  842. */
  843. public function testConstructorWithClean()
  844. {
  845. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  846. ->setMethods(['clean'])
  847. ->disableOriginalConstructor()
  848. ->getMock();
  849. $entity->expects($this->never())->method('clean');
  850. $entity->__construct(['a' => 'b', 'c' => 'd']);
  851. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  852. ->setMethods(['clean'])
  853. ->disableOriginalConstructor()
  854. ->getMock();
  855. $entity->expects($this->once())->method('clean');
  856. $entity->__construct(['a' => 'b', 'c' => 'd'], ['markClean' => true]);
  857. }
  858. /**
  859. * Tests the constructor when passing the markClean option
  860. *
  861. * @return void
  862. */
  863. public function testConstructorWithMarkNew()
  864. {
  865. $entity = $this->getMockBuilder('Cake\ORM\Entity')
  866. ->setMethods(['setNew', 'clean'])
  867. ->disableOriginalConstructor()
  868. ->getMock();
  869. $entity->expects($this->never())->method('clean');
  870. $entity->__construct(['a' => 'b', 'c' => 'd']);
  871. $entity = $this->getMockBuilder('Cake\ORM\Entity')
  872. ->setMethods(['setNew'])
  873. ->disableOriginalConstructor()
  874. ->getMock();
  875. $entity->expects($this->once())->method('setNew');
  876. $entity->__construct(['a' => 'b', 'c' => 'd'], ['markNew' => true]);
  877. }
  878. /**
  879. * Test toArray method.
  880. *
  881. * @return void
  882. */
  883. public function testToArray()
  884. {
  885. $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  886. $entity = new Entity($data);
  887. $this->assertEquals($data, $entity->toArray());
  888. }
  889. /**
  890. * Test toArray recursive.
  891. *
  892. * @return void
  893. */
  894. public function testToArrayRecursive()
  895. {
  896. $data = ['id' => 1, 'name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
  897. $user = new Extending($data);
  898. $comments = [
  899. new NonExtending(['user_id' => 1, 'body' => 'Comment 1']),
  900. new NonExtending(['user_id' => 1, 'body' => 'Comment 2']),
  901. ];
  902. $user->comments = $comments;
  903. $user->profile = new Entity(['email' => 'mark@example.com']);
  904. $expected = [
  905. 'id' => 1,
  906. 'name' => 'James',
  907. 'age' => 20,
  908. 'phones' => ['123', '457'],
  909. 'profile' => ['email' => 'mark@example.com'],
  910. 'comments' => [
  911. ['user_id' => 1, 'body' => 'Comment 1'],
  912. ['user_id' => 1, 'body' => 'Comment 2'],
  913. ],
  914. ];
  915. $this->assertEquals($expected, $user->toArray());
  916. }
  917. /**
  918. * Tests that an entity with entities and other misc types can be properly toArray'd
  919. *
  920. * @return void
  921. */
  922. public function testToArrayMixed()
  923. {
  924. $test = new Entity([
  925. 'id' => 1,
  926. 'foo' => [
  927. new Entity(['hi' => 'test']),
  928. 'notentity' => 1,
  929. ],
  930. ]);
  931. $expected = [
  932. 'id' => 1,
  933. 'foo' => [
  934. ['hi' => 'test'],
  935. 'notentity' => 1,
  936. ],
  937. ];
  938. $this->assertEquals($expected, $test->toArray());
  939. }
  940. /**
  941. * Test that get accessors are called when converting to arrays.
  942. *
  943. * @return void
  944. */
  945. public function testToArrayWithAccessor()
  946. {
  947. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  948. ->setMethods(['_getName'])
  949. ->getMock();
  950. $entity->setAccess('*', true);
  951. $entity->set(['name' => 'Mark', 'email' => 'mark@example.com']);
  952. $entity->expects($this->any())
  953. ->method('_getName')
  954. ->will($this->returnValue('Jose'));
  955. $expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
  956. $this->assertEquals($expected, $entity->toArray());
  957. }
  958. /**
  959. * Test that toArray respects hidden properties.
  960. *
  961. * @return void
  962. */
  963. public function testToArrayHiddenProperties()
  964. {
  965. $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
  966. $entity = new Entity($data);
  967. $entity->setHidden(['secret']);
  968. $this->assertEquals(['name' => 'mark', 'id' => 1], $entity->toArray());
  969. }
  970. /**
  971. * Tests setting hidden properties.
  972. *
  973. * @return void
  974. */
  975. public function testSetHidden()
  976. {
  977. $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
  978. $entity = new Entity($data);
  979. $entity->setHidden(['secret']);
  980. $result = $entity->getHidden();
  981. $this->assertSame(['secret'], $result);
  982. $entity->setHidden(['name']);
  983. $result = $entity->getHidden();
  984. $this->assertSame(['name'], $result);
  985. }
  986. /**
  987. * Tests setting hidden properties with merging.
  988. *
  989. * @return void
  990. */
  991. public function testSetHiddenWithMerge()
  992. {
  993. $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
  994. $entity = new Entity($data);
  995. $entity->setHidden(['secret'], true);
  996. $result = $entity->getHidden();
  997. $this->assertSame(['secret'], $result);
  998. $entity->setHidden(['name'], true);
  999. $result = $entity->getHidden();
  1000. $this->assertSame(['secret', 'name'], $result);
  1001. $entity->setHidden(['name'], true);
  1002. $result = $entity->getHidden();
  1003. $this->assertSame(['secret', 'name'], $result);
  1004. }
  1005. /**
  1006. * Test toArray includes 'virtual' properties.
  1007. *
  1008. * @return void
  1009. */
  1010. public function testToArrayVirtualProperties()
  1011. {
  1012. $entity = $this->getMockBuilder('\Cake\ORM\Entity')
  1013. ->setMethods(['_getName'])
  1014. ->getMock();
  1015. $entity->setAccess('*', true);
  1016. $entity->expects($this->any())
  1017. ->method('_getName')
  1018. ->will($this->returnValue('Jose'));
  1019. $entity->set(['email' => 'mark@example.com']);
  1020. $entity->setVirtual(['name']);
  1021. $expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
  1022. $this->assertEquals($expected, $entity->toArray());
  1023. $this->assertEquals(['name'], $entity->getVirtual());
  1024. $entity->setHidden(['name']);
  1025. $expected = ['email' => 'mark@example.com'];
  1026. $this->assertEquals($expected, $entity->toArray());
  1027. $this->assertEquals(['name'], $entity->getHidden());
  1028. }
  1029. /**
  1030. * Tests setting virtual properties with merging.
  1031. *
  1032. * @return void
  1033. */
  1034. public function testSetVirtualWithMerge()
  1035. {
  1036. $data = ['virtual' => 'sauce', 'name' => 'mark', 'id' => 1];
  1037. $entity = new Entity($data);
  1038. $entity->setVirtual(['virtual']);
  1039. $result = $entity->getVirtual();
  1040. $this->assertSame(['virtual'], $result);
  1041. $entity->setVirtual(['name'], true);
  1042. $result = $entity->getVirtual();
  1043. $this->assertSame(['virtual', 'name'], $result);
  1044. $entity->setVirtual(['name'], true);
  1045. $result = $entity->getVirtual();
  1046. $this->assertSame(['virtual', 'name'], $result);
  1047. }
  1048. /**
  1049. * Tests the errors method
  1050. *
  1051. * @group deprecated
  1052. * @return void
  1053. */
  1054. public function testErrors()
  1055. {
  1056. $this->deprecated(function () {
  1057. $entity = new Entity();
  1058. $this->assertEmpty($entity->errors());
  1059. $this->assertSame($entity, $entity->errors('foo', 'bar'));
  1060. $this->assertEquals(['bar'], $entity->errors('foo'));
  1061. $this->assertEquals([], $entity->errors('boo'));
  1062. $entity['boo'] = [
  1063. 'something' => 'stupid',
  1064. 'and' => false,
  1065. ];
  1066. $this->assertEquals([], $entity->errors('boo'));
  1067. $entity->errors('foo', 'other error');
  1068. $this->assertEquals(['bar', 'other error'], $entity->errors('foo'));
  1069. $entity->errors('bar', ['something', 'bad']);
  1070. $this->assertEquals(['something', 'bad'], $entity->errors('bar'));
  1071. $expected = ['foo' => ['bar', 'other error'], 'bar' => ['something', 'bad']];
  1072. $this->assertEquals($expected, $entity->errors());
  1073. $errors = ['foo' => ['something'], 'bar' => 'else', 'baz' => ['error']];
  1074. $this->assertSame($entity, $entity->errors($errors, null, true));
  1075. $errors['bar'] = ['else'];
  1076. $this->assertEquals($errors, $entity->errors());
  1077. });
  1078. }
  1079. /**
  1080. * Tests error getters and setters
  1081. *
  1082. * @return void
  1083. */
  1084. public function testGetErrorAndSetError()
  1085. {
  1086. $entity = new Entity();
  1087. $this->assertEmpty($entity->getErrors());
  1088. $entity->setError('foo', 'bar');
  1089. $this->assertEquals(['bar'], $entity->getError('foo'));
  1090. $expected = [
  1091. 'foo' => ['bar'],
  1092. ];
  1093. $result = $entity->getErrors();
  1094. $this->assertEquals($expected, $result);
  1095. $indexedErrors = [2 => ['foo' => 'bar']];
  1096. $entity = new Entity();
  1097. $entity->setError('indexes', $indexedErrors);
  1098. $expectedIndexed = [
  1099. 'indexes' => ['2' => ['foo' => 'bar']],
  1100. ];
  1101. $result = $entity->getErrors();
  1102. $this->assertEquals($expectedIndexed, $result);
  1103. }
  1104. /**
  1105. * Tests reading errors from nested validator
  1106. *
  1107. * @return void
  1108. */
  1109. public function testGetErrorNested()
  1110. {
  1111. $entity = new Entity();
  1112. $entity->setError('options', ['subpages' => ['_empty' => 'required']]);
  1113. $expected = [
  1114. 'subpages' => ['_empty' => 'required'],
  1115. ];
  1116. $this->assertEquals($expected, $entity->getError('options'));
  1117. $expected = ['_empty' => 'required'];
  1118. $this->assertEquals($expected, $entity->getError('options.subpages'));
  1119. }
  1120. /**
  1121. * Tests that it is possible to get errors for nested entities
  1122. *
  1123. * @return void
  1124. */
  1125. public function testErrorsDeep()
  1126. {
  1127. $user = new Entity();
  1128. $owner = new NonExtending();
  1129. $author = new Extending([
  1130. 'foo' => 'bar',
  1131. 'thing' => 'baz',
  1132. 'user' => $user,
  1133. 'owner' => $owner,
  1134. ]);
  1135. $author->setError('thing', ['this is a mistake']);
  1136. $user->setErrors(['a' => ['error1'], 'b' => ['error2']]);
  1137. $owner->setErrors(['c' => ['error3'], 'd' => ['error4']]);
  1138. $expected = ['a' => ['error1'], 'b' => ['error2']];
  1139. $this->assertEquals($expected, $author->getError('user'));
  1140. $expected = ['c' => ['error3'], 'd' => ['error4']];
  1141. $this->assertEquals($expected, $author->getError('owner'));
  1142. $author->set('multiple', [$user, $owner]);
  1143. $expected = [
  1144. ['a' => ['error1'], 'b' => ['error2']],
  1145. ['c' => ['error3'], 'd' => ['error4']],
  1146. ];
  1147. $this->assertEquals($expected, $author->getError('multiple'));
  1148. $expected = [
  1149. 'thing' => $author->getError('thing'),
  1150. 'user' => $author->getError('user'),
  1151. 'owner' => $author->getError('owner'),
  1152. 'multiple' => $author->getError('multiple'),
  1153. ];
  1154. $this->assertEquals($expected, $author->getErrors());
  1155. }
  1156. /**
  1157. * Tests that check if hasErrors() works
  1158. *
  1159. * @return void
  1160. */
  1161. public function testHasErrors()
  1162. {
  1163. $entity = new Entity();
  1164. $hasErrors = $entity->hasErrors();
  1165. $this->assertFalse($hasErrors);
  1166. $nestedEntity = new Entity();
  1167. $entity->set([
  1168. 'nested' => $nestedEntity,
  1169. ]);
  1170. $hasErrors = $entity->hasErrors();
  1171. $this->assertFalse($hasErrors);
  1172. $nestedEntity->setError('description', 'oops');
  1173. $hasErrors = $entity->hasErrors();
  1174. $this->assertTrue($hasErrors);
  1175. $hasErrors = $entity->hasErrors(false);
  1176. $this->assertFalse($hasErrors);
  1177. $entity->clean();
  1178. $hasErrors = $entity->hasErrors();
  1179. $this->assertTrue($hasErrors);
  1180. $hasErrors = $entity->hasErrors(false);
  1181. $this->assertFalse($hasErrors);
  1182. $nestedEntity->clean();
  1183. $hasErrors = $entity->hasErrors();
  1184. $this->assertFalse($hasErrors);
  1185. $entity->setError('foo', []);
  1186. $this->assertFalse($entity->hasErrors());
  1187. }
  1188. /**
  1189. * Test that errors can be read with a path.
  1190. *
  1191. * @return void
  1192. */
  1193. public function testErrorPathReading()
  1194. {
  1195. $assoc = new Entity();
  1196. $assoc2 = new NonExtending();
  1197. $entity = new Extending([
  1198. 'field' => 'value',
  1199. 'one' => $assoc,
  1200. 'many' => [$assoc2],
  1201. ]);
  1202. $entity->setError('wrong', 'Bad stuff');
  1203. $assoc->setError('nope', 'Terrible things');
  1204. $assoc2->setError('nope', 'Terrible things');
  1205. $this->assertEquals(['Bad stuff'], $entity->getError('wrong'));
  1206. $this->assertEquals(['Terrible things'], $entity->getError('many.0.nope'));
  1207. $this->assertEquals(['Terrible things'], $entity->getError('one.nope'));
  1208. $this->assertEquals(['nope' => ['Terrible things']], $entity->getError('one'));
  1209. $this->assertEquals([0 => ['nope' => ['Terrible things']]], $entity->getError('many'));
  1210. $this->assertEquals(['nope' => ['Terrible things']], $entity->getError('many.0'));
  1211. $this->assertEquals([], $entity->getError('many.0.mistake'));
  1212. $this->assertEquals([], $entity->getError('one.mistake'));
  1213. $this->assertEquals([], $entity->getError('one.1.mistake'));
  1214. $this->assertEquals([], $entity->getError('many.1.nope'));
  1215. }
  1216. /**
  1217. * Tests that changing the value of a property will remove errors
  1218. * stored for it
  1219. *
  1220. * @return void
  1221. */
  1222. public function testDirtyRemovesError()
  1223. {
  1224. $entity = new Entity(['a' => 'b']);
  1225. $entity->setError('a', 'is not good');
  1226. $entity->set('a', 'c');
  1227. $this->assertEmpty($entity->getError('a'));
  1228. $entity->setError('a', 'is not good');
  1229. $entity->setDirty('a', true);
  1230. $this->assertEmpty($entity->getError('a'));
  1231. }
  1232. /**
  1233. * Tests that marking an entity as clean will remove errors too
  1234. *
  1235. * @return void
  1236. */
  1237. public function testCleanRemovesErrors()
  1238. {
  1239. $entity = new Entity(['a' => 'b']);
  1240. $entity->setError('a', 'is not good');
  1241. $entity->clean();
  1242. $this->assertEmpty($entity->getErrors());
  1243. }
  1244. /**
  1245. * Tests accessible() method as a getter and setter
  1246. *
  1247. * @return void
  1248. */
  1249. public function testAccessible()
  1250. {
  1251. $entity = new Entity();
  1252. $entity->setAccess('*', false);
  1253. $this->assertFalse($entity->isAccessible('foo'));
  1254. $this->assertFalse($entity->isAccessible('bar'));
  1255. $this->assertSame($entity, $entity->setAccess('foo', true));
  1256. $this->assertTrue($entity->isAccessible('foo'));
  1257. $this->assertFalse($entity->isAccessible('bar'));
  1258. $this->assertSame($entity, $entity->setAccess('bar', true));
  1259. $this->assertTrue($entity->isAccessible('foo'));
  1260. $this->assertTrue($entity->isAccessible('bar'));
  1261. $this->assertSame($entity, $entity->setAccess('foo', false));
  1262. $this->assertFalse($entity->isAccessible('foo'));
  1263. $this->assertTrue($entity->isAccessible('bar'));
  1264. $this->assertSame($entity, $entity->setAccess('bar', false));
  1265. $this->assertFalse($entity->isAccessible('foo'));
  1266. $this->assertFalse($entity->isAccessible('bar'));
  1267. }
  1268. /**
  1269. * Tests that an array can be used to set
  1270. *
  1271. * @return void
  1272. */
  1273. public function testAccessibleAsArray()
  1274. {
  1275. $entity = new Entity();
  1276. $entity->setAccess(['foo', 'bar', 'baz'], true);
  1277. $this->assertTrue($entity->isAccessible('foo'));
  1278. $this->assertTrue($entity->isAccessible('bar'));
  1279. $this->assertTrue($entity->isAccessible('baz'));
  1280. $entity->setAccess('foo', false);
  1281. $this->assertFalse($entity->isAccessible('foo'));
  1282. $this->assertTrue($entity->isAccessible('bar'));
  1283. $this->assertTrue($entity->isAccessible('baz'));
  1284. $entity->setAccess(['foo', 'bar', 'baz'], false);
  1285. $this->assertFalse($entity->isAccessible('foo'));
  1286. $this->assertFalse($entity->isAccessible('bar'));
  1287. $this->assertFalse($entity->isAccessible('baz'));
  1288. }
  1289. /**
  1290. * Tests that a wildcard can be used for setting accessible properties
  1291. *
  1292. * @return void
  1293. */
  1294. public function testAccessibleWildcard()
  1295. {
  1296. $entity = new Entity();
  1297. $entity->setAccess(['foo', 'bar', 'baz'], true);
  1298. $this->assertTrue($entity->isAccessible('foo'));
  1299. $this->assertTrue($entity->isAccessible('bar'));
  1300. $this->assertTrue($entity->isAccessible('baz'));
  1301. $entity->setAccess('*', false);
  1302. $this->assertFalse($entity->isAccessible('foo'));
  1303. $this->assertFalse($entity->isAccessible('bar'));
  1304. $this->assertFalse($entity->isAccessible('baz'));
  1305. $this->assertFalse($entity->isAccessible('newOne'));
  1306. $entity->setAccess('*', true);
  1307. $this->assertTrue($entity->isAccessible('foo'));
  1308. $this->assertTrue($entity->isAccessible('bar'));
  1309. $this->assertTrue($entity->isAccessible('baz'));
  1310. $this->assertTrue($entity->isAccessible('newOne2'));
  1311. }
  1312. /**
  1313. * Tests that only accessible properties can be set
  1314. *
  1315. * @return void
  1316. */
  1317. public function testSetWithAccessible()
  1318. {
  1319. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  1320. $options = ['guard' => true];
  1321. $entity->setAccess('*', false);
  1322. $entity->setAccess('foo', true);
  1323. $entity->set('bar', 3, $options);
  1324. $entity->set('foo', 4, $options);
  1325. $this->assertEquals(2, $entity->get('bar'));
  1326. $this->assertEquals(4, $entity->get('foo'));
  1327. $entity->setAccess('bar', true);
  1328. $entity->set('bar', 3, $options);
  1329. $this->assertEquals(3, $entity->get('bar'));
  1330. }
  1331. /**
  1332. * Tests that only accessible properties can be set
  1333. *
  1334. * @return void
  1335. */
  1336. public function testSetWithAccessibleWithArray()
  1337. {
  1338. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  1339. $options = ['guard' => true];
  1340. $entity->setAccess('*', false);
  1341. $entity->setAccess('foo', true);
  1342. $entity->set(['bar' => 3, 'foo' => 4], $options);
  1343. $this->assertEquals(2, $entity->get('bar'));
  1344. $this->assertEquals(4, $entity->get('foo'));
  1345. $entity->setAccess('bar', true);
  1346. $entity->set(['bar' => 3, 'foo' => 5], $options);
  1347. $this->assertEquals(3, $entity->get('bar'));
  1348. $this->assertEquals(5, $entity->get('foo'));
  1349. }
  1350. /**
  1351. * Test that accessible() and single property setting works.
  1352. *
  1353. * @return void
  1354. */
  1355. public function testSetWithAccessibleSingleProperty()
  1356. {
  1357. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  1358. $entity->setAccess('*', false);
  1359. $entity->setAccess('title', true);
  1360. $entity->set(['title' => 'test', 'body' => 'Nope']);
  1361. $this->assertEquals('test', $entity->title);
  1362. $this->assertNull($entity->body);
  1363. $entity->body = 'Yep';
  1364. $this->assertEquals('Yep', $entity->body, 'Single set should bypass guards.');
  1365. $entity->set('body', 'Yes');
  1366. $this->assertEquals('Yes', $entity->body, 'Single set should bypass guards.');
  1367. }
  1368. /**
  1369. * Tests the entity's __toString method
  1370. *
  1371. * @return void
  1372. */
  1373. public function testToString()
  1374. {
  1375. $entity = new Entity(['foo' => 1, 'bar' => 2]);
  1376. $this->assertEquals(json_encode($entity, JSON_PRETTY_PRINT), (string)$entity);
  1377. }
  1378. /**
  1379. * Tests __debugInfo
  1380. *
  1381. * @return void
  1382. */
  1383. public function testDebugInfo()
  1384. {
  1385. $entity = new Entity(['foo' => 'bar'], ['markClean' => true]);
  1386. $entity->somethingElse = 'value';
  1387. $entity->setAccess('id', false);
  1388. $entity->setAccess('name', true);
  1389. $entity->setVirtual(['baz']);
  1390. $entity->setDirty('foo', true);
  1391. $entity->setError('foo', ['An error']);
  1392. $entity->setInvalidField('foo', 'a value');
  1393. $entity->setSource('foos');
  1394. $result = $entity->__debugInfo();
  1395. $expected = [
  1396. 'foo' => 'bar',
  1397. 'somethingElse' => 'value',
  1398. 'baz' => null,
  1399. '[new]' => true,
  1400. '[accessible]' => ['*' => true, 'id' => false, 'name' => true],
  1401. '[dirty]' => ['somethingElse' => true, 'foo' => true],
  1402. '[original]' => [],
  1403. '[virtual]' => ['baz'],
  1404. '[hasErrors]' => true,
  1405. '[errors]' => ['foo' => ['An error']],
  1406. '[invalid]' => ['foo' => 'a value'],
  1407. '[repository]' => 'foos',
  1408. ];
  1409. $this->assertSame($expected, $result);
  1410. }
  1411. /**
  1412. * Tests the source method
  1413. *
  1414. * @group deprecated
  1415. * @return void
  1416. */
  1417. public function testSource()
  1418. {
  1419. $this->deprecated(function () {
  1420. $entity = new Entity();
  1421. $this->assertNull($entity->source());
  1422. $entity->source('foos');
  1423. $this->assertEquals('foos', $entity->source());
  1424. });
  1425. }
  1426. /**
  1427. * Test the source getter
  1428. */
  1429. public function testGetAndSetSource()
  1430. {
  1431. $entity = new Entity();
  1432. $this->assertNull($entity->getSource());
  1433. $entity->setSource('foos');
  1434. $this->assertEquals('foos', $entity->getSource());
  1435. }
  1436. /**
  1437. * Provides empty values
  1438. *
  1439. * @return array
  1440. */
  1441. public function emptyNamesProvider()
  1442. {
  1443. return [[''], [null], [false]];
  1444. }
  1445. /**
  1446. * Tests that trying to get an empty property name throws exception
  1447. *
  1448. * @dataProvider emptyNamesProvider
  1449. * @return void
  1450. */
  1451. public function testEmptyProperties($property)
  1452. {
  1453. $this->expectException(\InvalidArgumentException::class);
  1454. $entity = new Entity();
  1455. $entity->get($property);
  1456. }
  1457. /**
  1458. * Tests that setting an empty property name does nothing
  1459. *
  1460. * @dataProvider emptyNamesProvider
  1461. * @return void
  1462. */
  1463. public function testSetEmptyPropertyName($property)
  1464. {
  1465. $this->expectException(\InvalidArgumentException::class);
  1466. $entity = new Entity();
  1467. $entity->set($property, 'bar');
  1468. }
  1469. /**
  1470. * Provides empty values
  1471. *
  1472. * @return void
  1473. */
  1474. public function testIsDirtyFromClone()
  1475. {
  1476. $entity = new Entity(
  1477. ['a' => 1, 'b' => 2],
  1478. ['markNew' => false, 'markClean' => true]
  1479. );
  1480. $this->assertFalse($entity->isNew());
  1481. $this->assertFalse($entity->isDirty());
  1482. $cloned = clone $entity;
  1483. $cloned->setNew(true);
  1484. $this->assertTrue($cloned->isDirty());
  1485. $this->assertTrue($cloned->isDirty('a'));
  1486. $this->assertTrue($cloned->isDirty('b'));
  1487. }
  1488. /**
  1489. * Provides empty values
  1490. *
  1491. * @group deprecated
  1492. * @return void
  1493. */
  1494. public function testDirtyFromClone()
  1495. {
  1496. $this->deprecated(function () {
  1497. $entity = new Entity(
  1498. ['a' => 1, 'b' => 2],
  1499. ['markNew' => false, 'markClean' => true]
  1500. );
  1501. $this->assertFalse($entity->isNew());
  1502. $this->assertFalse($entity->dirty());
  1503. $cloned = clone $entity;
  1504. $cloned->isNew(true);
  1505. $this->assertTrue($cloned->dirty());
  1506. $this->assertTrue($cloned->dirty('a'));
  1507. $this->assertTrue($cloned->dirty('b'));
  1508. });
  1509. }
  1510. /**
  1511. * Tests getInvalid and setInvalid
  1512. *
  1513. * @return void
  1514. */
  1515. public function testGetSetInvalid()
  1516. {
  1517. $entity = new Entity();
  1518. $return = $entity->setInvalid([
  1519. 'title' => 'albert',
  1520. 'body' => 'einstein',
  1521. ]);
  1522. $this->assertSame($entity, $return);
  1523. $this->assertSame([
  1524. 'title' => 'albert',
  1525. 'body' => 'einstein',
  1526. ], $entity->getInvalid());
  1527. $set = $entity->setInvalid([
  1528. 'title' => 'nikola',
  1529. 'body' => 'tesla',
  1530. ]);
  1531. $this->assertSame([
  1532. 'title' => 'albert',
  1533. 'body' => 'einstein',
  1534. ], $set->getInvalid());
  1535. $overwrite = $entity->setInvalid([
  1536. 'title' => 'nikola',
  1537. 'body' => 'tesla',
  1538. ], true);
  1539. $this->assertSame($entity, $overwrite);
  1540. $this->assertSame([
  1541. 'title' => 'nikola',
  1542. 'body' => 'tesla',
  1543. ], $entity->getInvalid());
  1544. }
  1545. /**
  1546. * Tests getInvalidField
  1547. *
  1548. * @return void
  1549. */
  1550. public function testGetSetInvalidField()
  1551. {
  1552. $entity = new Entity();
  1553. $return = $entity->setInvalidField('title', 'albert');
  1554. $this->assertSame($entity, $return);
  1555. $this->assertSame('albert', $entity->getInvalidField('title'));
  1556. $overwrite = $entity->setInvalidField('title', 'nikola');
  1557. $this->assertSame($entity, $overwrite);
  1558. $this->assertSame('nikola', $entity->getInvalidField('title'));
  1559. }
  1560. /**
  1561. * Tests getInvalidFieldNull
  1562. *
  1563. * @return void
  1564. */
  1565. public function testGetInvalidFieldNull()
  1566. {
  1567. $entity = new Entity();
  1568. $this->assertNull($entity->getInvalidField('foo'));
  1569. }
  1570. /**
  1571. * Test the isEmpty() check
  1572. *
  1573. * @return void
  1574. */
  1575. public function testIsEmpty()
  1576. {
  1577. $entity = new Entity([
  1578. 'array' => ['foo' => 'bar'],
  1579. 'emptyArray' => [],
  1580. 'object' => new \stdClass(),
  1581. 'string' => 'string',
  1582. 'emptyString' => '',
  1583. 'intZero' => 0,
  1584. 'intNotZero' => 1,
  1585. 'floatZero' => 0.0,
  1586. 'floatNonZero' => 1.5,
  1587. 'null' => null,
  1588. ]);
  1589. $this->assertFalse($entity->isEmpty('array'));
  1590. $this->assertTrue($entity->isEmpty('emptyArray'));
  1591. $this->assertFalse($entity->isEmpty('object'));
  1592. $this->assertFalse($entity->isEmpty('string'));
  1593. $this->assertTrue($entity->isEmpty('emptyString'));
  1594. $this->assertFalse($entity->isEmpty('intZero'));
  1595. $this->assertFalse($entity->isEmpty('intNotZero'));
  1596. $this->assertFalse($entity->isEmpty('floatZero'));
  1597. $this->assertFalse($entity->isEmpty('floatNonZero'));
  1598. $this->assertTrue($entity->isEmpty('null'));
  1599. }
  1600. /**
  1601. * Test hasValue()
  1602. *
  1603. * @return void
  1604. */
  1605. public function testHasValue()
  1606. {
  1607. $entity = new Entity([
  1608. 'array' => ['foo' => 'bar'],
  1609. 'emptyArray' => [],
  1610. 'object' => new \stdClass(),
  1611. 'string' => 'string',
  1612. 'emptyString' => '',
  1613. 'intZero' => 0,
  1614. 'intNotZero' => 1,
  1615. 'floatZero' => 0.0,
  1616. 'floatNonZero' => 1.5,
  1617. 'null' => null,
  1618. ]);
  1619. $this->assertTrue($entity->hasValue('array'));
  1620. $this->assertFalse($entity->hasValue('emptyArray'));
  1621. $this->assertTrue($entity->hasValue('object'));
  1622. $this->assertTrue($entity->hasValue('string'));
  1623. $this->assertFalse($entity->hasValue('emptyString'));
  1624. $this->assertTrue($entity->hasValue('intZero'));
  1625. $this->assertTrue($entity->hasValue('intNotZero'));
  1626. $this->assertTrue($entity->hasValue('floatZero'));
  1627. $this->assertTrue($entity->hasValue('floatNonZero'));
  1628. $this->assertFalse($entity->hasValue('null'));
  1629. }
  1630. }