355 lines
10 KiB
PHP
355 lines
10 KiB
PHP
<?php
|
|
namespace Tests\Unit\Memory;
|
|
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* test the model contains the correct attributes
|
|
*/
|
|
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);
|
|
$this->assertClassHasAttribute('S', Setting::class);
|
|
$this->assertClassHasAttribute('W', Setting::class);
|
|
$this->assertClassHasAttribute('B', Setting::class);
|
|
|
|
$this->assertClassHasAttribute('F', Setting::class);
|
|
$this->assertClassHasAttribute('P', Setting::class);
|
|
$this->assertClassHasAttribute('Q', Setting::class);
|
|
$this->assertClassHasAttribute('L', Setting::class);
|
|
$this->assertClassHasAttribute('M', Setting::class);
|
|
$this->assertClassHasAttribute('K', Setting::class);
|
|
$this->assertClassHasAttribute('music', Setting::class);
|
|
// $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 providerSet
|
|
*/
|
|
public function testSet($property, $value, $expected_success, $expected_value = false)
|
|
{
|
|
$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));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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 testSetSettingID()
|
|
{
|
|
$success = $this->setting->set('settingID', 1);
|
|
$this->assertTrue($success, 'first attempt should succeed');
|
|
|
|
$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 providerAppend
|
|
*/
|
|
public function testAppend($property, $value, $expected)
|
|
{
|
|
$success = $this->setting->append($property, $value);
|
|
$this->assertEquals($expected, $success);
|
|
if ($success) {
|
|
$this->assertContains($value, $this->setting->get($property));
|
|
}
|
|
}
|
|
|
|
public function providerAppend()
|
|
{
|
|
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 providerGet()
|
|
{
|
|
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']],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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 providerEquals
|
|
*/
|
|
public function testEquals($params)
|
|
{
|
|
$ob1 = new Setting();
|
|
$ob2 = new Setting();
|
|
|
|
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(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 providerEquals
|
|
*/
|
|
public function testEqualsFails($params)
|
|
{
|
|
$ob1 = new Setting();
|
|
$ob2 = new Setting();
|
|
|
|
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 + 1);
|
|
}
|
|
}
|
|
$this->assertFalse(Setting::equals($ob1, $ob2));
|
|
}
|
|
|
|
public function providerEquals()
|
|
{
|
|
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 testIsChanged()
|
|
{
|
|
$setting = new Setting();
|
|
|
|
$this->assertInstanceOf(Setting::class, $setting);
|
|
|
|
$this->assertFalse($setting->is_changed(), 'after load should be no changes');
|
|
|
|
$setting->set('K', 'G');
|
|
$this->assertTrue($setting->is_changed(), 'updating the name changes the data');
|
|
}
|
|
|
|
|
|
public function testConstruct()
|
|
{
|
|
$params = [
|
|
'settingID' => 3,
|
|
'Z' => [new Person()],
|
|
'P' => 'ABC',
|
|
];
|
|
|
|
$this->setting = new Setting($params);
|
|
|
|
foreach ($params as $key => $val) {
|
|
$this->assertEquals($val, $this->setting->get($key));
|
|
}
|
|
}
|
|
}
|