【问题标题】:Unit test UploadedFile.php in PHPSpecPHPSpec 中的单元测试 UploadedFile.php
【发布时间】:2015-04-15 02:44:29
【问题描述】:

我正在尝试测试在 Laravel 中使用 Symfony\Component\HttpFoundation\File\UploadedFile.php 类构建的数据传输对象类。我没有在PHPSpec 中测试这个实现。这是我目前所拥有的:

测试

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 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;

}
}

结果

 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

我尝试了几种方法来测试这一点,使用 Mockery、Prohpecy、stdClass,似乎没有任何效果。对此的任何帮助将不胜感激。我欢迎这个测试的任何实施。在此先感谢大家。

【问题讨论】:

    标签: php unit-testing laravel laravel-4 phpspec


    【解决方案1】:

    创建接口:

    interface UploadedFileInterface
    {}
    

    创建实现此接口并扩展 Symfony 类的类。

    class MyUploadedFile extends UploadedFile implements UploadedFileInterface
    {}
    

    更改命令中的构造函数:

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

    现在您可以轻松地单独测试您的课程:

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

    如果有一天你不得不从 Symfonys UploadedFile 切换到另一个实现,那么你唯一需要改变的地方就是 MyUploadedFile 类。

    【讨论】:

      【解决方案2】:

      我在使用 Prophecy 模拟 UploadedFile 时遇到了同样的问题,因此我提交了一个问题以供反馈 (https://github.com/phpspec/prophecy/issues/184)。

      解决方案是“不要嘲笑你不拥有的东西”。我还不确定你会如何将它付诸实践,但我想你可能会感兴趣。

      【讨论】:

        猜你喜欢
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多