|
|
@@ -9,13 +9,18 @@ use App\Entity\Person\Person;
|
|
|
use App\Enum\Core\FileStatusEnum;
|
|
|
use App\Enum\Core\FileTypeEnum;
|
|
|
use App\Repository\Access\AccessRepository;
|
|
|
+use App\Service\File\Factory\ImageFactory;
|
|
|
use App\Service\File\Storage\LocalStorage;
|
|
|
-use App\Service\File\Utils\FileUtils;
|
|
|
+use App\Service\Utils\FileUtils;
|
|
|
+use App\Service\Utils\UrlBuilder;
|
|
|
use DateTime;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
use Gaufrette\Filesystem;
|
|
|
use JetBrains\PhpStorm\Pure;
|
|
|
use Knp\Bundle\GaufretteBundle\FilesystemMap;
|
|
|
+use Liip\ImagineBundle\Imagine\Cache\CacheManager;
|
|
|
+use Liip\ImagineBundle\Imagine\Data\DataManager;
|
|
|
+use PHPUnit\Framework\MockObject\MockObject;
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
use RuntimeException;
|
|
|
|
|
|
@@ -36,28 +41,50 @@ class LocalStorageTest extends TestCase
|
|
|
private FilesystemMap $filesystemMap;
|
|
|
private EntityManagerInterface $entityManager;
|
|
|
private AccessRepository $accessRepository;
|
|
|
+ private DataManager $dataManager;
|
|
|
+ private CacheManager $cacheManager;
|
|
|
+ private ImageFactory $imageFactory;
|
|
|
private Filesystem $filesystem;
|
|
|
- private FileUtils $imageUtils;
|
|
|
+ private FileUtils $fileUtils;
|
|
|
+ private UrlBuilder $urlBuilder;
|
|
|
|
|
|
public function setUp(): void
|
|
|
{
|
|
|
$this->filesystemMap = $this->getMockBuilder(FilesystemMap::class)->disableOriginalConstructor()->getMock();
|
|
|
$this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
$this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
|
|
|
- $this->imageUtils = $this->getMockBuilder(FileUtils::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->dataManager = $this->getMockBuilder(DataManager::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->cacheManager = $this->getMockBuilder(CacheManager::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->imageFactory = $this->getMockBuilder(ImageFactory::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->fileUtils = $this->getMockBuilder(FileUtils::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->urlBuilder = $this->getMockBuilder(UrlBuilder::class)->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
$this->filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
|
|
|
$this->filesystemMap->method('get')->with(TestableLocalStorage::FS_KEY)->willReturn($this->filesystem);
|
|
|
}
|
|
|
|
|
|
+ public function getMockForMethod(string $methodName): TestableLocalStorage | MockObject
|
|
|
+ {
|
|
|
+ return $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
+ ->setConstructorArgs([
|
|
|
+ $this->filesystemMap,
|
|
|
+ $this->entityManager,
|
|
|
+ $this->accessRepository,
|
|
|
+ $this->dataManager,
|
|
|
+ $this->cacheManager,
|
|
|
+ $this->imageFactory,
|
|
|
+ $this->fileUtils,
|
|
|
+ $this->urlBuilder
|
|
|
+ ])
|
|
|
+ ->setMethodsExcept([$methodName])
|
|
|
+ ->getMock();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @see LocalStorage::exists()
|
|
|
*/
|
|
|
public function testExists(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['exists'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('exists');
|
|
|
|
|
|
$file = $this->getMockBuilder(File::class)->getMock();
|
|
|
$file->method('getSlug')->willReturn('foo');
|
|
|
@@ -71,10 +98,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::exists()
|
|
|
*/
|
|
|
public function testExistsInexistant(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['exists'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('exists');
|
|
|
|
|
|
$file = $this->getMockBuilder(File::class)->getMock();
|
|
|
$file->method('getSlug')->willReturn('foo');
|
|
|
@@ -90,10 +114,7 @@ class LocalStorageTest extends TestCase
|
|
|
*/
|
|
|
public function testListByOwner(): void
|
|
|
{
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['listByOwner'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('listByOwner');
|
|
|
|
|
|
$owner = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
|
|
|
@@ -112,39 +133,10 @@ class LocalStorageTest extends TestCase
|
|
|
*/
|
|
|
public function testReadPdf(): void
|
|
|
{
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['read'])
|
|
|
- ->getMock();
|
|
|
-
|
|
|
- $file = $this->getMockBuilder(File::class)->getMock();
|
|
|
- $file->method('getSlug')->willReturn('foo');
|
|
|
- $file->method('getMimeType')->willReturn('application/pdf');
|
|
|
+ $fileStorage = $this->getMockForMethod('read');
|
|
|
|
|
|
- $this->filesystem->method('read')->with('foo')->willReturn('12345679');
|
|
|
-
|
|
|
- $this->assertEquals(
|
|
|
- '12345679',
|
|
|
- $fileStorage->read($file)
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @see LocalStorage::read()
|
|
|
- */
|
|
|
- public function testReadImage(): void
|
|
|
- {
|
|
|
$file = $this->getMockBuilder(File::class)->getMock();
|
|
|
$file->method('getSlug')->willReturn('foo');
|
|
|
- $file->method('getMimeType')->willReturn('image/jpeg');
|
|
|
-
|
|
|
- $imageUtils = $this->imageUtils;
|
|
|
- $imageUtils->method('formatImage')->with($file)->willReturn('12345679');
|
|
|
-
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $imageUtils])
|
|
|
- ->setMethodsExcept(['read'])
|
|
|
- ->getMock();
|
|
|
|
|
|
$this->filesystem->method('read')->with('foo')->willReturn('12345679');
|
|
|
|
|
|
@@ -159,10 +151,7 @@ class LocalStorageTest extends TestCase
|
|
|
*/
|
|
|
public function testPrepareFile(): void
|
|
|
{
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['prepareFile'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('prepareFile');
|
|
|
|
|
|
$owner = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
$author = $this->getMockBuilder(Access::class)->getMock();
|
|
|
@@ -200,10 +189,7 @@ class LocalStorageTest extends TestCase
|
|
|
*/
|
|
|
public function testPrepareFileDefaultValues(): void
|
|
|
{
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['prepareFile'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('prepareFile');
|
|
|
|
|
|
$owner = $this->getMockBuilder(Person::class)->getMock();
|
|
|
$author = $this->getMockBuilder(Access::class)->getMock();
|
|
|
@@ -213,6 +199,8 @@ class LocalStorageTest extends TestCase
|
|
|
$this->entityManager->expects(self::once())->method('persist');
|
|
|
$this->entityManager->expects(self::once())->method('flush');
|
|
|
|
|
|
+ $this->fileUtils->method('guessMimeTypeFromFilename')->with('file.txt')->willReturn('text/plain');
|
|
|
+
|
|
|
$file = $fileStorage->prepareFile($owner, 'file.txt', FileTypeEnum::NONE(), $author);
|
|
|
|
|
|
$this->assertEquals(null, $file->getOrganization());
|
|
|
@@ -229,10 +217,7 @@ class LocalStorageTest extends TestCase
|
|
|
*/
|
|
|
public function testPrepareFileNoFlush(): void
|
|
|
{
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['prepareFile'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('prepareFile');
|
|
|
|
|
|
$owner = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
$author = $this->getMockBuilder(Access::class)->getMock();
|
|
|
@@ -242,6 +227,8 @@ class LocalStorageTest extends TestCase
|
|
|
$this->entityManager->expects(self::once())->method('persist');
|
|
|
$this->entityManager->expects(self::never())->method('flush');
|
|
|
|
|
|
+ $this->fileUtils->method('guessMimeTypeFromFilename')->willReturn('text/plain');
|
|
|
+
|
|
|
$fileStorage->prepareFile(
|
|
|
$owner,
|
|
|
'file.txt',
|
|
|
@@ -249,7 +236,7 @@ class LocalStorageTest extends TestCase
|
|
|
$author,
|
|
|
false,
|
|
|
'NOBODY',
|
|
|
- null,
|
|
|
+ 'text/plain',
|
|
|
false
|
|
|
);
|
|
|
}
|
|
|
@@ -258,10 +245,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::write()
|
|
|
*/
|
|
|
public function testWriteNewFile(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['write'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('write');
|
|
|
|
|
|
$organization = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
|
|
|
@@ -312,10 +296,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::write()
|
|
|
*/
|
|
|
public function testWriteExistingFile(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['write'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('write');
|
|
|
|
|
|
$person = $this->getMockBuilder(Person::class)->getMock();
|
|
|
|
|
|
@@ -365,10 +346,7 @@ class LocalStorageTest extends TestCase
|
|
|
*/
|
|
|
public function testWriteExistingButMissingFile(): void
|
|
|
{
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['write'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('write');
|
|
|
|
|
|
$person = $this->getMockBuilder(Person::class)->getMock();
|
|
|
$author = $this->getMockBuilder(Access::class)->getMock();
|
|
|
@@ -398,10 +376,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::write()
|
|
|
*/
|
|
|
public function testWriteWithAccessOwner(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['write'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('write');
|
|
|
|
|
|
$access = $this->getMockBuilder(Access::class)->getMock();
|
|
|
$person = $this->getMockBuilder(Person::class)->getMock();
|
|
|
@@ -441,10 +416,7 @@ class LocalStorageTest extends TestCase
|
|
|
*/
|
|
|
public function testWriteWithNoName(): void
|
|
|
{
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['write'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('write');
|
|
|
|
|
|
$author = $this->getMockBuilder(Access::class)->getMock();
|
|
|
|
|
|
@@ -461,10 +433,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::makeFile()
|
|
|
*/
|
|
|
public function testMakeFile(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['makeFile'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('makeFile');
|
|
|
|
|
|
$organization = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
$author = $this->getMockBuilder(Access::class)->getMock();
|
|
|
@@ -497,10 +466,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::softDelete()
|
|
|
*/
|
|
|
public function testSoftdelete(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['softDelete'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('softDelete');
|
|
|
|
|
|
$author = $this->getMockBuilder(Access::class)->getMock();
|
|
|
$author->method('getId')->willReturn(123);
|
|
|
@@ -521,10 +487,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::hardDelete()
|
|
|
*/
|
|
|
public function testHardDelete(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['hardDelete'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('hardDelete');
|
|
|
|
|
|
$file = $this->getMockBuilder(File::class)->getMock();
|
|
|
$file->method('getSlug')->willReturn('key');
|
|
|
@@ -538,10 +501,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::hardDelete()
|
|
|
*/
|
|
|
public function testHardDeleteFailed(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['hardDelete'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('hardDelete');
|
|
|
|
|
|
$file = $this->getMockBuilder(File::class)->getMock();
|
|
|
$file->method('getSlug')->willReturn('key');
|
|
|
@@ -558,10 +518,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::getPrefix()
|
|
|
*/
|
|
|
public function testGetPrefixAccess(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['getPrefix'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('getPrefix');
|
|
|
|
|
|
$organization = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
$organization->method('getId')->willReturn(2);
|
|
|
@@ -579,10 +536,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::getPrefix()
|
|
|
*/
|
|
|
public function testGetPrefixOrganization(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['getPrefix'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('getPrefix');
|
|
|
|
|
|
$organization = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
$organization->method('getId')->willReturn(1);
|
|
|
@@ -596,10 +550,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::getPrefix()
|
|
|
*/
|
|
|
public function testGetPrefixPerson(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['getPrefix'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('getPrefix');
|
|
|
|
|
|
$person = $this->getMockBuilder(Person::class)->getMock();
|
|
|
$person->method('getId')->willReturn(1);
|
|
|
@@ -613,10 +564,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::getPrefix()
|
|
|
*/
|
|
|
public function testGetPrefixTemp(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['getPrefix'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('getPrefix');
|
|
|
|
|
|
$organization = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
$organization->method('getId')->willReturn(1);
|
|
|
@@ -630,10 +578,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::getPrefix()
|
|
|
*/
|
|
|
public function testGetPrefixWithType(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['getPrefix'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('getPrefix');
|
|
|
|
|
|
$organization = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
$organization->method('getId')->willReturn(1);
|
|
|
@@ -647,10 +592,7 @@ class LocalStorageTest extends TestCase
|
|
|
* @see LocalStorage::getOrganizationAndPersonFromOwner()
|
|
|
*/
|
|
|
public function testGetOrganizationAndPersonFromOwner(): void {
|
|
|
- $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
|
|
|
- ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
|
|
|
- ->setMethodsExcept(['getOrganizationAndPersonFromOwner'])
|
|
|
- ->getMock();
|
|
|
+ $fileStorage = $this->getMockForMethod('getOrganizationAndPersonFromOwner');
|
|
|
|
|
|
$organization = $this->getMockBuilder(Organization::class)->getMock();
|
|
|
$organization->method('getId')->willReturn(2);
|