tune = new Tune(); } /** * test the model contains the correct attributes */ public function testModelDefinition() { //Private attributes, probably shouldn't be tested $this->assertClassHasAttribute('tuneID', Tune::class); $this->assertClassHasAttribute('A', Tune::class); $this->assertClassHasAttribute('C', Tune::class); $this->assertClassHasAttribute('T', Tune::class); $this->assertClassHasAttribute('G', Tune::class); $this->assertClassHasAttribute('O', Tune::class); $this->assertClassHasAttribute('R', Tune::class); $this->assertClassHasAttribute('H', Tune::class); $this->assertClassHasAttribute('X', Tune::class); $this->assertClassHasAttribute('propertyNames', Tune::class); } /** * test the set property method * * @param (string) $property, the name of the property to set * @param (mixed) value to set * @param (Boolean) expected value for return value of set method * * @dataProvider providerSet */ public function testSet($property, $value, $expected) { $success = $this->tune->set($property, $value); $this->assertEquals($expected, $success); if ($success) { $this->assertEquals($value, $this->tune->get($property)); } } /** * test the set property method * * @param (string) $property, the name of the property to set * @param (mixed) value to set * @param (Boolean) expected value for return value of set method * * @dataProvider providerSet */ public function testMagicSetter($property, $value, $expected) { $this->tune->$property = $value; // $this->assertEquals($expected, $success); if ($expected) { $this->assertEquals($value, $this->tune->get($property)); } } /** * @dataProvider providerSet */ public function testConstruct($property, $value, $expected) { $params = [$property => $value]; $this->tune = new Tune($params); if ($expected) { $this->assertEquals($value, $this->tune->get($property)); } } public function providerSet() { return [ ['Y', 'anything', false], ['x', 1, false], ['X', 1, true], ['X', 'aplha', false], ['X', '2', true], ['T', 'string', false], ['T', 10, false], ['T', ['1', '2'], true], ['A', 10, false], ['A', ['1', '2'], false], ['A', [new Person(), new Person()], true], ['C', 10, false], ['C', ['1', '2'], false], ['C', [new Person(), new Person()], true], ['G', 10, false], ['G', ['1', '2'], true], ['H', 10, false], ['H', ['1', '2'], true], ['O', 10, false], ['O', ['1', '2'], true], ['R', 10, false], ['R', ['1', '2'], true], ['collection', 10, false], ['collection', '10', false], ['collection', [], false], ['collection', ['1', '2'], false], ['collection', [new Setting()], true], ['tuneHash', 10, false], ['tuneHash', '10', false], ['tuneHash', [], false], ['tuneHash', ['1', '2'], false], ['tuneID', 'abc', false], ['tuneID', '11', true], ['tuneID', 15, true], ]; } public function testSetTuneID() { //we should start with tuneID = 0 $this->assertEquals(0, $this->tune->get('tuneID'), 'should start at 0'); //because tuneID == 0 we can change value $this->tune->set('tuneID', 4); $this->assertEquals(4, $this->tune->get('tuneID'), 'should have changed to 4'); //tuneID is not 0 so we cannot change value $this->tune->set('tuneID', 11); $this->assertEquals(4, $this->tune->get('tuneID'), 'should not have changed from 4'); } /** * test the append value method * * @param (string) $property, the name of the property to set * @param (mixed) value to append * @param (Boolean) expected value for return value of append method * * @dataProvider providerAppend */ public function testAppend($property, $value, $expected) { $success = $this->tune->append($property, $value); $this->assertEquals($expected, $success); if ($success) { $this->assertContains($value, $this->tune->get($property)); } } public function providerAppend() { return [ ['Y', 1, false], ['X', 1, false], ['T', 'title', true], ['T', ['of', 'titles'], false], ['A', 'author', true], ['C', 'composer', true], ['G', 'Group', true], ['H', 'history', true], ['O', 'Origin', true], ['R', 'Rhythm', true], ['collection', 'setting', false], ['collection', new Setting(), true], ['collection', [new Setting()], false], ['tuneHash', 'tuneHash', false], ['tuneHash', new Setting(), false], ['tuneHash', [new Setting()], false], ]; } /** * test the static function equals * returns true if the data properties of 2 Abc_Tunes * are identical * * @param (array) $params list of properties to set on the objects * @param (boolean) $expected result of running equals * * @dataProvider providerEquals */ public function testEquals($params) { // $this->markTestIncomplete(); $ob1 = new Tune(); $ob2 = new Tune(); foreach ($params as $key => $val) { if (is_array($ob1->get($key))) { $ob1->append($key, $val); $ob2->append($key, $val); } else { $ob1->set($key, $val); $ob2->set($key, $val); } } $this->assertTrue(Tune::equals($ob1, $ob2)); } /** * test the static function equals * returns true if the data properties of 2 Abc_Tunes * are identical * * @param (array) $params list of properties to set on the objects * @param (boolean) $expected result of running equals * * @dataProvider providerEquals */ public function testEqualsFails($params) { $ob1 = new Tune(); $ob2 = new Tune(); foreach ($params as $key => $val) { if ($key === 'collection') { $ob1->set($key, $val); } elseif (is_array($ob1->get($key))) { $ob1->append($key, $val); } else { $ob1->set($key, $val); } } $this->assertFalse(Tune::equals($ob1, $ob2)); } public function providerEquals() { $setting1 = new Setting(['settingID' => 5, 'tuneID' => 444]); $setting2 = new Setting(['settingID' => 3, 'tuneID' => 2244]); return [ [['X' => '2']], [['T' => 'a title']], [['A' => 'an author']], [['C' => 'a composer']], [['G' => 'a group']], [['H' => 'some history']], [['O' => 'Plymouth']], [['R' => 'Jig']], [['collection' => [$setting1, $setting2]]], ]; } public function testIsChanged() { $tune = new Tune(); $this->assertInstanceOf(Tune::class, $tune); $this->assertFalse($tune->is_changed(), 'after load should be no changes'); $tune->set('X', '5'); $this->assertTrue($tune->is_changed(), 'updating the name changes the data'); } /** * @dataProvider providerGet */ public function testGet($key, $expected) { $result = $this->tune->set($key, $expected); $result = $this->tune->get($key); $this->assertEquals($expected, $result); } public function providerGet() { return [ ['', false], ['me', false], ['tuneID', 2], ['X', '2'], ['T', ['1', '2']], ['A', [new Person(), new Person()]], ['C', [new Person(), new Person()]], ['G', ['1', '2']], ['H', ['1', '2']], ['O', ['1', '2']], ['R', ['1', '2']], ['collection', [new Setting()]], ]; } }