From a4f8a745caf1ca35f5fa9b67ea32c24a2860bb90 Mon Sep 17 00:00:00 2001 From: Christopher Martin Date: Tue, 18 Dec 2012 17:51:31 -0500 Subject: [PATCH] FileInputTest mock fix; File PRG fix --- test/FileInputTest.php | 58 ++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/test/FileInputTest.php b/test/FileInputTest.php index 32d1b04b..0e2f4646 100644 --- a/test/FileInputTest.php +++ b/test/FileInputTest.php @@ -100,12 +100,19 @@ public function testRetrievingValueFiltersTheValueOnlyAfterValidating() $filterMock = $this->getMockBuilder('Zend\Filter\File\Rename') ->disableOriginalConstructor() ->getMock(); - $filterMock->expects($this->exactly(1)) + $filterMock->expects($this->any()) ->method('filter') - ->with($this->anything()) ->will($this->returnValue($newValue)); - $this->input->getFilterChain()->attach($filterMock); + // Why not attach mocked filter directly? + // No worky without wrapping in a callback. + // Missing something in mock setup? + $this->input->getFilterChain()->attach( + function ($value) use ($filterMock) { + return $filterMock->filter($value); + } + ); + $this->assertEquals($value, $this->input->getValue()); $this->assertTrue($this->input->isValid()); $this->assertEquals($newValue, $this->input->getValue()); @@ -124,15 +131,25 @@ public function testCanFilterArrayOfMultiFileData() $filterMock = $this->getMockBuilder('Zend\Filter\File\Rename') ->disableOriginalConstructor() ->getMock(); - $filterMock->expects($this->exactly(3)) + $filterMock->expects($this->any()) ->method('filter') - ->with($this->anything()) ->will($this->returnValue($newValue)); - $this->input->getFilterChain()->attach($filterMock); + // Why not attach mocked filter directly? + // No worky without wrapping in a callback. + // Missing something in mock setup? + $this->input->getFilterChain()->attach( + function ($value) use ($filterMock) { + return $filterMock->filter($value); + } + ); + $this->assertEquals($values, $this->input->getValue()); $this->assertTrue($this->input->isValid()); - $this->assertEquals(array($newValue, $newValue, $newValue), $this->input->getValue()); + $this->assertEquals( + array($newValue, $newValue, $newValue), + $this->input->getValue() + ); } public function testCanRetrieveRawValue() @@ -162,32 +179,45 @@ public function testIsValidReturnsTrueIfValidationChainSucceeds() public function testValidationOperatesBeforeFiltering() { - $this->input->setValue(array( + $badValue = array( 'tmp_name' => ' ' . __FILE__ . ' ', 'name' => 'foo', 'size' => 1, 'error' => 0, - )); + ); + $this->input->setValue($badValue); - $newValue = array('tmp_name' => 'new'); + $filteredValue = array('tmp_name' => 'new'); $filterMock = $this->getMockBuilder('Zend\Filter\File\Rename') ->disableOriginalConstructor() ->getMock(); $filterMock->expects($this->any()) ->method('filter') - ->will($this->returnValue($newValue)); + ->will($this->returnValue($filteredValue)); + + // Why not attach mocked filter directly? + // No worky without wrapping in a callback. + // Missing something in mock setup? + $this->input->getFilterChain()->attach( + function ($value) use ($filterMock) { + return $filterMock->filter($value); + } + ); - $this->input->getFilterChain()->attach($filterMock); $validator = new Validator\File\Exists(); $this->input->getValidatorChain()->attach($validator); $this->assertFalse($this->input->isValid()); - $this->input->setValue(array( + $this->assertEquals($badValue, $this->input->getValue()); + + $goodValue = array( 'tmp_name' => __FILE__, 'name' => 'foo', 'size' => 1, 'error' => 0, - )); + ); + $this->input->setValue($goodValue); $this->assertTrue($this->input->isValid()); + $this->assertEquals($filteredValue, $this->input->getValue()); } public function testGetMessagesReturnsValidationMessages()