EntityTest.php 35 KB

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