person = new Person(); } /** * test the model contains the correct attributes */ public function testModelDefinition() { $this->assertClassHasAttribute('person_id', Person::class); $this->assertClassHasAttribute('name', Person::class); $this->assertClassHasAttribute('email', Person::class); } public function testConstruct() { $params = ['person_id' => 5]; $this->person = new Person($params); $this->assertEquals(5, $this->person->get('person_id')); } /** * @dataProvider providerSet */ public function testMagicSet($property, $value, $expected) { $this->testSet($property, $value, $expected); } /** * 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->person->set($property, $value); $this->assertEquals($expected, $success); if ($success) { $this->assertEquals($value, $this->person->get($property)); } } public function providerSet() { return [ ['Y', 'anything', false], ['person_id', 'not', false], ['person_id', 11, true], ['person_id', '11', true], ['name', null, false], ['name', 1, false], ['name', 'anything', true], ['email', 'anything', false], ['email', 1, false], ['email', 'mail@example.com', true], ]; } /** * @dataProvider providerPersonGet */ public function testPersonGet($key, $expected) { $result = $this->person->set($key, $expected); $result = $this->person->get($key); $this->assertEquals($expected, $result); } public function providerPersonGet() { return [ ['', false], ['me', false], [['of', 'me'], false], [new Person(), false], ['name', 'Richard Morgan'], ['email', 'r_morgan@sympatico.ca'], ['person_id', 0], ]; } public function testIsChanged() { $person = new Person(['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 providerEquals */ public function testEquals($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); } 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 providerEquals */ public function testEqualsFails($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 providerEquals() { return [ // array( array('person_id'=>'2') ), [['name' => 'a title']], [['email' => 'mail@example.com']], ]; } }