EntityTest.php 54 KB

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