EntityTest.php 40 KB

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