Files
abcParser/tests/unit/memory/TuneTest.php
2016-04-15 21:44:45 -04:00

311 lines
9.5 KiB
PHP

<?php
use App\Libraries\abcParse\Tune;
use App\Libraries\abcParse\Setting;
use App\Libraries\abcParse\Person;
use \Codeception\TestCase\Test as TestCase;
/**
* @group Lib
* @group Unit
*/
class TuneTest extends TestCase
{
private $_tune;
public function setUp()
{
$this->_tune = new Tune();
}
/**
* test the model contains the correct attributes
*/
public function test_model_definition()
{
//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 provider_set
*/
public function test_set($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 provider_set
*/
public function test_magic_setter($property, $value, $expected)
{
$this->_tune->$property = $value;
// $this->assertEquals($expected, $success);
if($expected)
{
$this->assertEquals($value, $this->_tune->get($property));
}
}
/**
* @dataProvider provider_set
*/
public function test_construct($property, $value, $expected)
{
$params = array($property => $value);
$this->_tune = new Tune($params);
if($expected)
{
$this->assertEquals($value, $this->_tune->get($property));
}
}
public function provider_set()
{
return array(
array('Y', 'anything', FALSE),
array('x', 1, FALSE),
array('X', 1, TRUE),
array('X', 'aplha', FALSE),
array('X', '2', TRUE),
array('T', 'string', FALSE),
array('T', 10, FALSE),
array('T', array('1','2'), TRUE),
array('A', 10, FALSE),
array('A', array('1','2'), FALSE),
array('A', array(new Person(),new Person()), TRUE),
array('C', 10, FALSE),
array('C', array('1','2'), FALSE),
array('C', array(new Person(),new Person()), TRUE),
array('G', 10, FALSE),
array('G', array('1','2'), TRUE),
array('H', 10, FALSE),
array('H', array('1','2'), TRUE),
array('O', 10, FALSE),
array('O', array('1','2'), TRUE),
array('R', 10, FALSE),
array('R', array('1','2'), TRUE),
array('collection', 10, FALSE),
array('collection', '10', FALSE),
array('collection', array(), FALSE),
array('collection', array('1','2'), FALSE),
array('collection', array(new Setting()), TRUE),
array('tuneHash', 10, FALSE),
array('tuneHash', '10', FALSE),
array('tuneHash', array(), FALSE),
array('tuneHash', array('1','2'), FALSE),
array('tuneID', 'abc', FALSE),
array('tuneID', '11', TRUE),
array('tuneID', 15, TRUE),
);
}
public function test_set_tuneID()
{
//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 provider_append
*/
public function test_append($property, $value, $expected)
{
$success = $this->_tune->append($property, $value);
$this->assertEquals($expected, $success);
if($success)
{
$this->assertContains($value, $this->_tune->get($property));
}
}
public function provider_append()
{
return array(
array('Y', 1, FALSE),
array('X', 1, FALSE),
array('T','title', TRUE),
array('T', array('of','titles'), FALSE),
array('A', 'author', TRUE),
array('C', 'composer', TRUE),
array('G', 'Group', TRUE),
array('H', 'history', TRUE),
array('O', 'Origin', TRUE),
array('R', 'Rhythm', TRUE),
array('collection', 'setting', FALSE),
array('collection', new Setting(), TRUE),
array('collection', array(new Setting()), FALSE),
array('tuneHash', 'tuneHash', FALSE),
array('tuneHash', new Setting(), FALSE),
array('tuneHash', array(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 provider_equals
*/
public function test_equals($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 provider_equals
*/
public function test_equals_fails($params)
{
$ob1 = new Tune();
$ob2 = new Tune();
foreach($params as $key => $val)
{
if($key === 'collection')
{
$ob1->set($key, $val);
}
else if(is_array($ob1->get($key)))
{
$ob1->append($key, $val);
}
else {
$ob1->set($key, $val);
}
}
$this->assertFalse(Tune::equals($ob1, $ob2));
}
public function provider_equals()
{
$setting1 = new Setting(array('settingID'=>5, 'tuneID'=>444));
$setting2 = new Setting(array('settingID'=>3, 'tuneID'=>2244));
return array(
array( array('X'=>'2') ),
array( array('T'=>'a title') ),
array( array('A'=>'an author') ),
array( array('C'=>'a composer') ),
array( array('G'=>'a group') ),
array( array('H'=>'some history') ),
array( array('O'=>'Plymouth') ),
array( array('R'=>'Jig') ),
array( array('collection' => array($setting1, $setting2) ))
);
}
public function test_isChanged()
{
$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 provider_get
*/
public function test_get($key, $expected)
{
$result = $this->_tune->set($key, $expected);
$result = $this->_tune->get($key);
$this->assertEquals($expected, $result);
}
public function provider_get()
{
return array(
array('', FALSE),
array('me', FALSE),
array('tuneID', 2),
array('X', '2'),
array('T', array('1','2')),
array('A', array(new Person(),new Person())),
array('C', array(new Person(),new Person())),
array('G', array('1','2')),
array('H', array('1','2')),
array('O', array('1','2')),
array('R', array('1','2')),
array('collection', array(new Setting())),
);
}
}