UploadedFileListenerTest.php 5.14 KiB
<?php declare(strict_types=1);
/*
 * Copyright (C) 2015-2017 IRSTEA
 * All rights reserved.
 */
namespace Irstea\FileUploadBundle\Listener;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\UnitOfWork;
use Gaufrette\Exception\FileNotFound;
use Gaufrette\Filesystem;
use Irstea\FileUploadBundle\Entity\UploadedFile;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;
/**
 * Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:56:00.
class UploadedFileListenerTest extends PHPUnit_Framework_TestCase
    /**
     * @var UploadedFileListener
    protected $listener;
    /**
     * @var PHPUnit_Framework_MockObject_MockObject
    protected $filesystem;
    /**
     * @var PHPUnit_Framework_MockObject_MockObject
    protected $file;
    /**
     * @var PHPUnit_Framework_MockObject_MockObject
    protected $om;
    /**
     * @var PHPUnit_Framework_MockObject_MockObject
    protected $unitOfWork;
    /**
     * @var PHPUnit_Framework_MockObject_MockObject
    protected $event;
    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
    protected function setUp()
        $this->filesystem = $this->getMockBuilder(Filesystem::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->listener = new UploadedFileListener($this->filesystem);
        $this->file = $this->getMockBuilder(UploadedFile::class)
            ->setMethods(['setFilesystem', 'getPath'])
            ->getMockForAbstractClass();
        $this->unitOfWork = $this->getMockBuilder(UnitOfWork::class)
            ->disableOriginalConstructor()
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
->setMethods(['getEntityChangeSet']) ->getMock(); $this->om = $this->getMockBuilder(EntityManager::class) ->setMethods(['getUnitOfWork']) ->disableOriginalConstructor() ->getMock(); $this->om->expects(static::any())->method('getUnitOfWork')->willReturn($this->unitOfWork); $this->event = new LifecycleEventArgs($this->file, $this->om); } /** * @covers \Irstea\FileUploadBundle\Listener\UploadedFileListener::postLoad * * @todo Implement testPostLoad(). */ public function testPostLoad() { $this->file->expects(static::once())->method('setFilesystem')->with(static::identicalTo($this->filesystem)); $this->listener->postLoad($this->file, $this->event); } /** * @covers \Irstea\FileUploadBundle\Listener\UploadedFileListener::postUpdate */ public function testPostUpdateNoPathChange() { $this->unitOfWork->expects(static::once())->method('getEntityChangeSet')->with($this->file)->willReturn([]); $this->filesystem->expects(static::never())->method('rename'); $this->listener->postUpdate($this->file, $this->event); } /** * @covers \Irstea\FileUploadBundle\Listener\UploadedFileListener::postUpdate */ public function testPostUpdatePathChanged() { $this->unitOfWork->expects(static::once())->method('getEntityChangeSet')->with($this->file)->willReturn( ['path' => ['before', 'after']] ); $this->filesystem->expects(static::once())->method('rename')->with('before', 'after'); $this->listener->postUpdate($this->file, $this->event); } /** * @covers \Irstea\FileUploadBundle\Listener\UploadedFileListener::postRemove */ public function testRemove() { $this->file->expects(static::any())->method('getId')->willReturn('bla'); $this->file->expects(static::once())->method('getPath')->willReturn('foobar'); $this->filesystem->expects(static::once())->method('delete')->with('foobar'); $this->listener->preRemove($this->file, $this->event); $this->listener->postRemove($this->file, $this->event); } /** * @covers \Irstea\FileUploadBundle\Listener\UploadedFileListener::postRemove */ public function testRemoveIgnoreFileNotFound() { $this->file->expects(static::any())->method('getId')->willReturn('bla');
141142143144145146147148149150151152153154155156157158159160161162163164
$this->file->expects(static::once())->method('getPath')->willReturn('foobar'); $this->filesystem->expects(static::once())->method('delete')->with('foobar')->willThrowException(new FileNotFound('foobar')); $this->listener->preRemove($this->file, $this->event); $this->listener->postRemove($this->file, $this->event); } /** * @covers \Irstea\FileUploadBundle\Listener\UploadedFileListener::postRemove * @expectedException \InvalidArgumentException */ public function testRemovePropagateOtherExceptions() { $this->file->expects(static::any())->method('getId')->willReturn('bla'); $this->file->expects(static::once())->method('getPath')->willReturn('foobar'); $this->filesystem->expects(static::once())->method('delete')->with('foobar')->willThrowException(new \InvalidArgumentException()); $this->listener->preRemove($this->file, $this->event); $this->listener->postRemove($this->file, $this->event); } }