EntityTest.php 42 KB

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