Skip to content

Commit

Permalink
[Tests] Aligned existing tests with the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Dec 12, 2022
1 parent a198a5d commit 5bb01b3
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 140 deletions.
2 changes: 1 addition & 1 deletion src/contracts/Test/IbexaTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Ibexa\Contracts\Core\Persistence\TransactionHandler;
use Ibexa\Contracts\Core\Repository;
use Ibexa\Contracts\Core\Test\Persistence\Fixture\YamlFixture;
use Ibexa\Core\IO\Adapter\LocalSiteAccessAwareFilesystemAdapter;
use Ibexa\Core\IO\Flysystem\Adapter\LocalSiteAccessAwareFilesystemAdapter;
use JMS\TranslationBundle\JMSTranslationBundle;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use Liip\ImagineBundle\LiipImagineBundle;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Resources/settings/repository/siteaccessaware.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ services:

Ibexa\Core\Repository\SiteAccessAware\Config\IOConfigResolver:
arguments:
- '%ibexa.io.dir.storage%'
- '%ibexa.io.dir.root%'
- '%ibexa.legacy.url_prefix%'
- '%ibexa.url_prefix%'
2 changes: 2 additions & 0 deletions src/lib/Resources/settings/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ parameters:
ibexa.io.dir.root: '%ibexa.io.dir.storage%'
ibexa.site_access.list: [test]
ibexa.site_access.config.default.anonymous_user_id: 10
ibexa.site_access.config.default.io.permissions.files: 0644
ibexa.site_access.config.default.io.permissions.directories: 0755

services:
ibexa.api.persistence_handler:
Expand Down

This file was deleted.

7 changes: 4 additions & 3 deletions tests/bundle/IO/Migration/FileMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Ibexa\Tests\Bundle\IO\Migration;

use DateTime;
use Ibexa\Bundle\IO\ApiLoader\HandlerRegistry;
use Ibexa\Bundle\IO\Migration\FileMigrator\FileMigrator;
use Ibexa\Contracts\Core\IO\BinaryFile;
Expand Down Expand Up @@ -48,8 +49,8 @@ protected function setUp(): void
$this->metadataFlysystem = $this->createMock(IOMetadataHandler\Flysystem::class);
$this->metadataLegacyDFSCluster = $this->createMock(IOMetadataHandler\LegacyDFSCluster::class);

$this->binaryFlysystemFrom = $this->createMock(IOBinarydataHandler\Flysystem::class);
$this->binaryFlysystemTo = $this->createMock(IOBinarydataHandler\Flysystem::class);
$this->binaryFlysystemFrom = $this->createMock(IOBinarydataHandler::class);
$this->binaryFlysystemTo = $this->createMock(IOBinarydataHandler::class);

$this->fileMigrator = new FileMigrator($this->metadataHandlerRegistry, $this->binaryHandlerRegistry);
}
Expand Down Expand Up @@ -84,7 +85,7 @@ public function testMigrateFile(): void

$binaryFile = new BinaryFile();
$binaryFile->id = '1234.jpg';
$binaryFile->mtime = new \DateTime();
$binaryFile->mtime = new DateTime();
$binaryFile->size = 12345;
$binaryFile->uri = '1/1234.jpg';

Expand Down
31 changes: 31 additions & 0 deletions tests/integration/Core/LegacyTestContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
namespace Ibexa\Tests\Integration\Core;

use Ibexa\Bundle\Core\DependencyInjection\Compiler\ConsoleCommandPass;
use Ibexa\Bundle\Core\SiteAccess\Config\ComplexConfigProcessor;
use Ibexa\Contracts\Core\SiteAccess\ConfigProcessor;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\Base\Container\Compiler;
use Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessService;
use Ibexa\Tests\Integration\Core\Repository\Container\Compiler\SetAllServicesPublicPass;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -52,6 +57,8 @@ private function initCoreTestContainerBuilder(): void
$this->setParameter('ibexa.kernel.root_dir', $installDir);

$this->registerCompilerPasses();

$this->registerCoreBundleServices();
}

/**
Expand Down Expand Up @@ -138,4 +145,28 @@ public function getCoreLoader(): LoaderInterface

return $this->coreLoader;
}

/**
* Register needed services from Core Bundle.
*
* Note: if a service from a bundle layer is required to be registered here, it most likely
* means that the service should be located in core lib layer in the first place.
*/
private function registerCoreBundleServices(): void
{
$this->registerSiteAccessConfigProcessingServices();
}

private function registerSiteAccessConfigProcessingServices(): void
{
if ($this->hasDefinition(ConfigProcessor::class)) {
return;
}

$definition = new Definition(ComplexConfigProcessor::class);
$definition->setArgument('$configResolver', new Reference(ConfigResolverInterface::class));
$definition->setArgument('$siteAccessService', new Reference(SiteAccessService::class));

$this->setDefinition(ConfigProcessor::class, $definition);
}
}
3 changes: 2 additions & 1 deletion tests/lib/IO/FilePathNormalizer/FlysystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Ibexa\Core\IO\FilePathNormalizer\Flysystem;
use Ibexa\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter;
use League\Flysystem\WhitespacePathNormalizer;
use PHPUnit\Framework\TestCase;

