3

I am trying to test a data transfer object class which constructs with Symfony\Component\HttpFoundation\File\UploadedFile.php class in Laravel. I've had no luck testing this implementation in PHPSpec. Here is what I have so far:

Test

public function let($event_id)
{
    $prophet = new Prophet;
    $prophecy = $prophet->prophesize();
    $prophecy->willBeConstructedWith(array('path', 'name'));
    $prophecy->willExtend('Symfony\Component\HttpFoundation\File\UploadedFile');
    $attendee_list = $prophecy->reveal();

    $this->beConstructedWith($event_id, $attendee_list);
}

public function it_is_initializable()
{
    $this->shouldHaveType('Captix\Attendees\ImportAttendeesCommand');
}

Class

class ImportAttendeesCommand
{
/**
 * Event primary key
 * @var $event_id
 */
protected $event_id;

/**
 * CSV file
 * @var $attendee_list
 */
protected $attendee_list;

/**
 * Constructor for ImportAttendeesCommand
 */
public function __construct($event_id, UploadedFile $attendee_list)
{
    $this->event_id = $event_id;
    $this->attendee_list = $attendee_list;

}
}

Result

 2   -_-__,------,
 0   -_-__|   /\_/\
 0   -_-_^|__( o .o)
 1   -_-_  ""  ""

Captix/Attendees/ImportAttendeesCommand
  38  ! it is initializable
      exception [exc:Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException("The file "path" does not exist")] has been thrown.

       0 vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/File/File.php:41
         throw new Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException("The file "path" does not ...")
       1 vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/File/UploadedFile.php:100
         Symfony\Component\HttpFoundation\File\File->__construct("path", true)
       2 vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/ClassCreator.php(49) : eval()'d code:6
         Symfony\Component\HttpFoundation\File\UploadedFile->__construct("path", "name", null, null, null, null)
       3 [internal]
         Double\Symfony\Component\HttpFoundation\File\UploadedFile\P4->__construct("path", "name")
       4 vendor/phpspec/prophecy/src/Prophecy/Doubler/Doubler.php:109
         ReflectionClass->newInstanceArgs([array:2])
       5 vendor/phpspec/prophecy/src/Prophecy/Doubler/LazyDouble.php:119
         Prophecy\Doubler\Doubler->double([obj:ReflectionClass], [array:0], [array:2])
       6 vendor/phpspec/prophecy/src/Prophecy/Prophecy/ObjectProphecy.php:114
         Prophecy\Doubler\LazyDouble->getInstance()
       7 app/Spec/Captix/Attendees/ImportAttendeesCommandSpec.php:33
         Prophecy\Prophecy\ObjectProphecy->reveal()
       8 [internal]
         Spec\Captix\Attendees\ImportAttendeesCommandSpec->let([obj:PhpSpec\Wrapper\Collaborator])


3 examples (2 passed, 1 broken)
119ms

I've tried several ways to test this, using Mockery, Prohpecy, stdClass, nothing seems to be working. Any help on this would be much appreciated. I am welcome to any implementation of this test. Thanks to all in advance.

Anton
  • 172
  • 3
  • 10

2 Answers2

2

Create an interface:

interface UploadedFileInterface
{}

Create class that implements this interface and extends Symfony class.

class MyUploadedFile extends UploadedFile implements UploadedFileInterface
{}

Change constructor in your command:

public function __construct($event_id, UploadedFileInterface $attendee_list)
{
    $this->event_id = $event_id;
    $this->attendee_list = $attendee_list;
}

Now You can easily test your class in isolation:

public function let($event_id, UploadedFileInterface $attendeeList)
{
    $this->beConstructedWith($event_id, $attendeeList);
}

And if some day you will have to switch from Symfonys UploadedFile to another implementation, the only place that you will have to change will be MyUploadedFile class.

LNow
  • 68
  • 5
0

I had the same issue using prophecy to mock the UploadedFile so I submitted an issue for feedback (https://github.com/phpspec/prophecy/issues/184).

The solution is 'do not mock what you do not own'. I'm not sure yet how you would put that into practice, but thought you might be interested.

dblack
  • 79
  • 8