Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Randy Čupić committed Sep 17, 2024
1 parent a230db7 commit c0138e2
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 37 deletions.
5 changes: 0 additions & 5 deletions bundle/DependencyInjection/NetgenRemoteMediaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Netgen\Bundle\RemoteMediaBundle\DependencyInjection;

use InvalidArgumentException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
Expand Down Expand Up @@ -40,10 +39,6 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

if (!isset($config['provider'])) {
throw new InvalidArgumentException('The "provider" option must be set');
}

$container->setParameter('netgen_remote_media.remove_unused_resources', $config['remove_unused']);
$container->setAlias('netgen_remote_media.provider', 'netgen_remote_media.provider.' . $config['provider']);

Expand Down
3 changes: 2 additions & 1 deletion lib/Core/Provider/Cloudinary/Resolver/SearchExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,10 @@ private function resolveResourceIds(Query $query): ?string
return null;
}

$folderMode = $this->folderMode;
$resourceIds = array_unique(
array_map(
static fn ($remoteId) => CloudinaryRemoteId::fromRemoteId($remoteId, $this->folderMode)->getResourceId(),
static fn ($remoteId) => CloudinaryRemoteId::fromRemoteId($remoteId, $folderMode)->getResourceId(),
$query->getRemoteIds(),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Netgen\Bundle\RemoteMediaBundle\Tests\DependencyInjection;

use InvalidArgumentException;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Netgen\Bundle\RemoteMediaBundle\DependencyInjection\NetgenRemoteMediaExtension;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

#[CoversClass(NetgenRemoteMediaExtension::class)]
final class NetgenRemoteMediaExtensionTest extends AbstractExtensionTestCase
Expand All @@ -17,10 +17,10 @@ public function testItSetsValidContainerParameters(): void
$this->setParameter('kernel.bundles', []);
$this->load();

$this->assertContainerBuilderHasParameter('netgen_remote_media.parameters.testprovider.account_name', 'testname');
$this->assertContainerBuilderHasParameter('netgen_remote_media.parameters.testprovider.account_key', 'testkey');
$this->assertContainerBuilderHasParameter('netgen_remote_media.parameters.testprovider.account_secret', 'testsecret');
$this->assertContainerBuilderHasParameter('netgen_remote_media.parameters.testprovider.upload_prefix', 'testprefix');
$this->assertContainerBuilderHasParameter('netgen_remote_media.parameters.cloudinary.account_name', 'testname');
$this->assertContainerBuilderHasParameter('netgen_remote_media.parameters.cloudinary.account_key', 'testkey');
$this->assertContainerBuilderHasParameter('netgen_remote_media.parameters.cloudinary.account_secret', 'testsecret');
$this->assertContainerBuilderHasParameter('netgen_remote_media.parameters.cloudinary.upload_prefix', 'testprefix');
$this->assertContainerBuilderHasParameter('netgen_remote_media.remove_unused_resources', false);
$this->assertContainerBuilderHasParameter('netgen_remote_media.cache.pool_name', 'cache.app');
$this->assertContainerBuilderHasParameter('netgen_remote_media.cache.ttl', 3600);
Expand All @@ -46,12 +46,12 @@ public function testItSetsValidContainerParameters(): void

$this->assertContainerBuilderHasAlias('netgen_remote_media.provider.cloudinary.gateway.inner', 'netgen_remote_media.provider.cloudinary.gateway.api');
$this->assertContainerBuilderHasAlias('netgen_remote_media.provider.cloudinary.gateway', 'netgen_remote_media.provider.cloudinary.gateway.cached');
$this->assertContainerBuilderHasAlias('netgen_remote_media.provider', 'netgen_remote_media.provider.testprovider');
$this->assertContainerBuilderHasAlias('netgen_remote_media.provider', 'netgen_remote_media.provider.cloudinary');
}

public function testWithoutProviderParameter(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(InvalidConfigurationException::class);
$this->load(['provider' => null]);
}

Expand Down Expand Up @@ -102,7 +102,7 @@ protected function getContainerExtensions(): array
protected function getMinimalConfiguration(): array
{
return [
'provider' => 'testprovider',
'provider' => 'cloudinary',
'account_name' => 'testname',
'account_key' => 'testkey',
'account_secret' => 'testsecret',
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/Core/Provider/Cloudinary/CloudinaryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ protected function setUp(): void
new DateTimeFactory(),
new UploadOptionsResolver(
new VisibilityTypeConverter(),
CloudinaryProvider::FOLDER_MODE_FIXED,
['image', 'video'],
$this->mimeTypes,
),
[],
[],
CloudinaryProvider::FOLDER_MODE_FIXED,
$this->logger,
false,
);
Expand Down
35 changes: 35 additions & 0 deletions tests/lib/Core/Provider/Cloudinary/CloudinaryRemoteIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Netgen\RemoteMedia\Tests\Core\Provider\Cloudinary;

use Netgen\RemoteMedia\API\Values\Folder;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryRemoteId;
use Netgen\RemoteMedia\Exception\Cloudinary\InvalidRemoteIdException;
use Netgen\RemoteMedia\Exception\NotSupportedException;
use Netgen\RemoteMedia\Tests\AbstractTestCase;
use PHPUnit\Framework\Attributes\CoversClass;

Expand Down Expand Up @@ -78,6 +80,39 @@ public function testFromRemoteId(): void
);
}

