Skip to content

Commit

Permalink
Unifx linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed May 2, 2024
1 parent dd4cecd commit 9d8fb11
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 97 deletions.
27 changes: 12 additions & 15 deletions src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public static function get_shortcodes()
* Replace"[image id=n]" shortcode with an image reference.
* Permission checks will be enforced by the file routing itself.
*
* @param array $args Arguments passed to the parser
* @param string $content Raw shortcode
* @param ShortcodeParser $parser Parser
* @param string $shortcode Name of shortcode used to register this handler
* @param array $extra Extra arguments
* @param array $args Arguments passed to the parser
* @param string $content Raw shortcode
* @param ShortcodeParser $parser Parser
* @param string $shortcode Name of shortcode used to register this handler
* @param array $extra Extra arguments
* @return string Result of the handled shortcode
*/
public static function handle_shortcode($args, $content, $parser, $shortcode, $extra = [])
Expand Down Expand Up @@ -138,14 +138,11 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e

// cache it for future reference
if ($fileFound) {
$cache->set(
$cacheKey,
[
$cache->set($cacheKey, [
'markup' => $markup,
'filename' => $record instanceof File ? $record->getFilename() : null,
'hash' => $record instanceof File ? $record->getHash() : null,
]
);
]);
}

return $markup;
Expand Down Expand Up @@ -176,11 +173,11 @@ public static function createImageTag(array $attributes) : string
/**
* Regenerates "[image id=n]" shortcode with new src attribute prior to being edited within the CMS.
*
* @param array $args Arguments passed to the parser
* @param string $content Raw shortcode
* @param ShortcodeParser $parser Parser
* @param string $shortcode Name of shortcode used to register this handler
* @param array $extra Extra arguments
* @param array $args Arguments passed to the parser
* @param string $content Raw shortcode
* @param ShortcodeParser $parser Parser
* @param string $shortcode Name of shortcode used to register this handler
* @param array $extra Extra arguments
* @return string Result of the handled shortcode
*/
public static function regenerate_shortcode($args, $content, $parser, $shortcode, $extra = [])
Expand Down
129 changes: 47 additions & 82 deletions tests/php/ImageManipulationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ protected function setUp(): void
TestAssetStore::activate('ImageTest');

// Copy test images for each of the fixture references
/**
* @var File $image
*/
/** @var File $image */
$files = File::get()->exclude('ClassName', Folder::class);
foreach ($files as $image) {
$sourcePath = __DIR__ . '/ImageTest/' . $image->Name;
Expand All @@ -49,14 +47,11 @@ protected function setUp(): void
}

// Set default config
InterventionBackend::config()->set(
'error_cache_ttl',
[
InterventionBackend::config()->set('error_cache_ttl', [
InterventionBackend::FAILED_INVALID => 0,
InterventionBackend::FAILED_MISSING => '5,10',
InterventionBackend::FAILED_UNKNOWN => 300,
]
);
]);
}

protected function tearDown(): void
Expand All @@ -67,19 +62,15 @@ protected function tearDown(): void

