/** * Test hydrateObject (with being passed failure list). * * @test * @uses \Bairwell\Hydrator * @uses \Bairwell\Hydrator\CachedClass * @uses \Bairwell\Hydrator\CachedProperty * @uses \Bairwell\Hydrator\Failure * @uses \Bairwell\Hydrator\FailureList * @uses \Bairwell\Hydrator\Annotations\AsBase * @uses \Bairwell\Hydrator\Annotations\AsInt * @uses \Bairwell\Hydrator\Annotations\AsString * @uses \Bairwell\Hydrator\Annotations\From * @covers \Bairwell\Hydrator::hydrateObject */ public function testHydrateObject() { $sut = new Hydrator(); $sources = new Hydrator\Sources(); $sources->add('dummySource', function ($fieldName) { switch ($fieldName) { case 'numbered': return "1245"; case 'stringed': return 45; default: throw new \Exception('Unexpected source field - part 1 ' . $fieldName); } }); $sources->add('other', function ($fieldName) { switch ($fieldName) { case 'testOther': return 'correct'; default: throw new \Exception('Unexpected source field for other part 1: ' . $fieldName); } }); $conditions = new Hydrator\Conditionals(); $conditions->add('sunrisen', function () { return true; }); $conditions->add('moonrisen', function () { return false; }); $mockedObject = new Hydrator\MockedObject(); $failures = new Hydrator\FailureList(); $dummyFailure = new Hydrator\Failure(); $dummyFailure->setInputField('unittest'); $dummyFailure->setMessage('unittest made message'); $dummyFailure->setSource('jeff'); $failures->add($dummyFailure); $failures = $sut->hydrateObject($mockedObject, $sources, $conditions, $failures); // check the failures $this->assertInstanceOf('\\Bairwell\\Hydrator\\FailureList', $failures); $this->assertCount(1, $failures); $this->assertSame($dummyFailure, $failures[0]); // now check the contents $this->assertInstanceOf('\\Bairwell\\Hydrator\\MockedObject', $mockedObject); $this->assertInternalType('integer', $mockedObject->testIntCast); $this->assertEquals(1245, $mockedObject->testIntCast); $this->assertNull($mockedObject->testStringCast); $this->assertInternalType('string', $mockedObject->testOther); $this->assertEquals('correct', $mockedObject->testOther); // okay, now let's repeat that with the same object but different sources and conditions. $sources = new Hydrator\Sources(); $sources->add(['dummySource', 'other'], function ($fieldName) { switch ($fieldName) { case 'numbered': return null; case 'testOther': return null; case 'stringed': return 45; default: throw new \Exception('Unexpected source field for second part:' . $fieldName); } }); $conditions->unset('moonrisen'); $conditions->add('moonrisen', function () { return true; }); $failures = $sut->hydrateObject($mockedObject, $sources, $conditions, $failures); // check the failures $this->assertInstanceOf('\\Bairwell\\Hydrator\\FailureList', $failures); $this->assertCount(1, $failures); $this->assertSame($dummyFailure, $failures[0]); // now check the contents $this->assertInstanceOf('\\Bairwell\\Hydrator\\MockedObject', $mockedObject); $this->assertInternalType('integer', $mockedObject->testIntCast); $this->assertEquals(1245, $mockedObject->testIntCast); $this->assertInternalType('string', $mockedObject->testStringCast); $this->assertEquals('45', $mockedObject->testStringCast); $this->assertInternalType('string', $mockedObject->testOther); $this->assertEquals('correct', $mockedObject->testOther); }