Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Say I have the following class:

class Document
{
  private file;

  public function setFile(UploadedFile $file)
  {
    $this->file = $file;
  }

  public function getExt()
  {
    return $this->file->guessExtension();
  }
}

I'd like to test the getExt() method. I tried to set up the test as follows:

$file = $this->getMock('UploadedFile');

$file->expects($this->at(0))
  ->method('guessExtension')
  ->will($this->returnValue('png'));

$doc = new Document();
$doc->setFile($file);
...

However, it is giving me error saying that setFile() is expecting an instance of UploadedFile and the mock object is found instead. Can anyone shed some light on how to test this kind of scenario? I am a beginner when it comes to testing with mocks and stubs.

Thanks!

share|improve this question
    
That should not have been an issue, the generated mock class extends the original class. Are you using namespaces by chance? – fschmengler May 19 '13 at 12:53
up vote -1 down vote accepted

Thanks to fab's comment. I made the following change and got it working,

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
        ->disableOriginalConstructor()
        ->getMock();
share|improve this answer

As of PHP 5.5.16 Chieh solution can't be used anymore. See reference.

A possibile solution is creating a temporary dummy file in /tmp with setUp()/tearDown() and pass it tot he constructor. Then we mocked the required methods to return what needed.

Note: UploadedFile will require you to pass two parameters

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.