190 lines
4.9 KiB
PHP
190 lines
4.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group Lib
|
|
* @group Unit
|
|
*/
|
|
|
|
use App\Libraries\abcParse\Person;
|
|
use \Codeception\TestCase\Test;
|
|
|
|
class PersonTest extends Test
|
|
{
|
|
private $_person;
|
|
|
|
public function setUp()
|
|
{
|
|
$this->_person = new Person();
|
|
}
|
|
|
|
/**
|
|
* test the model contains the correct attributes
|
|
*/
|
|
public function test_model_definition()
|
|
{
|
|
$this->assertClassHasAttribute('person_id', Person::class);
|
|
$this->assertClassHasAttribute('name', Person::class);
|
|
$this->assertClassHasAttribute('email', Person::class);
|
|
}
|
|
|
|
public function test_construct()
|
|
{
|
|
$params = array('person_id' => 5);
|
|
$this->_person = new Person($params);
|
|
|
|
$this->assertEquals(5, $this->_person->get('person_id'));
|
|
}
|
|
|
|
|
|
/**
|
|
* 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->_person->set($property, $value);
|
|
$this->assertEquals($expected, $success);
|
|
if($success)
|
|
{
|
|
$this->assertEquals($value, $this->_person->get($property));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provider_set
|
|
*/
|
|
public function test_magic_set($property, $value, $expected)
|
|
{
|
|
$this->test_set($property, $value, $expected);
|
|
}
|
|
|
|
public function provider_set()
|
|
{
|
|
return array(
|
|
array('Y', 'anything', FALSE),
|
|
array('person_id', 'not', FALSE),
|
|
array('person_id', 11, TRUE),
|
|
array('person_id', '11', TRUE),
|
|
array('name', null, FALSE),
|
|
array('name', 1, FALSE),
|
|
array('name', 'anything', TRUE),
|
|
array('email', 'anything', FALSE),
|
|
array('email', 1, FALSE),
|
|
array('email', 'mail@example.com', TRUE),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provider_person_get
|
|
*/
|
|
public function test_person_get($key, $expected)
|
|
{
|
|
$result = $this->_person->set($key, $expected);
|
|
$result = $this->_person->get($key);
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function provider_person_get()
|
|
{
|
|
return array(
|
|
array('', FALSE),
|
|
array('me', FALSE),
|
|
array(array('of','me'), FALSE),
|
|
array(new Person(), FALSE),
|
|
array('name', 'Richard Morgan'),
|
|
array('email', 'r_morgan@sympatico.ca'),
|
|
array('person_id', 0)
|
|
);
|
|
}
|
|
|
|
public function test_is_changed()
|
|
{
|
|
$person = new Person(array('name'=>'Richard Morgan'));
|
|
|
|
$this->assertInstanceOf(Person::class, $person);
|
|
|
|
$this->assertFalse($person->is_changed(), 'after load should be no changes');
|
|
|
|
$person->set('name','Richard Armitage');
|
|
$this->assertTrue($person->is_changed(), 'updating the name changes the data');
|
|
}
|
|
|
|
/**
|
|
* test the static function equals
|
|
* returns true if the data properties of 2 Person objects
|
|
* 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 Person();
|
|
$ob2 = new Person();
|
|
|
|
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(Person::equals($ob1, $ob2));
|
|
}
|
|
/**
|
|
* test the static function equals
|
|
* returns true if the data properties of 2 Person objects
|
|
* 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 Person();
|
|
$ob2 = new Person();
|
|
|
|
foreach($params as $key => $val)
|
|
{
|
|
if(is_array($ob1->get($key)))
|
|
{
|
|
$ob1->append($key, $val);
|
|
$ob2->append($key, $val." hello");
|
|
}
|
|
else {
|
|
$ob1->set($key, $val);
|
|
$ob2->set($key, $val+1);
|
|
}
|
|
}
|
|
// print_r($ob1); print_r($ob2); die;
|
|
$this->assertFalse(Person::equals($ob1, $ob2));
|
|
}
|
|
public function provider_equals()
|
|
{
|
|
return array(
|
|
// array( array('person_id'=>'2') ),
|
|
array( array('name'=>'a title') ),
|
|
array( array('email'=>'mail@example.com') ),
|
|
);
|
|
}
|
|
|
|
|
|
}
|