| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\ORM;
- use Cake\ORM\Entity;
- use Cake\TestSuite\TestCase;
- use TestApp\Model\Entity\Extending;
- use TestApp\Model\Entity\NonExtending;
- /**
- * Entity test case.
- */
- class EntityTest extends TestCase
- {
- /**
- * Tests setting a single property in an entity without custom setters
- *
- * @return void
- */
- public function testSetOneParamNoSetters()
- {
- $entity = new Entity();
- $this->assertNull($entity->getOriginal('foo'));
- $entity->set('foo', 'bar');
- $this->assertEquals('bar', $entity->foo);
- $this->assertEquals('bar', $entity->getOriginal('foo'));
- $entity->set('foo', 'baz');
- $this->assertEquals('baz', $entity->foo);
- $this->assertEquals('bar', $entity->getOriginal('foo'));
- $entity->set('id', 1);
- $this->assertSame(1, $entity->id);
- $this->assertEquals(1, $entity->getOriginal('id'));
- $this->assertEquals('bar', $entity->getOriginal('foo'));
- }
- /**
- * Tests setting multiple properties without custom setters
- *
- * @return void
- */
- public function testSetMultiplePropertiesNoSetters()
- {
- $entity = new Entity();
- $entity->setAccess('*', true);
- $entity->set(['foo' => 'bar', 'id' => 1]);
- $this->assertEquals('bar', $entity->foo);
- $this->assertSame(1, $entity->id);
- $entity->set(['foo' => 'baz', 'id' => 2, 'thing' => 3]);
- $this->assertEquals('baz', $entity->foo);
- $this->assertSame(2, $entity->id);
- $this->assertSame(3, $entity->thing);
- $this->assertEquals('bar', $entity->getOriginal('foo'));
- $this->assertEquals(1, $entity->getOriginal('id'));
- }
- /**
- * Test that getOriginal() retains falsey values.
- *
- * @return void
- */
- public function testGetOriginal()
- {
- $entity = new Entity(
- ['false' => false, 'null' => null, 'zero' => 0, 'empty' => ''],
- ['markNew' => true]
- );
- $this->assertNull($entity->getOriginal('null'));
- $this->assertFalse($entity->getOriginal('false'));
- $this->assertSame(0, $entity->getOriginal('zero'));
- $this->assertSame('', $entity->getOriginal('empty'));
- $entity->set(['false' => 'y', 'null' => 'y', 'zero' => 'y', 'empty' => '']);
- $this->assertNull($entity->getOriginal('null'));
- $this->assertFalse($entity->getOriginal('false'));
- $this->assertSame(0, $entity->getOriginal('zero'));
- $this->assertSame('', $entity->getOriginal('empty'));
- }
- /**
- * Test extractOriginal()
- *
- * @return void
- */
- public function testExtractOriginal()
- {
- $entity = new Entity([
- 'id' => 1,
- 'title' => 'original',
- 'body' => 'no',
- 'null' => null,
- ], ['markNew' => true]);
- $entity->set('body', 'updated body');
- $result = $entity->extractOriginal(['id', 'title', 'body', 'null']);
- $expected = [
- 'id' => 1,
- 'title' => 'original',
- 'body' => 'no',
- 'null' => null,
- ];
- $this->assertEquals($expected, $result);
- $result = $entity->extractOriginalChanged(['id', 'title', 'body', 'null']);
- $expected = [
- 'body' => 'no',
- ];
- $this->assertEquals($expected, $result);
- $entity->set('null', 'not null');
- $result = $entity->extractOriginalChanged(['id', 'title', 'body', 'null']);
- $expected = [
- 'null' => null,
- 'body' => 'no',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that all original values are returned properly
- *
- * @return void
- */
- public function testExtractOriginalValues()
- {
- $entity = new Entity([
- 'id' => 1,
- 'title' => 'original',
- 'body' => 'no',
- 'null' => null,
- ], ['markNew' => true]);
- $entity->set('body', 'updated body');
- $result = $entity->getOriginalValues();
- $expected = [
- 'id' => 1,
- 'title' => 'original',
- 'body' => 'no',
- 'null' => null,
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests setting a single property using a setter function
- *
- * @return void
- */
- public function testSetOneParamWithSetter()
- {
- /** @var \Cake\ORM\Entity|\PHPUnit\Framework\MockObject\MockObject $entity */
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_setName'])
- ->getMock();
- $entity->expects($this->once())->method('_setName')
- ->with('Jones')
- ->will($this->returnCallback(function ($name) {
- $this->assertEquals('Jones', $name);
- return 'Dr. ' . $name;
- }));
- $entity->set('name', 'Jones');
- $this->assertEquals('Dr. Jones', $entity->name);
- }
- /**
- * Tests setting multiple properties using a setter function
- *
- * @return void
- */
- public function testMultipleWithSetter()
- {
- /** @var \Cake\ORM\Entity|\PHPUnit\Framework\MockObject\MockObject $entity */
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_setName', '_setStuff'])
- ->getMock();
- $entity->setAccess('*', true);
- $entity->expects($this->once())->method('_setName')
- ->with('Jones')
- ->will($this->returnCallback(function ($name) {
- $this->assertEquals('Jones', $name);
- return 'Dr. ' . $name;
- }));
- $entity->expects($this->once())->method('_setStuff')
- ->with(['a', 'b'])
- ->will($this->returnCallback(function ($stuff) {
- $this->assertEquals(['a', 'b'], $stuff);
- return ['c', 'd'];
- }));
- $entity->set(['name' => 'Jones', 'stuff' => ['a', 'b']]);
- $this->assertEquals('Dr. Jones', $entity->name);
- $this->assertEquals(['c', 'd'], $entity->stuff);
- }
- /**
- * Tests that it is possible to bypass the setters
- *
- * @return void
- */
- public function testBypassSetters()
- {
- /** @var \Cake\ORM\Entity|\PHPUnit\Framework\MockObject\MockObject $entity */
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_setName', '_setStuff'])
- ->getMock();
- $entity->setAccess('*', true);
- $entity->expects($this->never())->method('_setName');
- $entity->expects($this->never())->method('_setStuff');
- $entity->set('name', 'Jones', ['setter' => false]);
- $this->assertEquals('Jones', $entity->name);
- $entity->set('stuff', 'Thing', ['setter' => false]);
- $this->assertEquals('Thing', $entity->stuff);
- $entity->set(['name' => 'foo', 'stuff' => 'bar'], ['setter' => false]);
- $this->assertEquals('bar', $entity->stuff);
- }
- /**
- * Tests that the constructor will set initial properties
- *
- * @return void
- */
- public function testConstructor()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['set'])
- ->disableOriginalConstructor()
- ->getMock();
- $entity->expects($this->at(0))
- ->method('set')
- ->with(['a' => 'b', 'c' => 'd'], ['setter' => true, 'guard' => false]);
- $entity->expects($this->at(1))
- ->method('set')
- ->with(['foo' => 'bar'], ['setter' => false, 'guard' => false]);
- $entity->__construct(['a' => 'b', 'c' => 'd']);
- $entity->__construct(['foo' => 'bar'], ['useSetters' => false]);
- }
- /**
- * Tests that the constructor will set initial properties and pass the guard
- * option along
- *
- * @return void
- */
- public function testConstructorWithGuard()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['set'])
- ->disableOriginalConstructor()
- ->getMock();
- $entity->expects($this->once())
- ->method('set')
- ->with(['foo' => 'bar'], ['setter' => true, 'guard' => true]);
- $entity->__construct(['foo' => 'bar'], ['guard' => true]);
- }
- /**
- * Tests getting properties with no custom getters
- *
- * @return void
- */
- public function testGetNoGetters()
- {
- $entity = new Entity(['id' => 1, 'foo' => 'bar']);
- $this->assertSame(1, $entity->get('id'));
- $this->assertSame('bar', $entity->get('foo'));
- }
- /**
- * Tests get with custom getter
- *
- * @return void
- */
- public function testGetCustomGetters()
- {
- /** @var \Cake\ORM\Entity|\PHPUnit\Framework\MockObject\MockObject $entity */
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getName'])
- ->getMock();
- $entity->expects($this->any())
- ->method('_getName')
- ->with('Jones')
- ->will($this->returnCallback(function ($name) {
- return 'Dr. ' . $name;
- }));
- $entity->set('name', 'Jones');
- $this->assertEquals('Dr. Jones', $entity->get('name'));
- $this->assertEquals('Dr. Jones', $entity->get('name'));
- }
- /**
- * Tests get with custom getter
- *
- * @return void
- */
- public function testGetCustomGettersAfterSet()
- {
- /** @var \Cake\ORM\Entity|\PHPUnit\Framework\MockObject\MockObject $entity */
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getName'])
- ->getMock();
- $entity->expects($this->any())
- ->method('_getName')
- ->will($this->returnCallback(function ($name) {
- return 'Dr. ' . $name;
- }));
- $entity->set('name', 'Jones');
- $this->assertEquals('Dr. Jones', $entity->get('name'));
- $this->assertEquals('Dr. Jones', $entity->get('name'));
- $entity->set('name', 'Mark');
- $this->assertEquals('Dr. Mark', $entity->get('name'));
- $this->assertEquals('Dr. Mark', $entity->get('name'));
- }
- /**
- * Tests that the get cache is cleared by unsetProperty.
- *
- * @return void
- */
- public function testGetCacheClearedByUnset()
- {
- /** @var \Cake\ORM\Entity|\PHPUnit\Framework\MockObject\MockObject $entity */
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getName'])
- ->getMock();
- $entity->expects($this->any())->method('_getName')
- ->will($this->returnCallback(function ($name) {
- return 'Dr. ' . $name;
- }));
- $entity->set('name', 'Jones');
- $this->assertEquals('Dr. Jones', $entity->get('name'));
- $entity->unsetProperty('name');
- $this->assertEquals('Dr. ', $entity->get('name'));
- }
- /**
- * Test getting camelcased virtual fields.
- *
- * @return void
- */
- public function testGetCamelCasedProperties()
- {
- /** @var \Cake\ORM\Entity|\PHPUnit\Framework\MockObject\MockObject $entity */
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getListIdName'])
- ->getMock();
- $entity->expects($this->any())->method('_getListIdName')
- ->will($this->returnCallback(function ($name) {
- return 'A name';
- }));
- $entity->setVirtual(['ListIdName']);
- $this->assertSame('A name', $entity->list_id_name, 'underscored virtual field should be accessible');
- $this->assertSame('A name', $entity->listIdName, 'Camelbacked virtual field should be accessible');
- }
- /**
- * Test magic property setting with no custom setter
- *
- * @return void
- */
- public function testMagicSet()
- {
- $entity = new Entity();
- $entity->name = 'Jones';
- $this->assertEquals('Jones', $entity->name);
- $entity->name = 'George';
- $this->assertEquals('George', $entity->name);
- }
- /**
- * Tests magic set with custom setter function
- *
- * @return void
- */
- public function testMagicSetWithSetter()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_setName'])
- ->getMock();
- $entity->expects($this->once())->method('_setName')
- ->with('Jones')
- ->will($this->returnCallback(function ($name) {
- $this->assertEquals('Jones', $name);
- return 'Dr. ' . $name;
- }));
- $entity->name = 'Jones';
- $this->assertEquals('Dr. Jones', $entity->name);
- }
- /**
- * Tests magic set with custom setter function using a Title cased property
- *
- * @return void
- */
- public function testMagicSetWithSetterTitleCase()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_setName'])
- ->getMock();
- $entity->expects($this->once())
- ->method('_setName')
- ->with('Jones')
- ->will($this->returnCallback(function ($name) {
- $this->assertEquals('Jones', $name);
- return 'Dr. ' . $name;
- }));
- $entity->Name = 'Jones';
- $this->assertEquals('Dr. Jones', $entity->Name);
- }
- /**
- * Tests the magic getter with a custom getter function
- *
- * @return void
- */
- public function testMagicGetWithGetter()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getName'])
- ->getMock();
- $entity->expects($this->once())->method('_getName')
- ->with('Jones')
- ->will($this->returnCallback(function ($name) {
- $this->assertSame('Jones', $name);
- return 'Dr. ' . $name;
- }));
- $entity->set('name', 'Jones');
- $this->assertEquals('Dr. Jones', $entity->name);
- }
- /**
- * Tests magic get with custom getter function using a Title cased property
- *
- * @return void
- */
- public function testMagicGetWithGetterTitleCase()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getName'])
- ->getMock();
- $entity->expects($this->once())
- ->method('_getName')
- ->with('Jones')
- ->will($this->returnCallback(function ($name) {
- $this->assertEquals('Jones', $name);
- return 'Dr. ' . $name;
- }));
- $entity->set('Name', 'Jones');
- $this->assertEquals('Dr. Jones', $entity->Name);
- }
- /**
- * Test indirectly modifying internal properties
- *
- * @return void
- */
- public function testIndirectModification()
- {
- $entity = new Entity();
- $entity->things = ['a', 'b'];
- $entity->things[] = 'c';
- $this->assertEquals(['a', 'b', 'c'], $entity->things);
- }
- /**
- * Tests has() method
- *
- * @return void
- */
- public function testHas()
- {
- $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
- $this->assertTrue($entity->has('id'));
- $this->assertTrue($entity->has('name'));
- $this->assertFalse($entity->has('foo'));
- $this->assertFalse($entity->has('last_name'));
- $this->assertTrue($entity->has(['id']));
- $this->assertTrue($entity->has(['id', 'name']));
- $this->assertFalse($entity->has(['id', 'foo']));
- $this->assertFalse($entity->has(['id', 'nope']));
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getThings'])
- ->getMock();
- $entity->expects($this->once())->method('_getThings')
- ->will($this->returnValue(0));
- $this->assertTrue($entity->has('things'));
- }
- /**
- * Tests unsetProperty one property at a time
- *
- * @return void
- */
- public function testUnset()
- {
- $entity = new Entity(['id' => 1, 'name' => 'bar']);
- $entity->unsetProperty('id');
- $this->assertFalse($entity->has('id'));
- $this->assertTrue($entity->has('name'));
- $entity->unsetProperty('name');
- $this->assertFalse($entity->has('id'));
- }
- /**
- * Unsetting a property should not mark it as dirty.
- *
- * @return void
- */
- public function testUnsetMakesClean()
- {
- $entity = new Entity(['id' => 1, 'name' => 'bar']);
- $this->assertTrue($entity->isDirty('name'));
- $entity->unsetProperty('name');
- $this->assertFalse($entity->isDirty('name'), 'Removed properties are not dirty.');
- }
- /**
- * Tests unsetProperty with multiple properties
- *
- * @return void
- */
- public function testUnsetMultiple()
- {
- $entity = new Entity(['id' => 1, 'name' => 'bar', 'thing' => 2]);
- $entity->unsetProperty(['id', 'thing']);
- $this->assertFalse($entity->has('id'));
- $this->assertTrue($entity->has('name'));
- $this->assertFalse($entity->has('thing'));
- }
- /**
- * Tests the magic __isset() method
- *
- * @return void
- */
- public function testMagicIsset()
- {
- $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
- $this->assertTrue(isset($entity->id));
- $this->assertTrue(isset($entity->name));
- $this->assertFalse(isset($entity->foo));
- $this->assertFalse(isset($entity->thing));
- }
- /**
- * Tests the magic __unset() method
- *
- * @return void
- */
- public function testMagicUnset()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['unsetProperty'])
- ->getMock();
- $entity->expects($this->at(0))
- ->method('unsetProperty')
- ->with('foo');
- unset($entity->foo);
- }
- /**
- * Tests isset with array access
- *
- * @return void
- */
- public function testIssetArrayAccess()
- {
- $entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
- $this->assertArrayHasKey('id', $entity);
- $this->assertArrayHasKey('name', $entity);
- $this->assertArrayNotHasKey('foo', $entity);
- $this->assertArrayNotHasKey('thing', $entity);
- }
- /**
- * Tests get property with array access
- *
- * @return void
- */
- public function testGetArrayAccess()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['get'])
- ->getMock();
- $entity->expects($this->at(0))
- ->method('get')
- ->with('foo')
- ->will($this->returnValue('worked'));
- $entity->expects($this->at(1))
- ->method('get')
- ->with('bar')
- ->will($this->returnValue('worked too'));
- $this->assertEquals('worked', $entity['foo']);
- $this->assertEquals('worked too', $entity['bar']);
- }
- /**
- * Tests set with array access
- *
- * @return void
- */
- public function testSetArrayAccess()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['set'])
- ->getMock();
- $entity->setAccess('*', true);
- $entity->expects($this->at(0))
- ->method('set')
- ->with('foo', 1)
- ->will($this->returnSelf());
- $entity->expects($this->at(1))
- ->method('set')
- ->with('bar', 2)
- ->will($this->returnSelf());
- $entity['foo'] = 1;
- $entity['bar'] = 2;
- }
- /**
- * Tests unset with array access
- *
- * @return void
- */
- public function testUnsetArrayAccess()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['unsetProperty'])
- ->getMock();
- $entity->expects($this->at(0))
- ->method('unsetProperty')
- ->with('foo');
- unset($entity['foo']);
- }
- /**
- * Tests that the method cache will only report the methods for the called class,
- * this is, calling methods defined in another entity will not cause a fatal error
- * when trying to call directly an inexistent method in another class
- *
- * @return void
- */
- public function testMethodCache()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_setFoo', '_getBar'])
- ->getMock();
- $entity2 = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_setBar'])
- ->getMock();
- $entity->expects($this->once())->method('_setFoo');
- $entity->expects($this->once())->method('_getBar');
- $entity2->expects($this->once())->method('_setBar');
- $entity->set('foo', 1);
- $entity->get('bar');
- $entity2->set('bar', 1);
- }
- /**
- * Tests that long properties in the entity are inflected correctly
- *
- * @return void
- */
- public function testSetGetLongPropertyNames()
- {
- $entity = $this->getMockBUilder('\Cake\ORM\Entity')
- ->setMethods(['_getVeryLongProperty', '_setVeryLongProperty'])
- ->getMock();
- $entity->expects($this->once())->method('_getVeryLongProperty');
- $entity->expects($this->once())->method('_setVeryLongProperty');
- $entity->get('very_long_property');
- $entity->set('very_long_property', 1);
- }
- /**
- * Tests serializing an entity as json
- *
- * @return void
- */
- public function testJsonSerialize()
- {
- $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
- $entity = new Entity($data);
- $this->assertEquals(json_encode($data), json_encode($entity));
- }
- /**
- * Tests serializing an entity as PHP
- *
- * @return void
- */
- public function testPhpSerialize()
- {
- $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
- $entity = new Entity($data);
- $copy = unserialize(serialize($entity));
- $this->assertInstanceOf(Entity::class, $copy);
- $this->assertEquals($data, $copy->toArray());
- }
- /**
- * Tests that jsonSerialize is called recursively for contained entities
- *
- * @return void
- */
- public function testJsonSerializeRecursive()
- {
- $phone = $this->getMockBuilder(Entity::class)
- ->setMethods(['jsonSerialize'])
- ->getMock();
- $phone->expects($this->once())->method('jsonSerialize')->will($this->returnValue('12345'));
- $data = ['name' => 'James', 'age' => 20, 'phone' => $phone];
- $entity = new Entity($data);
- $expected = ['name' => 'James', 'age' => 20, 'phone' => '12345'];
- $this->assertEquals(json_encode($expected), json_encode($entity));
- }
- /**
- * Tests the extract method
- *
- * @return void
- */
- public function testExtract()
- {
- $entity = new Entity([
- 'id' => 1,
- 'title' => 'Foo',
- 'author_id' => 3,
- ]);
- $expected = ['author_id' => 3, 'title' => 'Foo', ];
- $this->assertEquals($expected, $entity->extract(['author_id', 'title']));
- $expected = ['id' => 1];
- $this->assertEquals($expected, $entity->extract(['id']));
- $expected = [];
- $this->assertEquals($expected, $entity->extract([]));
- $expected = ['id' => 1, 'craziness' => null];
- $this->assertEquals($expected, $entity->extract(['id', 'craziness']));
- }
- /**
- * Tests isDirty() method on a newly created object
- *
- * @return void
- */
- public function testIsDirty()
- {
- $entity = new Entity([
- 'id' => 1,
- 'title' => 'Foo',
- 'author_id' => 3,
- ]);
- $this->assertTrue($entity->isDirty('id'));
- $this->assertTrue($entity->isDirty('title'));
- $this->assertTrue($entity->isDirty('author_id'));
- $this->assertTrue($entity->isDirty());
- $entity->setDirty('id', false);
- $this->assertFalse($entity->isDirty('id'));
- $this->assertTrue($entity->isDirty('title'));
- $entity->setDirty('title', false);
- $this->assertFalse($entity->isDirty('title'));
- $this->assertTrue($entity->isDirty(), 'should be dirty, one field left');
- $entity->setDirty('author_id', false);
- $this->assertFalse($entity->isDirty(), 'all fields are clean.');
- }
- /**
- * Test setDirty().
- *
- * @return void
- */
- public function testSetDirty()
- {
- $entity = new Entity([
- 'id' => 1,
- 'title' => 'Foo',
- 'author_id' => 3,
- ], ['markClean' => true]);
- $this->assertFalse($entity->isDirty());
- $this->assertSame($entity, $entity->setDirty('title'));
- $this->assertSame($entity, $entity->setDirty('id', false));
- $entity->setErrors(['title' => ['badness']]);
- $entity->setDirty('title', true);
- $this->assertEmpty($entity->getErrors('title'), 'Making a field dirty clears errors.');
- }
- /**
- * Tests dirty() when altering properties values and adding new ones
- *
- * @return void
- */
- public function testDirtyChangingProperties()
- {
- $entity = new Entity([
- 'title' => 'Foo',
- ]);
- $entity->setDirty('title', false);
- $this->assertFalse($entity->isDirty('title'));
- $entity->set('title', 'Foo');
- $this->assertTrue($entity->isDirty('title'));
- $entity->set('title', 'Foo');
- $this->assertTrue($entity->isDirty('title'));
- $entity->set('something', 'else');
- $this->assertTrue($entity->isDirty('something'));
- }
- /**
- * Tests extract only dirty properties
- *
- * @return void
- */
- public function testExtractDirty()
- {
- $entity = new Entity([
- 'id' => 1,
- 'title' => 'Foo',
- 'author_id' => 3,
- ]);
- $entity->setDirty('id', false);
- $entity->setDirty('title', false);
- $expected = ['author_id' => 3];
- $result = $entity->extract(['id', 'title', 'author_id'], true);
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests the getDirty method
- *
- * @return void
- */
- public function testGetDirty()
- {
- $entity = new Entity([
- 'id' => 1,
- 'title' => 'Foo',
- 'author_id' => 3,
- ]);
- $expected = [
- 'id',
- 'title',
- 'author_id',
- ];
- $result = $entity->getDirty();
- $this->assertSame($expected, $entity->getDirty());
- }
- /**
- * Tests the clean method
- *
- * @return void
- */
- public function testClean()
- {
- $entity = new Entity([
- 'id' => 1,
- 'title' => 'Foo',
- 'author_id' => 3,
- ]);
- $this->assertTrue($entity->isDirty('id'));
- $this->assertTrue($entity->isDirty('title'));
- $this->assertTrue($entity->isDirty('author_id'));
- $entity->clean();
- $this->assertFalse($entity->isDirty('id'));
- $this->assertFalse($entity->isDirty('title'));
- $this->assertFalse($entity->isDirty('author_id'));
- }
- /**
- * Tests the isNew method
- *
- * @return void
- */
- public function testIsNew()
- {
- $data = [
- 'id' => 1,
- 'title' => 'Foo',
- 'author_id' => 3,
- ];
- $entity = new Entity($data);
- $this->assertTrue($entity->isNew());
- $entity->setNew(true);
- $this->assertTrue($entity->isNew());
- $entity->setNew(false);
- $this->assertFalse($entity->isNew());
- }
- /**
- * Tests the constructor when passing the markClean option
- *
- * @return void
- */
- public function testConstructorWithClean()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['clean'])
- ->disableOriginalConstructor()
- ->getMock();
- $entity->expects($this->never())->method('clean');
- $entity->__construct(['a' => 'b', 'c' => 'd']);
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['clean'])
- ->disableOriginalConstructor()
- ->getMock();
- $entity->expects($this->once())->method('clean');
- $entity->__construct(['a' => 'b', 'c' => 'd'], ['markClean' => true]);
- }
- /**
- * Tests the constructor when passing the markClean option
- *
- * @return void
- */
- public function testConstructorWithMarkNew()
- {
- $entity = $this->getMockBuilder('Cake\ORM\Entity')
- ->setMethods(['setNew', 'clean'])
- ->disableOriginalConstructor()
- ->getMock();
- $entity->expects($this->never())->method('clean');
- $entity->__construct(['a' => 'b', 'c' => 'd']);
- $entity = $this->getMockBuilder('Cake\ORM\Entity')
- ->setMethods(['setNew'])
- ->disableOriginalConstructor()
- ->getMock();
- $entity->expects($this->once())->method('setNew');
- $entity->__construct(['a' => 'b', 'c' => 'd'], ['markNew' => true]);
- }
- /**
- * Test toArray method.
- *
- * @return void
- */
- public function testToArray()
- {
- $data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
- $entity = new Entity($data);
- $this->assertEquals($data, $entity->toArray());
- }
- /**
- * Test toArray recursive.
- *
- * @return void
- */
- public function testToArrayRecursive()
- {
- $data = ['id' => 1, 'name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
- $user = new Extending($data);
- $comments = [
- new NonExtending(['user_id' => 1, 'body' => 'Comment 1']),
- new NonExtending(['user_id' => 1, 'body' => 'Comment 2']),
- ];
- $user->comments = $comments;
- $user->profile = new Entity(['email' => 'mark@example.com']);
- $expected = [
- 'id' => 1,
- 'name' => 'James',
- 'age' => 20,
- 'phones' => ['123', '457'],
- 'profile' => ['email' => 'mark@example.com'],
- 'comments' => [
- ['user_id' => 1, 'body' => 'Comment 1'],
- ['user_id' => 1, 'body' => 'Comment 2'],
- ],
- ];
- $this->assertEquals($expected, $user->toArray());
- }
- /**
- * Tests that an entity with entities and other misc types can be properly toArray'd
- *
- * @return void
- */
- public function testToArrayMixed()
- {
- $test = new Entity([
- 'id' => 1,
- 'foo' => [
- new Entity(['hi' => 'test']),
- 'notentity' => 1,
- ],
- ]);
- $expected = [
- 'id' => 1,
- 'foo' => [
- ['hi' => 'test'],
- 'notentity' => 1,
- ],
- ];
- $this->assertEquals($expected, $test->toArray());
- }
- /**
- * Test that get accessors are called when converting to arrays.
- *
- * @return void
- */
- public function testToArrayWithAccessor()
- {
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getName'])
- ->getMock();
- $entity->setAccess('*', true);
- $entity->set(['name' => 'Mark', 'email' => 'mark@example.com']);
- $entity->expects($this->any())
- ->method('_getName')
- ->will($this->returnValue('Jose'));
- $expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
- $this->assertEquals($expected, $entity->toArray());
- }
- /**
- * Test that toArray respects hidden properties.
- *
- * @return void
- */
- public function testToArrayHiddenProperties()
- {
- $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
- $entity = new Entity($data);
- $entity->setHidden(['secret']);
- $this->assertEquals(['name' => 'mark', 'id' => 1], $entity->toArray());
- }
- /**
- * Tests setting hidden properties.
- *
- * @return void
- */
- public function testSetHidden()
- {
- $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
- $entity = new Entity($data);
- $entity->setHidden(['secret']);
- $result = $entity->getHidden();
- $this->assertSame(['secret'], $result);
- $entity->setHidden(['name']);
- $result = $entity->getHidden();
- $this->assertSame(['name'], $result);
- }
- /**
- * Tests setting hidden properties with merging.
- *
- * @return void
- */
- public function testSetHiddenWithMerge()
- {
- $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
- $entity = new Entity($data);
- $entity->setHidden(['secret'], true);
- $result = $entity->getHidden();
- $this->assertSame(['secret'], $result);
- $entity->setHidden(['name'], true);
- $result = $entity->getHidden();
- $this->assertSame(['secret', 'name'], $result);
- $entity->setHidden(['name'], true);
- $result = $entity->getHidden();
- $this->assertSame(['secret', 'name'], $result);
- }
- /**
- * Test toArray includes 'virtual' properties.
- *
- * @return void
- */
- public function testToArrayVirtualProperties()
- {
- /** @var \Cake\ORM\Entity|\PHPUnit\Framework\MockObject\MockObject $entity */
- $entity = $this->getMockBuilder('\Cake\ORM\Entity')
- ->setMethods(['_getName'])
- ->getMock();
- $entity->setAccess('*', true);
- $entity->expects($this->any())
- ->method('_getName')
- ->will($this->returnValue('Jose'));
- $entity->set(['email' => 'mark@example.com']);
- $entity->setVirtual(['name']);
- $expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
- $this->assertEquals($expected, $entity->toArray());
- $this->assertEquals(['name'], $entity->getVirtual());
- $entity->setHidden(['name']);
- $expected = ['email' => 'mark@example.com'];
- $this->assertEquals($expected, $entity->toArray());
- $this->assertEquals(['name'], $entity->getHidden());
- }
- /**
- * Tests the getVisible() method
- *
- * @return void
- */
- public function testGetVisible()
- {
- $entity = new Entity();
- $entity->foo = 'foo';
- $entity->bar = 'bar';
- $expected = $entity->getVisible();
- $this->assertSame(['foo', 'bar'], $expected);
- }
- /**
- * Tests setting virtual properties with merging.
- *
- * @return void
- */
- public function testSetVirtualWithMerge()
- {
- $data = ['virtual' => 'sauce', 'name' => 'mark', 'id' => 1];
- $entity = new Entity($data);
- $entity->setVirtual(['virtual']);
- $result = $entity->getVirtual();
- $this->assertSame(['virtual'], $result);
- $entity->setVirtual(['name'], true);
- $result = $entity->getVirtual();
- $this->assertSame(['virtual', 'name'], $result);
- $entity->setVirtual(['name'], true);
- $result = $entity->getVirtual();
- $this->assertSame(['virtual', 'name'], $result);
- }
- /**
- * Tests getAccessible() method
- *
- * @return void
- */
- public function testGetAccessible()
- {
- $entity = new Entity();
- $entity->setAccess('*', false);
- $entity->setAccess('bar', true);
- $accessible = $entity->getAccessible();
- $expected = [
- '*' => false,
- 'bar' => true,
- ];
- $this->assertSame($expected, $accessible);
- }
- /**
- * Tests the errors method
- *
- * @group deprecated
- * @return void
- */
- public function testErrors()
- {
- $this->deprecated(function () {
- $entity = new Entity();
- $this->assertEmpty($entity->errors());
- $this->assertSame($entity, $entity->errors('foo', 'bar'));
- $this->assertEquals(['bar'], $entity->errors('foo'));
- $this->assertEquals([], $entity->errors('boo'));
- $entity['boo'] = [
- 'something' => 'stupid',
- 'and' => false,
- ];
- $this->assertEquals([], $entity->errors('boo'));
- $entity->errors('foo', 'other error');
- $this->assertEquals(['bar', 'other error'], $entity->errors('foo'));
- $entity->errors('bar', ['something', 'bad']);
- $this->assertEquals(['something', 'bad'], $entity->errors('bar'));
- $expected = ['foo' => ['bar', 'other error'], 'bar' => ['something', 'bad']];
- $this->assertEquals($expected, $entity->errors());
- $errors = ['foo' => ['something'], 'bar' => 'else', 'baz' => ['error']];
- $this->assertSame($entity, $entity->errors($errors, null, true));
- $errors['bar'] = ['else'];
- $this->assertEquals($errors, $entity->errors());
- });
- }
- /**
- * Tests error getters and setters
- *
- * @return void
- */
- public function testGetErrorAndSetError()
- {
- $entity = new Entity();
- $this->assertEmpty($entity->getErrors());
- $entity->setError('foo', 'bar');
- $this->assertEquals(['bar'], $entity->getError('foo'));
- $expected = [
- 'foo' => ['bar'],
- ];
- $result = $entity->getErrors();
- $this->assertEquals($expected, $result);
- $indexedErrors = [2 => ['foo' => 'bar']];
- $entity = new Entity();
- $entity->setError('indexes', $indexedErrors);
- $expectedIndexed = [
- 'indexes' => ['2' => ['foo' => 'bar']],
- ];
- $result = $entity->getErrors();
- $this->assertEquals($expectedIndexed, $result);
- }
- /**
- * Tests reading errors from nested validator
- *
- * @return void
- */
- public function testGetErrorNested()
- {
- $entity = new Entity();
- $entity->setError('options', ['subpages' => ['_empty' => 'required']]);
- $expected = [
- 'subpages' => ['_empty' => 'required'],
- ];
- $this->assertEquals($expected, $entity->getError('options'));
- $expected = ['_empty' => 'required'];
- $this->assertEquals($expected, $entity->getError('options.subpages'));
- }
- /**
- * Tests that it is possible to get errors for nested entities
- *
- * @return void
- */
- public function testErrorsDeep()
- {
- $user = new Entity();
- $owner = new NonExtending();
- $author = new Extending([
- 'foo' => 'bar',
- 'thing' => 'baz',
- 'user' => $user,
- 'owner' => $owner,
- ]);
- $author->setError('thing', ['this is a mistake']);
- $user->setErrors(['a' => ['error1'], 'b' => ['error2']]);
- $owner->setErrors(['c' => ['error3'], 'd' => ['error4']]);
- $expected = ['a' => ['error1'], 'b' => ['error2']];
- $this->assertEquals($expected, $author->getError('user'));
- $expected = ['c' => ['error3'], 'd' => ['error4']];
- $this->assertEquals($expected, $author->getError('owner'));
- $author->set('multiple', [$user, $owner]);
- $expected = [
- ['a' => ['error1'], 'b' => ['error2']],
- ['c' => ['error3'], 'd' => ['error4']],
- ];
- $this->assertEquals($expected, $author->getError('multiple'));
- $expected = [
- 'thing' => $author->getError('thing'),
- 'user' => $author->getError('user'),
- 'owner' => $author->getError('owner'),
- 'multiple' => $author->getError('multiple'),
- ];
- $this->assertEquals($expected, $author->getErrors());
- }
- /**
- * Tests that check if hasErrors() works
- *
- * @return void
- */
- public function testHasErrors()
- {
- $entity = new Entity();
- $hasErrors = $entity->hasErrors();
- $this->assertFalse($hasErrors);
- $nestedEntity = new Entity();
- $entity->set([
- 'nested' => $nestedEntity,
- ]);
- $hasErrors = $entity->hasErrors();
- $this->assertFalse($hasErrors);
- $nestedEntity->setError('description', 'oops');
- $hasErrors = $entity->hasErrors();
- $this->assertTrue($hasErrors);
- $hasErrors = $entity->hasErrors(false);
- $this->assertFalse($hasErrors);
- $entity->clean();
- $hasErrors = $entity->hasErrors();
- $this->assertTrue($hasErrors);
- $hasErrors = $entity->hasErrors(false);
- $this->assertFalse($hasErrors);
- $nestedEntity->clean();
- $hasErrors = $entity->hasErrors();
- $this->assertFalse($hasErrors);
- $entity->setError('foo', []);
- $this->assertFalse($entity->hasErrors());
- }
- /**
- * Test that errors can be read with a path.
- *
- * @return void
- */
- public function testErrorPathReading()
- {
- $assoc = new Entity();
- $assoc2 = new NonExtending();
- $entity = new Extending([
- 'field' => 'value',
- 'one' => $assoc,
- 'many' => [$assoc2],
- ]);
- $entity->setError('wrong', 'Bad stuff');
- $assoc->setError('nope', 'Terrible things');
- $assoc2->setError('nope', 'Terrible things');
- $this->assertEquals(['Bad stuff'], $entity->getError('wrong'));
- $this->assertEquals(['Terrible things'], $entity->getError('many.0.nope'));
- $this->assertEquals(['Terrible things'], $entity->getError('one.nope'));
- $this->assertEquals(['nope' => ['Terrible things']], $entity->getError('one'));
- $this->assertEquals([0 => ['nope' => ['Terrible things']]], $entity->getError('many'));
- $this->assertEquals(['nope' => ['Terrible things']], $entity->getError('many.0'));
- $this->assertEquals([], $entity->getError('many.0.mistake'));
- $this->assertEquals([], $entity->getError('one.mistake'));
- $this->assertEquals([], $entity->getError('one.1.mistake'));
- $this->assertEquals([], $entity->getError('many.1.nope'));
- }
- /**
- * Tests that changing the value of a property will remove errors
- * stored for it
- *
- * @return void
- */
- public function testDirtyRemovesError()
- {
- $entity = new Entity(['a' => 'b']);
- $entity->setError('a', 'is not good');
- $entity->set('a', 'c');
- $this->assertEmpty($entity->getError('a'));
- $entity->setError('a', 'is not good');
- $entity->setDirty('a', true);
- $this->assertEmpty($entity->getError('a'));
- }
- /**
- * Tests that marking an entity as clean will remove errors too
- *
- * @return void
- */
- public function testCleanRemovesErrors()
- {
- $entity = new Entity(['a' => 'b']);
- $entity->setError('a', 'is not good');
- $entity->clean();
- $this->assertEmpty($entity->getErrors());
- }
- /**
- * Tests accessible() method as a getter and setter
- *
- * @return void
- */
- public function testAccessible()
- {
- $entity = new Entity();
- $entity->setAccess('*', false);
- $this->assertFalse($entity->isAccessible('foo'));
- $this->assertFalse($entity->isAccessible('bar'));
- $this->assertSame($entity, $entity->setAccess('foo', true));
- $this->assertTrue($entity->isAccessible('foo'));
- $this->assertFalse($entity->isAccessible('bar'));
- $this->assertSame($entity, $entity->setAccess('bar', true));
- $this->assertTrue($entity->isAccessible('foo'));
- $this->assertTrue($entity->isAccessible('bar'));
- $this->assertSame($entity, $entity->setAccess('foo', false));
- $this->assertFalse($entity->isAccessible('foo'));
- $this->assertTrue($entity->isAccessible('bar'));
- $this->assertSame($entity, $entity->setAccess('bar', false));
- $this->assertFalse($entity->isAccessible('foo'));
- $this->assertFalse($entity->isAccessible('bar'));
- }
- /**
- * Tests that an array can be used to set
- *
- * @return void
- */
- public function testAccessibleAsArray()
- {
- $entity = new Entity();
- $entity->setAccess(['foo', 'bar', 'baz'], true);
- $this->assertTrue($entity->isAccessible('foo'));
- $this->assertTrue($entity->isAccessible('bar'));
- $this->assertTrue($entity->isAccessible('baz'));
- $entity->setAccess('foo', false);
- $this->assertFalse($entity->isAccessible('foo'));
- $this->assertTrue($entity->isAccessible('bar'));
- $this->assertTrue($entity->isAccessible('baz'));
- $entity->setAccess(['foo', 'bar', 'baz'], false);
- $this->assertFalse($entity->isAccessible('foo'));
- $this->assertFalse($entity->isAccessible('bar'));
- $this->assertFalse($entity->isAccessible('baz'));
- }
- /**
- * Tests that a wildcard can be used for setting accessible properties
- *
- * @return void
- */
- public function testAccessibleWildcard()
- {
- $entity = new Entity();
- $entity->setAccess(['foo', 'bar', 'baz'], true);
- $this->assertTrue($entity->isAccessible('foo'));
- $this->assertTrue($entity->isAccessible('bar'));
- $this->assertTrue($entity->isAccessible('baz'));
- $entity->setAccess('*', false);
- $this->assertFalse($entity->isAccessible('foo'));
- $this->assertFalse($entity->isAccessible('bar'));
- $this->assertFalse($entity->isAccessible('baz'));
- $this->assertFalse($entity->isAccessible('newOne'));
- $entity->setAccess('*', true);
- $this->assertTrue($entity->isAccessible('foo'));
- $this->assertTrue($entity->isAccessible('bar'));
- $this->assertTrue($entity->isAccessible('baz'));
- $this->assertTrue($entity->isAccessible('newOne2'));
- }
- /**
- * Tests that only accessible properties can be set
- *
- * @return void
- */
- public function testSetWithAccessible()
- {
- $entity = new Entity(['foo' => 1, 'bar' => 2]);
- $options = ['guard' => true];
- $entity->setAccess('*', false);
- $entity->setAccess('foo', true);
- $entity->set('bar', 3, $options);
- $entity->set('foo', 4, $options);
- $this->assertEquals(2, $entity->get('bar'));
- $this->assertEquals(4, $entity->get('foo'));
- $entity->setAccess('bar', true);
- $entity->set('bar', 3, $options);
- $this->assertEquals(3, $entity->get('bar'));
- }
- /**
- * Tests that only accessible properties can be set
- *
- * @return void
- */
- public function testSetWithAccessibleWithArray()
- {
- $entity = new Entity(['foo' => 1, 'bar' => 2]);
- $options = ['guard' => true];
- $entity->setAccess('*', false);
- $entity->setAccess('foo', true);
- $entity->set(['bar' => 3, 'foo' => 4], $options);
- $this->assertEquals(2, $entity->get('bar'));
- $this->assertEquals(4, $entity->get('foo'));
- $entity->setAccess('bar', true);
- $entity->set(['bar' => 3, 'foo' => 5], $options);
- $this->assertEquals(3, $entity->get('bar'));
- $this->assertEquals(5, $entity->get('foo'));
- }
- /**
- * Test that accessible() and single property setting works.
- *
- * @return void
- */
- public function testSetWithAccessibleSingleProperty()
- {
- $entity = new Entity(['foo' => 1, 'bar' => 2]);
- $entity->setAccess('*', false);
- $entity->setAccess('title', true);
- $entity->set(['title' => 'test', 'body' => 'Nope']);
- $this->assertEquals('test', $entity->title);
- $this->assertNull($entity->body);
- $entity->body = 'Yep';
- $this->assertEquals('Yep', $entity->body, 'Single set should bypass guards.');
- $entity->set('body', 'Yes');
- $this->assertEquals('Yes', $entity->body, 'Single set should bypass guards.');
- }
- /**
- * Tests the entity's __toString method
- *
- * @return void
- */
- public function testToString()
- {
- $entity = new Entity(['foo' => 1, 'bar' => 2]);
- $this->assertEquals(json_encode($entity, JSON_PRETTY_PRINT), (string)$entity);
- }
- /**
- * Tests __debugInfo
- *
- * @return void
- */
- public function testDebugInfo()
- {
- $entity = new Entity(['foo' => 'bar'], ['markClean' => true]);
- $entity->somethingElse = 'value';
- $entity->setAccess('id', false);
- $entity->setAccess('name', true);
- $entity->setVirtual(['baz']);
- $entity->setDirty('foo', true);
- $entity->setError('foo', ['An error']);
- $entity->setInvalidField('foo', 'a value');
- $entity->setSource('foos');
- $result = $entity->__debugInfo();
- $expected = [
- 'foo' => 'bar',
- 'somethingElse' => 'value',
- 'baz' => null,
- '[new]' => true,
- '[accessible]' => ['*' => true, 'id' => false, 'name' => true],
- '[dirty]' => ['somethingElse' => true, 'foo' => true],
- '[original]' => [],
- '[virtual]' => ['baz'],
- '[hasErrors]' => true,
- '[errors]' => ['foo' => ['An error']],
- '[invalid]' => ['foo' => 'a value'],
- '[repository]' => 'foos',
- ];
- $this->assertSame($expected, $result);
- }
- /**
- * Tests the source method
- *
- * @group deprecated
- * @return void
- */
- public function testSource()
- {
- $this->deprecated(function () {
- $entity = new Entity();
- $this->assertNull($entity->source());
- $entity->source('foos');
- $this->assertEquals('foos', $entity->source());
- });
- }
- /**
- * Test the source getter
- */
- public function testGetAndSetSource()
- {
- $entity = new Entity();
- $this->assertNull($entity->getSource());
- $entity->setSource('foos');
- $this->assertEquals('foos', $entity->getSource());
- }
- /**
- * Provides empty values
- *
- * @return array
- */
- public function emptyNamesProvider()
- {
- return [[''], [null], [false]];
- }
- /**
- * Tests that trying to get an empty property name throws exception
- *
- * @dataProvider emptyNamesProvider
- * @return void
- */
- public function testEmptyProperties($property)
- {
- $this->expectException(\InvalidArgumentException::class);
- $entity = new Entity();
- $entity->get($property);
- }
- /**
- * Tests that setting an empty property name does nothing
- *
- * @dataProvider emptyNamesProvider
- * @return void
- */
- public function testSetEmptyPropertyName($property)
- {
- $this->expectException(\InvalidArgumentException::class);
- $entity = new Entity();
- $entity->set($property, 'bar');
- }
- /**
- * Provides empty values
- *
- * @return void
- */
- public function testIsDirtyFromClone()
- {
- $entity = new Entity(
- ['a' => 1, 'b' => 2],
- ['markNew' => false, 'markClean' => true]
- );
- $this->assertFalse($entity->isNew());
- $this->assertFalse($entity->isDirty());
- $cloned = clone $entity;
- $cloned->setNew(true);
- $this->assertTrue($cloned->isDirty());
- $this->assertTrue($cloned->isDirty('a'));
- $this->assertTrue($cloned->isDirty('b'));
- }
- /**
- * Provides empty values
- *
- * @group deprecated
- * @return void
- */
- public function testDirtyFromClone()
- {
- $this->deprecated(function () {
- $entity = new Entity(
- ['a' => 1, 'b' => 2],
- ['markNew' => false, 'markClean' => true]
- );
- $this->assertFalse($entity->isNew());
- $this->assertFalse($entity->dirty());
- $cloned = clone $entity;
- $cloned->isNew(true);
- $this->assertTrue($cloned->dirty());
- $this->assertTrue($cloned->dirty('a'));
- $this->assertTrue($cloned->dirty('b'));
- });
- }
- /**
- * Tests getInvalid and setInvalid
- *
- * @return void
- */
- public function testGetSetInvalid()
- {
- $entity = new Entity();
- $return = $entity->setInvalid([
- 'title' => 'albert',
- 'body' => 'einstein',
- ]);
- $this->assertSame($entity, $return);
- $this->assertSame([
- 'title' => 'albert',
- 'body' => 'einstein',
- ], $entity->getInvalid());
- $set = $entity->setInvalid([
- 'title' => 'nikola',
- 'body' => 'tesla',
- ]);
- $this->assertSame([
- 'title' => 'albert',
- 'body' => 'einstein',
- ], $set->getInvalid());
- $overwrite = $entity->setInvalid([
- 'title' => 'nikola',
- 'body' => 'tesla',
- ], true);
- $this->assertSame($entity, $overwrite);
- $this->assertSame([
- 'title' => 'nikola',
- 'body' => 'tesla',
- ], $entity->getInvalid());
- }
- /**
- * Tests getInvalidField
- *
- * @return void
- */
- public function testGetSetInvalidField()
- {
- $entity = new Entity();
- $return = $entity->setInvalidField('title', 'albert');
- $this->assertSame($entity, $return);
- $this->assertSame('albert', $entity->getInvalidField('title'));
- $overwrite = $entity->setInvalidField('title', 'nikola');
- $this->assertSame($entity, $overwrite);
- $this->assertSame('nikola', $entity->getInvalidField('title'));
- }
- /**
- * Tests getInvalidFieldNull
- *
- * @return void
- */
- public function testGetInvalidFieldNull()
- {
- $entity = new Entity();
- $this->assertNull($entity->getInvalidField('foo'));
- }
- /**
- * Test the isEmpty() check
- *
- * @return void
- */
- public function testIsEmpty()
- {
- $entity = new Entity([
- 'array' => ['foo' => 'bar'],
- 'emptyArray' => [],
- 'object' => new \stdClass(),
- 'string' => 'string',
- 'stringZero' => '0',
- 'emptyString' => '',
- 'intZero' => 0,
- 'intNotZero' => 1,
- 'floatZero' => 0.0,
- 'floatNonZero' => 1.5,
- 'null' => null,
- ]);
- $this->assertFalse($entity->isEmpty('array'));
- $this->assertTrue($entity->isEmpty('emptyArray'));
- $this->assertFalse($entity->isEmpty('object'));
- $this->assertFalse($entity->isEmpty('string'));
- $this->assertFalse($entity->isEmpty('stringZero'));
- $this->assertTrue($entity->isEmpty('emptyString'));
- $this->assertFalse($entity->isEmpty('intZero'));
- $this->assertFalse($entity->isEmpty('intNotZero'));
- $this->assertFalse($entity->isEmpty('floatZero'));
- $this->assertFalse($entity->isEmpty('floatNonZero'));
- $this->assertTrue($entity->isEmpty('null'));
- }
- /**
- * Test hasValue()
- *
- * @return void
- */
- public function testHasValue()
- {
- $entity = new Entity([
- 'array' => ['foo' => 'bar'],
- 'emptyArray' => [],
- 'object' => new \stdClass(),
- 'string' => 'string',
- 'stringZero' => '0',
- 'emptyString' => '',
- 'intZero' => 0,
- 'intNotZero' => 1,
- 'floatZero' => 0.0,
- 'floatNonZero' => 1.5,
- 'null' => null,
- ]);
- $this->assertTrue($entity->hasValue('array'));
- $this->assertFalse($entity->hasValue('emptyArray'));
- $this->assertTrue($entity->hasValue('object'));
- $this->assertTrue($entity->hasValue('string'));
- $this->assertTrue($entity->hasValue('stringZero'));
- $this->assertFalse($entity->hasValue('emptyString'));
- $this->assertTrue($entity->hasValue('intZero'));
- $this->assertTrue($entity->hasValue('intNotZero'));
- $this->assertTrue($entity->hasValue('floatZero'));
- $this->assertTrue($entity->hasValue('floatNonZero'));
- $this->assertFalse($entity->hasValue('null'));
- }
- }
|