Fix the failing unit tests

refactor the codestyle for the tests we wanted to keep
This commit is contained in:
2017-10-17 06:41:19 -04:00
parent 8ae78ef047
commit 0cc8361929
40 changed files with 1395 additions and 5439 deletions

View File

@@ -1,30 +1,30 @@
<?php
namespace Tests\Unit\Memory;
use App\Libraries\abcParse\Setting;
use App\Libraries\abcParse\Person;
use \Codeception\TestCase\Test as TestCase;
use Codeception\TestCase\Test as TestCase;
use XaiCorp\AbcParser\Models\Memory\Person;
use XaiCorp\AbcParser\Models\Memory\Setting;
/**
* @group Lib
*/
class SettingTest extends TestCase
{
private $_setting;
public function setUp()
{
$this->_setting = new Setting();
}
private $setting;
public function setUp()
{
$this->setting = new Setting();
}
/**
* test the model contains the correct attributes
*/
public function test_model_definition()
{
public function testModelDefinition()
{
$this->assertClassHasAttribute('settingID', Setting::class);
$this->assertClassHasAttribute('tuneID', Setting::class);
$this->assertClassHasAttribute('Z', Setting::class);
$this->assertClassHasAttribute('N', Setting::class);
$this->assertClassHasAttribute('D', Setting::class);
@@ -42,323 +42,313 @@ class SettingTest extends TestCase
// $this->assertClassHasAttribute('music_hash', Setting::class);
$this->assertClassHasAttribute('propertyNames', Setting::class);
}
}
/**
* test the set property method
*
*
* also tests the validate_line protected 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
*
* @dataProvider providerSet
*/
public function test_set($property, $value, $expected_success, $expected_value = FALSE)
public function testSet($property, $value, $expected_success, $expected_value = false)
{
$success = $this->_setting->set($property, $value);
$success = $this->setting->set($property, $value);
$this->assertEquals($expected_success, $success);
if($success)
{
if(! $expected_value ) { $expected_value = $value; }
$this->assertEquals($expected_value, $this->_setting->get($property));
if ($success) {
if (!$expected_value) {
$expected_value = $value;
}
$this->assertEquals($expected_value, $this->setting->get($property));
}
}
/**
* @dataProvider provider_set
*/
public function test_magic_set($property, $value, $expected_success, $expected_value = FALSE)
{
$this->_setting->$property = $value;
if($expected_success)
{
$this->assertEquals($value, $this->_setting->get($property));
}
}
public function provider_set()
{
return array(
array('Y', 'anything', FALSE),
array('settingID', 1, TRUE),
array('tuneID', array('alpha'), FALSE),
array('tuneID', 'alpha', FALSE),
array('tuneID', '1', TRUE),
array('tuneID', 1, TRUE),
array('f', 1, FALSE),
array('F', 1, FALSE),
array('F', '1',FALSE),
array('F', array('aplha'), TRUE),
array('P', array('string'), FALSE),
array('P', 10, FALSE),
array('P', 'string', TRUE),
array('Q', array('string'), FALSE),
array('Q', 10, FALSE),
array('Q', 'string', FALSE),
array('Q', '1/8=200', TRUE),
array('L', array('string'), FALSE),
array('L', 10, FALSE),
array('L', 'string', FALSE),
array('L', '1/8', TRUE),
array('M', array('string'), FALSE),
array('M', 10, FALSE),
array('M', 'string', FALSE),
array('M', '6/8', TRUE),
array('M', 'C', TRUE),
array('M', 'c', TRUE),
array('M', 'C|', TRUE),
array('K', array('string'), FALSE),
array('K', 10, FALSE),
array('K', 'C', TRUE),
array('K', 'Am', TRUE),
array('K', 'A minor', TRUE),
array('K', 'E dorian', TRUE),
array('music', 'GABc:|\\', TRUE),
array('Z', new Person(), FALSE),
array('Z', array(new Person(), 'not a person'), FALSE),
array('Z', array(new Person()), TRUE),
array('Z', 111, FALSE),
array('N', array('a string'), TRUE),
array('D', array('a string'), TRUE),
array('S', array('a string'), TRUE),
array('W', array('a string'), TRUE),
array('B', array('a string'), TRUE),
);
/**
* @dataProvider providerSet
*/
public function testMagicSet($property, $value, $expected_success, $expected_value = false)
{
$this->setting->$property = $value;
if ($expected_success) {
$this->assertEquals($value, $this->setting->get($property));
}
}
public function providerSet()
{
return [
['Y', 'anything', false],
['settingID', 1, true],
['tuneID', ['alpha'], false],
['tuneID', 'alpha', false],
['tuneID', '1', true],
['tuneID', 1, true],
['f', 1, false],
['F', 1, false],
['F', '1', false],
['F', ['aplha'], true],
['P', ['string'], false],
['P', 10, false],
['P', 'string', true],
['Q', ['string'], false],
['Q', 10, false],
['Q', 'string', false],
['Q', '1/8=200', true],
['L', ['string'], false],
['L', 10, false],
['L', 'string', false],
['L', '1/8', true],
['M', ['string'], false],
['M', 10, false],
['M', 'string', false],
['M', '6/8', true],
['M', 'C', true],
['M', 'c', true],
['M', 'C|', true],
['K', ['string'], false],
['K', 10, false],
['K', 'C', true],
['K', 'Am', true],
['K', 'A minor', true],
['K', 'E dorian', true],
['music', 'GABc:|\\', true],
['Z', new Person(), false],
['Z', [new Person(), 'not a person'], false],
['Z', [new Person()], true],
['Z', 111, false],
['N', ['a string'], true],
['D', ['a string'], true],
['S', ['a string'], true],
['W', ['a string'], true],
['B', ['a string'], true],
];
}
/**
* the settingID can only be set if it's value is currently 0
* this ensures that we can't change a setting's id once it's set.
* the default, unset value for settingID is 0
*/
public function test_set_settingID()
public function testSetSettingID()
{
$success = $this->_setting->set('settingID', 1);
$success = $this->setting->set('settingID', 1);
$this->assertTrue($success, 'first attempt should succeed');
$success = $this->_setting->set('settingID', 2);
$success = $this->setting->set('settingID', 2);
$this->assertFalse($success, 'second attempt should fail');
}
/**
* 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
*
* @dataProvider providerAppend
*/
public function test_append($property, $value, $expected)
public function testAppend($property, $value, $expected)
{
$success = $this->_setting->append($property, $value);
$success = $this->setting->append($property, $value);
$this->assertEquals($expected, $success);
if($success)
{
$this->assertContains($value, $this->_setting->get($property));
if ($success) {
$this->assertContains($value, $this->setting->get($property));
}
}
public function provider_append()
{
return array(
array('Y', 'anything', FALSE),
array('settingID', 1, FALSE),
array('tuneID', array('alpha'), FALSE),
array('tuneID', 'alpha', FALSE),
array('tuneID', '1', FALSE),
array('tuneID', 1, FALSE),
array('f', 1, FALSE),
array('F', 1, TRUE),
array('F', '1', TRUE),
array('F', array('aplha'), FALSE),
array('P', array('string'), FALSE),
array('P', 10, FALSE),
array('P', 'string', FALSE),
array('Q', array('string'), FALSE),
array('Q', 10, FALSE),
array('Q', 'string', FALSE),
array('Q', '1/8=200', FALSE),
array('L', array('string'), FALSE),
array('L', 10, FALSE),
array('L', 'string', FALSE),
array('L', '1/8', FALSE),
array('M', array('string'), FALSE),
array('M', 10, FALSE),
array('M', 'string', FALSE),
array('M', '6/8', FALSE),
array('M', 'C', FALSE),
array('M', 'c', FALSE),
array('M', 'C|', FALSE),
array('K', array('string'), FALSE),
array('K', 10, FALSE),
array('K', 'C', FALSE),
array('K', 'Am', FALSE),
array('K', 'A minor', FALSE),
array('K', 'E dorian', FALSE),
array('music', 'GABc:|\\', FALSE),
array('Z', new Person(), TRUE),
array('Z', array(new Person()), FALSE),
array('Z', 111, FALSE),
array('N', array('a string'), FALSE),
array('D', array('a string'), FALSE),
array('S', array('a string'), FALSE),
array('W', array('a string'), FALSE),
array('B', array('a string'), FALSE),
array('N', 'more notes', TRUE),
array('D', 'another record', TRUE),
array('S', 'another source', TRUE),
array('W', 'words', TRUE),
array('B', 'another book', TRUE),
);
}
/**
* @dataProvider provider_get
*/
public function test_get($key, $expected)
public function providerAppend()
{
$result = $this->_setting->set($key, $expected);
$result = $this->_setting->get($key);
return [
['Y', 'anything', false],
['settingID', 1, false],
['tuneID', ['alpha'], false],
['tuneID', 'alpha', false],
['tuneID', '1', false],
['tuneID', 1, false],
['f', 1, false],
['F', 1, true],
['F', '1', true],
['F', ['aplha'], false],
['P', ['string'], false],
['P', 10, false],
['P', 'string', false],
['Q', ['string'], false],
['Q', 10, false],
['Q', 'string', false],
['Q', '1/8=200', false],
['L', ['string'], false],
['L', 10, false],
['L', 'string', false],
['L', '1/8', false],
['M', ['string'], false],
['M', 10, false],
['M', 'string', false],
['M', '6/8', false],
['M', 'C', false],
['M', 'c', false],
['M', 'C|', false],
['K', ['string'], false],
['K', 10, false],
['K', 'C', false],
['K', 'Am', false],
['K', 'A minor', false],
['K', 'E dorian', false],
['music', 'GABc:|\\', false],
['Z', new Person(), true],
['Z', [new Person()], false],
['Z', 111, false],
['N', ['a string'], false],
['D', ['a string'], false],
['S', ['a string'], false],
['W', ['a string'], false],
['B', ['a string'], false],
['N', 'more notes', true],
['D', 'another record', true],
['S', 'another source', true],
['W', 'words', true],
['B', 'another book', true],
];
}
/**
* @dataProvider providerGet
*/
public function testGet($key, $expected)
{
$result = $this->setting->set($key, $expected);
$result = $this->setting->get($key);
$this->assertEquals($expected, $result);
}
public function provider_get()
public function providerGet()
{
return array(
array('', FALSE),
array('me', FALSE),
array('settingID', 1),
array('tuneID', 2),
array('Q', '1/8=100'),
array('L', '1/8'),
array('M', '6/8'),
array('K', 'Am'),
array('music', 'GABc:|\\'),
return [
['', false],
['me', false],
['settingID', 1],
['tuneID', 2],
['Q', '1/8=100'],
['L', '1/8'],
['M', '6/8'],
['K', 'Am'],
['music', 'GABc:|\\'],
['Z', [new Person()]],
['N', ['a string']],
['D', ['a string']],
['S', ['a string']],
['W', ['a string']],
['B', ['a string']],
];
}
array('Z', array(new Person())),
array('N', array('a string')),
array('D', array('a string')),
array('S', array('a string')),
array('W', array('a string')),
array('B', array('a string')),
);
}
/**
* test the static function equals
* returns true if the data properties of 2 Setting Objects
* are identical
*
*
* @param (array) $params list of properties to set on the objects
* @param (boolean) $expected result of running equals
*
* @dataProvider provider_equals
*
* @dataProvider providerEquals
*/
public function test_equals($params)
public function testEquals($params)
{
// $this->markTestIncomplete();
$ob1 = new Setting();
$ob2 = new Setting();
foreach($params as $key => $val)
{
if(is_array($ob1->get($key)))
{
foreach ($params as $key => $val) {
if (is_array($ob1->get($key))) {
$ob1->append($key, $val);
$ob2->append($key, $val);
}
else {
} else {
$ob1->set($key, $val);
$ob2->set($key, $val);
}
}
$this->assertTrue(Setting::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
*
* @dataProvider providerEquals
*/
public function test_equals_fails($params)
public function testEqualsFails($params)
{
$ob1 = new Setting();
$ob2 = new Setting();
foreach($params as $key => $val)
{
if(is_array($ob1->get($key)))
{
foreach ($params as $key => $val) {
if (is_array($ob1->get($key))) {
$ob1->append($key, $val);
// $ob2->append($key, $val);
}
else {
} else {
$ob1->set($key, $val);
$ob2->set($key, $val+1);
$ob2->set($key, $val + 1);
}
}
$this->assertFalse(Setting::equals($ob1, $ob2));
}
public function provider_equals()
public function providerEquals()
{
return array(
array( array('settingID'=>'2') ),
array( array('tuneID'=>'2') ),
array( array('Z'=> new Person()) ),
array( array('N'=>'an author') ),
array( array('D'=>'a composer') ),
array( array('S'=>'a group') ),
array( array('W'=>'some history') ),
array( array('B'=>'Plymouth') ),
array( array('F'=>'Jig') ),
array( array('P'=>'ABB') ),
array( array('Q'=>'1/8=100') ),
array( array('L'=>'1/4') ),
array( array('M'=>'2/4') ),
array( array('K'=>'G') ),
array( array('music'=>'a|cab|') ),
);
return [
[['settingID' => '2']],
[['tuneID' => '2']],
[['Z' => new Person()]],
[['N' => 'an author']],
[['D' => 'a composer']],
[['S' => 'a group']],
[['W' => 'some history']],
[['B' => 'Plymouth']],
[['F' => 'Jig']],
[['P' => 'ABB']],
[['Q' => '1/8=100']],
[['L' => '1/4']],
[['M' => '2/4']],
[['K' => 'G']],
[['music' => 'a|cab|']],
];
}
public function test_isChanged()
public function testIsChanged()
{
$setting = new Setting();
$this->assertInstanceOf(Setting::class, $setting);
$this->assertFalse($setting->is_changed(), 'after load should be no changes');
$setting->set('K','G');
$setting->set('K', 'G');
$this->assertTrue($setting->is_changed(), 'updating the name changes the data');
}
public function test_construct()
public function testConstruct()
{
$params = array(
$params = [
'settingID' => 3,
'Z' => array(new Person()),
'P' => 'ABC'
);
$this->_setting = new Setting ($params);
foreach($params as $key => $val)
{
$this->assertEquals($val, $this->_setting->get($key));
'Z' => [new Person()],
'P' => 'ABC',
];
$this->setting = new Setting($params);
foreach ($params as $key => $val) {
$this->assertEquals($val, $this->setting->get($key));
}
}
}