public function testFromRemoteIdInDynamicFolderMode(): void
{
$remoteId = CloudinaryRemoteId::fromRemoteId(
'private|video|media/videos/my_test_video.mp4',
CloudinaryProvider::FOLDER_MODE_DYNAMIC,
);

self::assertSame(
'private|video|media/videos/my_test_video.mp4',
$remoteId->getRemoteId(),
);

self::assertSame(
'media/videos/my_test_video.mp4',
$remoteId->getResourceId(),
);

self::assertSame(
'video',
$remoteId->getResourceType(),
);

self::assertSame(
'private',
$remoteId->getType(),
);

self::expectException(NotSupportedException::class);
self::expectExceptionMessage('Provider "Cloudinary" does not support "fetching folder from path in "dynamic" folder mode".');

$remoteId->getFolder();
}

public function testFromInvalidRemoteId(): void
{
self::expectException(InvalidRemoteIdException::class);
Expand Down
78 changes: 72 additions & 6 deletions tests/lib/Core/Provider/Cloudinary/Factory/RemoteResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Netgen\RemoteMedia\API\Factory\FileHash as FileHashFactoryInterface;
use Netgen\RemoteMedia\API\Values\Folder;
use Netgen\RemoteMedia\API\Values\RemoteResource;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\ResourceType as ResourceTypeConverter;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\VisibilityType as VisibilityTypeConverter;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Factory\CloudinaryConfiguration;
Expand All @@ -20,7 +21,9 @@
#[CoversClass(RemoteResourceFactory::class)]
final class RemoteResourceTest extends AbstractTestCase
{
protected RemoteResourceFactory $remoteResourceFactory;
protected RemoteResourceFactory $fixedFolderModeRemoteResourceFactory;

protected RemoteResourceFactory $dynamicFolderModeRemoteResourceFactory;

protected FileHashFactoryInterface|MockObject $fileHashFactoryMock;

Expand All @@ -38,15 +41,23 @@ protected function setUp(): void

$cloudinaryConfigurationFactory->create();

$this->remoteResourceFactory = new RemoteResourceFactory(
$this->fixedFolderModeRemoteResourceFactory = new RemoteResourceFactory(
new ResourceTypeConverter(),
new VisibilityTypeConverter(),
$this->fileHashFactoryMock,
CloudinaryProvider::FOLDER_MODE_FIXED,
);

$this->dynamicFolderModeRemoteResourceFactory = new RemoteResourceFactory(
new ResourceTypeConverter(),
new VisibilityTypeConverter(),
$this->fileHashFactoryMock,
CloudinaryProvider::FOLDER_MODE_DYNAMIC,
);
}

#[DataProvider('createDataProvider')]
public function testCreate(array $cloudinaryResponse, RemoteResource $expectedResource): void
public function testCreate(array $cloudinaryResponse, RemoteResource $expectedResource, string $folderMode = CloudinaryProvider::FOLDER_MODE_FIXED): void
{
if (!($cloudinaryResponse['etag'] ?? null)) {
$this->fileHashFactoryMock
Expand All @@ -56,7 +67,9 @@ public function testCreate(array $cloudinaryResponse, RemoteResource $expectedRe
->willReturn('a522f23sf81aa0afd03387c37e2b6eax');
}

$resource = $this->remoteResourceFactory->create($cloudinaryResponse);
$resource = $folderMode === CloudinaryProvider::FOLDER_MODE_FIXED
? $this->fixedFolderModeRemoteResourceFactory->create($cloudinaryResponse)
: $this->dynamicFolderModeRemoteResourceFactory->create($cloudinaryResponse);

self::assertRemoteResourceSame(
$expectedResource,
Expand All @@ -69,15 +82,15 @@ public function testCreateMissingPublicId(): void
self::expectException(InvalidDataException::class);
self::expectExceptionMessage('Missing required "public_id" property!');

$this->remoteResourceFactory->create(['test' => 'test']);
$this->fixedFolderModeRemoteResourceFactory->create(['test' => 'test']);
}

public function testCreateMissingUrls(): void
{
self::expectException(InvalidDataException::class);
self::expectExceptionMessage('Missing required "secure_url" or "url" property!');

$this->remoteResourceFactory->create(['public_id' => 'test']);
$this->dynamicFolderModeRemoteResourceFactory->create(['public_id' => 'test']);
}

public static function createDataProvider(): array
Expand Down Expand Up @@ -139,6 +152,7 @@ public static function createDataProvider(): array
'source' => 'user_upload',
],
),
CloudinaryProvider::FOLDER_MODE_FIXED,
],
[
[
Expand Down Expand Up @@ -177,6 +191,7 @@ public static function createDataProvider(): array
'created_at' => '2013-06-23T13:59:18Z',
],
),
CloudinaryProvider::FOLDER_MODE_FIXED,
],
[
[
Expand Down Expand Up @@ -223,6 +238,7 @@ public static function createDataProvider(): array
'overwritten' => 'false',
],
),
CloudinaryProvider::FOLDER_MODE_DYNAMIC,
],
[
[
Expand All @@ -245,6 +261,7 @@ public static function createDataProvider(): array
'variation1',
'variation2',
],
'asset_folder' => 'media/test',
],
new RemoteResource(
remoteId: 'authenticated|video|c87hg9xfxrd4itiim3t0',
Expand All @@ -255,6 +272,7 @@ public static function createDataProvider(): array
originalFilename: 'c87hg9xfxrd4itiim3t0.mp4',
version: '1371995958',
visibility: 'protected',
folder: Folder::fromPath('media/test'),
size: 120253,
tags: ['tag1', 'tag2'],
metadata: [
Expand All @@ -266,6 +284,7 @@ public static function createDataProvider(): array
'overwritten' => 'false',
],
),
CloudinaryProvider::FOLDER_MODE_DYNAMIC,
],
[
[
Expand Down Expand Up @@ -309,6 +328,7 @@ public static function createDataProvider(): array
'test' => 'test',
],
),
CloudinaryProvider::FOLDER_MODE_DYNAMIC,
],
[
[
Expand Down Expand Up @@ -352,6 +372,52 @@ public static function createDataProvider(): array
'test2' => 'test2',
],
),
CloudinaryProvider::FOLDER_MODE_FIXED,
],
[
[
'public_id' => 'c87hg9xfxrd4itiim3t0',
'version' => 1371995958,
'signature' => 'f8645b000be7d717599affc89a068157e4748276',
'format' => 'zip',
'resource_type' => 'raw',
'created_at' => '2011-06-23T13:59:18Z',
'bytes' => 12025,
'type' => 'test',
'secure_url' => 'https://res.cloudinary.com/testcloud/v1371995958/raw/media/raw/new/c87hg9xfxrd4itiim3t0',
'etag' => 'e522f43cf89aa0afd03387c38e2b6e29',
'context' => [
'test' => 'test',
'custom' => [
'test2' => 'test2',
],
'original_filename' => 'test.mp4',
],
'asset_folder' => 'media/raw/new',
],
new RemoteResource(
remoteId: 'test|raw|c87hg9xfxrd4itiim3t0',
type: 'other',
url: 'https://res.cloudinary.com/testcloud/raw/test/c87hg9xfxrd4itiim3t0',
md5: 'e522f43cf89aa0afd03387c38e2b6e29',
name: 'c87hg9xfxrd4itiim3t0',
originalFilename: 'test.mp4',
version: '1371995958',
visibility: 'public',
folder: Folder::fromPath('media/raw/new'),
size: 12025,
tags: [],
metadata: [
'signature' => 'f8645b000be7d717599affc89a068157e4748276',
'format' => 'zip',
'created_at' => '2011-06-23T13:59:18Z',
],
context: [
'test' => 'test',
'test2' => 'test2',
],
),
CloudinaryProvider::FOLDER_MODE_DYNAMIC,
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Netgen\RemoteMedia\API\Values\AuthToken;
use Netgen\RemoteMedia\API\Values\RemoteResource;
use Netgen\RemoteMedia\API\Values\StatusData;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryRemoteId;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Gateway\Cache\Psr6CachedGateway;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\GatewayInterface;
Expand Down Expand Up @@ -51,12 +52,14 @@ protected function setUp(): void
$this->taggableCachedGateway = new Psr6CachedGateway(
$this->apiGatewayMock,
$this->taggableCache,
CloudinaryProvider::FOLDER_MODE_FIXED,
self::CACHE_TTL,
);

$this->nonTaggableCachedGateway = new Psr6CachedGateway(
$this->apiGatewayMock,
$this->nonTaggableCache,
CloudinaryProvider::FOLDER_MODE_DYNAMIC,
self::CACHE_TTL,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Netgen\RemoteMedia\API\Values\AuthToken;
use Netgen\RemoteMedia\API\Values\RemoteResource;
use Netgen\RemoteMedia\API\Values\StatusData;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryRemoteId;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\ResourceType as ResourceTypeConverter;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\VisibilityType as VisibilityTypeConverter;
Expand Down Expand Up @@ -61,6 +62,7 @@ protected function setUp(): void
new SearchExpressionResolver(
new ResourceTypeConverter(),
new VisibilityTypeConverter(),
CloudinaryProvider::FOLDER_MODE_FIXED,
),
new AuthTokenResolver(CloudinaryConfigurationInitializer::ENCRYPTION_KEY),
);
Expand Down Expand Up @@ -239,6 +241,7 @@ public function testIsEncryptionEnabled(): void
new SearchExpressionResolver(
new ResourceTypeConverter(),
new VisibilityTypeConverter(),
CloudinaryProvider::FOLDER_MODE_FIXED,
),
new AuthTokenResolver(),
);
Expand Down
Loading

0 comments on commit c0138e2

Please sign in to comment.