EntityTest.php 39 KB

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