EntityTest.php 42 KB

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