EntityTest.php 36 KB

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