Skip to content

Commit

Permalink
[TASK] Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayacoo committed Feb 18, 2024
1 parent 7fc0e74 commit e1470c7
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 28 deletions.
14 changes: 14 additions & 0 deletions Build/bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env php
<?php

include_once __DIR__ . '/../../.Build/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Symfony\Component\Translation\Command\XliffLintCommand;
use Symfony\Component\Yaml\Command\LintCommand;

$application = new Application();
$application->add(new XliffLintCommand(null, null, null, false));
$application->add(new LintCommand());

exit($application->run());
2 changes: 1 addition & 1 deletion Classes/Command/UpdateMetadataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($audios as $audio) {
$file = $this->resourceFactory->getFileObject($audio['uid']);
$metaData = $soundcloudHelper->getMetaData($file);
if (!empty($metaData)) {
if ($metaData !== []) {
$newMetaData = [
'width' => (int)$metaData['width'],
'height' => (int)$metaData['height'],
Expand Down
28 changes: 13 additions & 15 deletions Classes/Helper/SoundcloudHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ protected function transformMediaIdToFile($mediaId, Folder $targetFolder, $fileE
$fileName = $mediaId . '.' . $fileExtension;

$oEmbed = $this->getOEmbedData($mediaId);
if (!empty($oEmbed['title'])) {
$title = $this->handleSoundcloudTitle($oEmbed['title']);
if ($title !== '' && $title !== '0') {
$fileName = $title . '.' . $fileExtension;
}
$title = $this->handleSoundcloudTitle($oEmbed['title'] ?? '');
if ($title !== '' && $title !== '0') {
$fileName = $title . '.' . $fileExtension;
}
$file = $this->createNewFile($targetFolder, $fileName, $mediaId);
}
Expand All @@ -72,12 +70,12 @@ public function getPublicUrl(File $file, $relativeToCurrentScript = false)
public function getPreviewImage(File $file)
{
$properties = $file->getProperties();
$previewImageUrl = $properties['soundcloud_thumbnail_url'] ?? '';
$previewImageUrl = trim($properties['soundcloud_thumbnail_url'] ?? '');

$audioId = $this->getOnlineMediaId($file);
$temporaryFileName = $this->getTempFolderPath() . 'soundcloud_' . md5($audioId) . '.jpg';

if (!empty($previewImageUrl)) {
if ($previewImageUrl !== '') {
$previewImage = GeneralUtility::getUrl($previewImageUrl);
file_put_contents($temporaryFileName, $previewImage);
GeneralUtility::fixPermissions($temporaryFileName);
Expand All @@ -93,23 +91,23 @@ public function getPreviewImage(File $file)
*
* @return array with metadata
*/
public function getMetaData(File $file)
public function getMetaData(File $file): array
{
$metaData = [];

$oEmbed = $this->getOEmbedData($this->getOnlineMediaId($file));
if ($oEmbed) {
if ($oEmbed !== null) {
$metaData['width'] = (int)$oEmbed['width'];
// We only get the value "100%" from the oEmbed query
// The 225 pixels come from the 16:9 format at 400 pixels
$metaData['height'] = 225;
if (empty($file->getProperty('title'))) {
$metaData['title'] = $this->handleSoundcloudTitle($oEmbed['title']);
if ($file->getProperty('title') !== '') {
$metaData['title'] = $this->handleSoundcloudTitle($oEmbed['title'] ?? '');
}
$metaData['author'] = $oEmbed['author_name'];
$metaData['soundcloud_html'] = $oEmbed['html'];
$metaData['soundcloud_thumbnail_url'] = $oEmbed['thumbnail_url'];
$metaData['soundcloud_author_url'] = $oEmbed['author_url'];
$metaData['author'] = $oEmbed['author_name'] ?? '';
$metaData['soundcloud_html'] = $oEmbed['html'] ?? '';
$metaData['soundcloud_thumbnail_url'] = $oEmbed['thumbnail_url'] ?? '';
$metaData['soundcloud_author_url'] = $oEmbed['author_url'] ?? '';
}

return $metaData;
Expand Down
2 changes: 1 addition & 1 deletion Classes/Rendering/SoundcloudPreviewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function renderPageModulePreviewContent(GridColumnItem $item): string
$fileReferences = BackendUtility::resolveFileReferences('tt_content', 'assets', $row);
foreach ($fileReferences as $fileReferenceObject) {
// Do not show previews of hidden references
if ($fileReferenceObject->getProperty('hidden')) {
if ($fileReferenceObject->getProperty('hidden') === true) {
continue;
}
$fileObject = $fileReferenceObject->getOriginalFile();
Expand Down
2 changes: 1 addition & 1 deletion Classes/Rendering/SoundcloudRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function render(FileInterface $file, $width, $height, array $options = []
{
$output = $file->getProperty('soundcloud_html') ?? '';
if ($this->getPrivacySetting()) {
$output = str_replace('src', 'data-name="iframe-soundcloud" data-src', (string)$output);
$output = str_replace('src', 'data-name="iframe-soundcloud" data-src', $output);
}

$modifySoundcloudOutputEvent = $this->eventDispatcher->dispatch(
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Domain/Repository/FileRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class FileRepositoryTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = ['ayacoo/ayacoo-soundcloud'];
protected array $testExtensionsToLoad = ['ayacoo_soundcloud'];

private FileRepository $subject;

Expand Down
100 changes: 96 additions & 4 deletions Tests/Unit/Helper/SoundcloudHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types=1);

namespace Ayacoo\AyacooSoundcloud\Tests\Unit\Domain\Model;
namespace Ayacoo\AyacooSoundcloud\Tests\Unit\Helper;

use Ayacoo\AyacooSoundcloud\Helper\SoundcloudHelper;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

Expand All @@ -24,7 +25,7 @@ protected function setUp(): void
/**
* @test
*/
public function isAbstractEntity(): void
public function isAbstractOEmbedHelper(): void
{
self::assertInstanceOf(AbstractOEmbedHelper::class, $this->subject);
}
Expand Down Expand Up @@ -66,7 +67,6 @@ public static function handleSoundcloudTitleDataProvider(): array

/**
* @test
* @return void
*/
public function getOEmbedUrlWithJsonFormat(): void
{
Expand All @@ -82,7 +82,6 @@ public function getOEmbedUrlWithJsonFormat(): void

/**
* @test
* @return void
*/
public function getOEmbedUrlWithXmlFormat(): void
{
Expand Down Expand Up @@ -122,6 +121,99 @@ public static function getAudioIdDataProvider(): array
];
}

/**
* @test
*/
public function getOEmbedDataWithEmbedDataReturnsOptimizedArray()
{
$fileResourceMock = $this->createMock(File::class);
$fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('audio/soundcloud');

/** @var SoundcloudHelper|\PHPUnit\Framework\MockObject\MockObject $soundcloudHelperMock */
$soundcloudHelperMock = $this->getMockBuilder(SoundcloudHelper::class)
->setConstructorArgs(['soundcloud'])
->onlyMethods(['getOEmbedData', 'getOnlineMediaId'])
->getMock();

$title = 'Alan Walker, Dash Berlin, Vikkstar - Better Off (Alone, Pt. III) by Alan Walker';
$author = 'Dash Berlin';
$oEmbedData = [
'width' => 100,
'height' => '100%',
'title' => $title,
'author_name' => $author,
];

$soundcloudHelperMock->expects(self::any())
->method('getOnlineMediaId')
->with($fileResourceMock)
->willReturn('alanwalker/better-off-alone-pt-iii-1');
$soundcloudHelperMock->expects(self::any())->method('getOEmbedData')->willReturn($oEmbedData);

$expected = [
'width' => 100,
'height' => 225,
'title' => $title,
'author' => $author,
'soundcloud_html' => '',
'soundcloud_thumbnail_url' => '',
'soundcloud_author_url' => '',
];

$result = $soundcloudHelperMock->getMetaData($fileResourceMock);

self::assertSame($expected, $result);
}

/**
* @test
*/
public function getOEmbedDataWithoutEmbedDataReturnsEmptyArray()
{
$fileResourceMock = $this->createMock(File::class);
$fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('audio/soundcloud');

/** @var SoundcloudHelper|\PHPUnit\Framework\MockObject\MockObject $soundcloudHelperMock */
$soundcloudHelperMock = $this->getMockBuilder(SoundcloudHelper::class)
->setConstructorArgs(['soundcloud'])
->onlyMethods(['getOEmbedData', 'getOnlineMediaId'])
->getMock();

$soundcloudHelperMock->expects(self::any())
->method('getOnlineMediaId')
->with($fileResourceMock)
->willReturn('alanwalker/better-off-alone-pt-iii-1');
$soundcloudHelperMock->expects(self::any())->method('getOEmbedData')->willReturn(null);


$result = $soundcloudHelperMock->getMetaData($fileResourceMock);
self::assertSame([], $result);
}

/**
* @test
*/
public function getPublicUrlReturnsPublicUrl() {
$fileResourceMock = $this->createMock(File::class);
$fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('audio/soundcloud');

/** @var SoundcloudHelper|\PHPUnit\Framework\MockObject\MockObject $soundcloudHelperMock */
$soundcloudHelperMock = $this->getMockBuilder(SoundcloudHelper::class)
->setConstructorArgs(['soundcloud'])
->onlyMethods(['getOnlineMediaId'])
->getMock();

$soundcloudHelperMock->expects(self::any())
->method('getOnlineMediaId')
->with($fileResourceMock)
->willReturn('alanwalker/better-off-alone-pt-iii-1');

$expected = 'https://soundcloud.com/alanwalker/better-off-alone-pt-iii-1';

$result = $soundcloudHelperMock->getPublicUrl($fileResourceMock);
self::assertSame($expected, $result);
}

private function buildReflectionForProtectedFunction(string $methodName, array $params)
{
$reflectionCalendar = new \ReflectionClass($this->subject);
Expand Down
79 changes: 79 additions & 0 deletions Tests/Unit/Rendering/SoundcloudRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Ayacoo\AyacooSoundcloud\Tests\Unit\Rendering;

use Ayacoo\AyacooSoundcloud\Helper\SoundcloudHelper;
use Ayacoo\AyacooSoundcloud\Rendering\SoundcloudRenderer;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

final class SoundcloudRendererTest extends UnitTestCase
{
private SoundcloudRenderer $subject;

protected bool $resetSingletonInstances = true;

protected function setUp(): void
{
parent::setUp();

$eventDispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
//$eventDispatcherMock->expects(self::atLeastOnce())->method('dispatch')->with(self::anything())->willReturnArgument(0);

$configurationManager = $this->getMockBuilder(ConfigurationManager::class)
->onlyMethods(['getConfiguration'])
->disableOriginalConstructor()
->getMock();

$this->subject = new SoundcloudRenderer($eventDispatcherMock, $configurationManager);
}

/**
* @test
*/
public function hasFileRendererInterface(): void
{
self::assertInstanceOf(FileRendererInterface::class, $this->subject);
}

/**
* @test
*/
public function canRenderWithMatchingMimeTypeReturnsTrue(): void
{
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['onlineMediaHelpers']['soundcloud'] = SoundcloudHelper::class;

$fileResourceMock = $this->createMock(File::class);
$fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('audio/soundcloud');
$fileResourceMock->expects(self::any())->method('getExtension')->willReturn('soundcloud');

$result = $this->subject->canRender($fileResourceMock);
self::assertTrue($result);
}

/**
* @test
*/
public function canRenderWithMatchingMimeTypeReturnsFalse(): void
{
$fileResourceMock = $this->createMock(File::class);
$fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('video/youtube');
$fileResourceMock->expects(self::any())->method('getExtension')->willReturn('youtube');

$result = $this->subject->canRender($fileResourceMock);
self::assertFalse($result);
}

private function buildReflectionForProtectedFunction(string $methodName, array $params)
{
$reflectionCalendar = new \ReflectionClass($this->subject);
$method = $reflectionCalendar->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($this->subject, $params);
}
}
18 changes: 13 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
"typo3/cms-core": "^12.4"
},
"require-dev": {
"typo3/coding-standards": "^0.5.5",
"friendsofphp/php-cs-fixer": "^3.47.1",
"typo3/testing-framework": "^8",
"friendsofphp/php-cs-fixer": "^3.49.0",
"helmich/typo3-typoscript-lint": "^3.1.0",
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan": "^1.10.57",
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-strict-rules": "^1.5.2",
"phpunit/phpunit": "^10.5"
"phpunit/phpunit": "^10.5",
"saschaegerer/phpstan-typo3": "^1.10.0",
"symfony/console": "^5.4 || ^6.4 || ^7.0",
"symfony/translation": "^5.4 || ^6.4 || ^7.0",
"typo3/coding-standards": "^0.5.5",
"typo3/testing-framework": "^8"
},
"autoload": {
"psr-4": {
Expand All @@ -48,7 +52,11 @@
"ci:php:cs": "php ./.Build/bin/php-cs-fixer fix --dry-run -v --config ./Build/php-cs-fixer/php-cs-fixer.php ./",
"ci:php:csfix": "php ./.Build/bin/php-cs-fixer fix -v --config ./Build/php-cs-fixer/php-cs-fixer.php ./",
"ci:php:lint": "find .*.php *.php Classes Configuration Tests -name '*.php' -print0 | xargs -r -0 -n 1 -P 4 php -l",
"ci:tests:unit": ".Build/bin/phpunit -c Build/phpunit/UnitTests.xml Tests/Unit"
"ci:php:sniff": "phpcs Classes Configuration Tests",
"ci:php:stan": "phpstan --no-progress",
"ci:tests:unit": ".Build/bin/phpunit -c Build/phpunit/UnitTests.xml Tests/Unit",
"ci:ts:lint": "typoscript-lint -c Configuration/TsLint.yml --ansi -n --fail-on-warnings -vvv Configuration/TypoScript",
"ci:xliff:lint": "php Build/bin/console lint:xliff Resources/Private/Language"
},
"extra": {
"typo3/cms": {
Expand Down
19 changes: 19 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
parameters:
parallel:
# Don't be overly greedy on machines with more CPU's to be a good neighbor especially on CI
maximumNumberOfProcesses: 5

level: 3

bootstrapFiles:
- .Build/vendor/autoload.php

paths:
- Classes
- Configuration
- Tests

scanDirectories:
- Classes
- Configuration
- Tests

0 comments on commit e1470c7

Please sign in to comment.