public function testAttribute()
{
/**
* @var Image $origin
*/
/** @var Image $origin */
$origin = $this->objFromFixture(Image::class, 'imageWithTitle');

$this->assertEmpty(
$origin->getAttribute('foo'),
'Undefined attributes are falsy'
);

/**
* @var DBFile $image
*/
/** @var DBFile $image */
$image = $origin->setAttribute('foo', 'bar');
$this->assertEquals(
'bar',
Expand Down Expand Up @@ -121,9 +112,7 @@ public function testAttribute()

public function testInitAttributes()
{
/**
* @var Image $origin
*/
/** @var Image $origin */
$origin = $this->objFromFixture(Image::class, 'imageWithTitle');

$origin->initAttributes(['foo' => 'bar']);
Expand All @@ -133,9 +122,7 @@ public function testInitAttributes()
'Attributes hawve been set'
);

/**
* @var DBFile $dbfile
*/
/** @var DBFile $dbfile */
$dbfile = $origin->setAttribute('hello', 'bonjour');
$dbfile->initAttributes(['bar' => 'foo']);
$this->assertEquals(
Expand All @@ -151,9 +138,7 @@ public function testInitAttributes()

public function testGetAttributesForImage()
{
/**
* @var Image $origin
*/
/** @var Image $origin */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');

$this->assertEquals(
Expand Down Expand Up @@ -220,37 +205,33 @@ public function testGetAttributesForImage()

public function testGetAttributesWhenLazyLoadIsDisabled()
{
Config::withConfig(
function () {
Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
/**
* @var Image $origin
*/
$image = $this->objFromFixture(Image::class, 'imageWithTitle');

$this->assertEquals(
[
'width' => $image->getWidth(),
'height' => $image->getHeight(),
'alt' => $image->getTitle(),
'src' => $image->getURL(),
],
$image->getAttributes(),
'No loading attribute when lazy loading is disabled globally'
);
Config::withConfig(function () {
Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
/** @var Image $origin */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');

$this->assertEquals(
[
'width' => $image->getWidth(),
'height' => $image->getHeight(),
'alt' => $image->getTitle(),
'src' => $image->getURL(),
],
$image->getAttributes(),
'No loading attribute when lazy loading is disabled globally'
);

$this->assertEquals(
[
$this->assertEquals(
[
'width' => $image->getWidth(),
'height' => $image->getHeight(),
'alt' => $image->getTitle(),
'src' => $image->getURL(),
],
$image->LazyLoad(true)->getAttributes(),
'No loading attribute when lazy loading is disabled globally even if requested'
);
}
);
],
$image->LazyLoad(true)->getAttributes(),
'No loading attribute when lazy loading is disabled globally even if requested'
);
});
}

public function testGetAttributesForPlainFile()
Expand All @@ -276,15 +257,13 @@ public function testIsLazyLoaded()
'Disabling LazyLoad turns off lazy load'
);

Config::withConfig(
function () use ($image) {
Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
$this->assertFalse(
$image->IsLazyLoaded(),
'Disabling LazyLoad globally turns off lazy load'
);
}
);
Config::withConfig(function () use ($image) {
Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
$this->assertFalse(
$image->IsLazyLoaded(),
'Disabling LazyLoad globally turns off lazy load'
);
});

$file = File::create();
$file->setFromString('boom', 'boom.txt');
Expand All @@ -296,9 +275,7 @@ function () use ($image) {

public function testAttributesHTML()
{
/**
* @var Image $origin
*/
/** @var Image $origin*/
$image = $this->objFromFixture(Image::class, 'imageWithTitle');
$this->assertEquals(
'width="300" height="300" alt="This is a image Title" src="/assets/ImageTest/folder/test-image.png" loading="lazy"',
Expand All @@ -325,15 +302,13 @@ public function lazyLoadProvider()
}

/**
* @param $val
* @param $expected
* @param $val
* @param $expected
* @dataProvider lazyLoadProvider
*/
public function testLazyLoad($val, bool $expected)
{
/**
* @var Image $origin
*/
/** @var Image $origin */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');

$newImg = $image->LazyLoad($val);
Expand Down Expand Up @@ -362,14 +337,12 @@ public function lazyLoadBadProvider()
}

/**
* @param $val
* @param $val
* @dataProvider lazyLoadBadProvider
*/
public function testBadLazyLoad($val)
{
/**
* @var Image $origin
*/
/** @var Image $origin */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');

$newImg = $image->LazyLoad($val);
Expand Down Expand Up @@ -397,9 +370,7 @@ public function testLazyLoadIsAccessibleInExtensions()
{
Image::add_extension(LazyLoadAccessorExtension::class);

/**
* @var Image $origin
*/
/** @var Image $origin */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');

$this->assertTrue(
Expand Down Expand Up @@ -474,9 +445,7 @@ public function renderProvider()
*/
public function testRender(string $template, string $expected)
{
/**
* @var Image $origin
*/
/** @var Image $origin */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');

$this->assertEquals(
Expand All @@ -501,9 +470,7 @@ public function testThumbnailURL()
'Thumbnail URL is correct'
);

/**
* @var AssetStore assetStore
*/
/** @var AssetStore assetStore */
$assetStore = Injector::inst()->get(AssetStore::class);
$this->assertFalse(
$assetStore->isGranted($fileUrl),
Expand Down Expand Up @@ -620,9 +587,7 @@ public function testConvert(string $originalFileFixtureClass, string $originalFi
{
// Make sure we have a known set of converters for testing
FileConverterManager::config()->set('converters', [TestTxtToImageConverter::class]);
/**
* @var File $file
*/
/** @var File $file */
$file = $this->objFromFixture($originalFileFixtureClass, $originalFileFixture);

// Set up mock logger so we can check the exception gets logged
Expand Down

0 comments on commit 9d8fb11

Please sign in to comment.