EntityTest.php 43 KB

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