final class FlysystemTest extends TestCase
Expand All @@ -23,7 +24,7 @@ final class FlysystemTest extends TestCase
public function setUp(): void
{
$this->slugConverter = $this->createMock(SlugConverter::class);
$this->filePathNormalizer = new Flysystem($this->slugConverter);
$this->filePathNormalizer = new Flysystem($this->slugConverter, new WhitespacePathNormalizer());
}

/**
Expand Down
123 changes: 56 additions & 67 deletions tests/lib/IO/IOMetadataHandler/FlysystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ protected function setUp(): void
$this->handler = new Flysystem($this->filesystem);
}

public function testCreate()
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function testCreate(): void
{
// good example of bad responsibilities... since create also loads, we test the same thing twice
$spiCreateStruct = new SPIBinaryFileCreateStruct();
Expand All @@ -42,17 +45,16 @@ public function testCreate()
$expectedSpiBinaryFile->mtime = new DateTime('@1307155200');

$this->filesystem
->expects($this->once())
->method('getMetadata')
->expects(self::once())
->method('fileSize')
->with($spiCreateStruct->id)
->willReturn(123);

$this->filesystem
->expects(self::once())
->method('lastModified')
->with($spiCreateStruct->id)
->will(
$this->returnValue(
[
'timestamp' => 1307155200,
'size' => 123,
]
)
);
->willReturn(1307155200);

$spiBinaryFile = $this->handler->create($spiCreateStruct);

Expand All @@ -66,53 +68,32 @@ public function testDelete()
$this->handler->delete('prefix/my/file.png');
}

public function testLoad()
public function testLoad(): void
{
$expectedSpiBinaryFile = new SPIBinaryFile();
$expectedSpiBinaryFile->id = 'prefix/my/file.png';
$filePath = 'prefix/my/file.png';
$expectedSpiBinaryFile->id = $filePath;
$expectedSpiBinaryFile->size = 123;
$expectedSpiBinaryFile->mtime = new DateTime('@1307155200');

$this->filesystem
->expects($this->once())
->method('getMetadata')
->with('prefix/my/file.png')
->will(
$this->returnValue(
[
'timestamp' => 1307155200,
'size' => 123,
]
)
);

$spiBinaryFile = $this->handler->load('prefix/my/file.png');
->expects(self::once())
->method('fileSize')
->with($filePath)
->willReturn(123);

$this->filesystem
->expects(self::once())
->method('lastModified')
->with($filePath)
->willReturn(1307155200);

$spiBinaryFile = $this->handler->load($filePath);

$this->assertInstanceOf(SPIBinaryFile::class, $spiBinaryFile);
$this->assertEquals($expectedSpiBinaryFile, $spiBinaryFile);
}

/**
* The timestamp index can be unset with some handlers, like AWS/S3.
*/
public function testLoadNoTimestamp()
{
$this->filesystem
->expects($this->once())
->method('getMetadata')
->with('prefix/my/file.png')
->will(
$this->returnValue(
[
'size' => 123,
]
)
);

$spiBinaryFile = $this->handler->load('prefix/my/file.png');
$this->assertNull($spiBinaryFile->mtime);
}

public function testLoadNotFound(): void
{
$notExistentPath = 'prefix/my/file.png';
Expand All @@ -127,42 +108,50 @@ public function testLoadNotFound(): void
$this->handler->load($notExistentPath);
}

public function testExists()
/**
* @dataProvider getDataForFileExists
*/
public function testExists(string $filePath, bool $exists): void
{
$this->filesystem
->expects($this->once())
->method('has')
->with('prefix/my/file.png')
->will($this->returnValue(true));
->method('fileExists')
->with($filePath)
// Note: test proper proxying of Flysystem call as this is a unit test
->willReturn($exists);

self::assertTrue($this->handler->exists('prefix/my/file.png'));
self::assertSame($exists, $this->handler->exists($filePath));
}

public function testExistsNot()
public function getDataForFileExists(): iterable
{
$this->filesystem
->expects($this->once())
->method('has')
->with('prefix/my/file.png')
->will($this->returnValue(false));
$filePath = 'prefix/my/file.png';

self::assertFalse($this->handler->exists('prefix/my/file.png'));
yield 'exists' => [$filePath, true];
yield 'does not exist' => [$filePath, false];
}

public function testGetMimeType()
public function testGetMimeType(): void
{
$fileName = 'file.txt';
$this->filesystem
->expects($this->once())
->method('getMimeType')
->with('file.txt')
->will($this->returnValue('text/plain'));
->expects(self::once())
->method('mimeType')
->with($fileName)
// Note: test proper proxying of Flysystem call as this is a unit test
->willReturn('text/plain');

self::assertEquals('text/plain', $this->handler->getMimeType('file.txt'));
self::assertEquals('text/plain', $this->handler->getMimeType($fileName));
}

public function testDeleteDirectory()
public function testDeleteDirectory(): void
{
$this->filesystem->expects($this->never())->method('deleteDir');
// test this actually doesn't call Flysystem's FilesystemOperator::deleteDirectory
// (see \Ibexa\Core\IO\IOMetadataHandler\Flysystem::deleteDirectory for more details)
$this->filesystem
->expects(self::never())
->method('deleteDirectory');

$this->handler->deleteDirectory('some/path');
}
}
Expand Down

0 comments on commit 5bb01b3

Please sign in to comment.