Skip to content

Commit e01b75d

Browse files
committed
Adding isExternalUrl() helper for Asset, with full Test coverage of AssetData
1 parent 85804e1 commit e01b75d

File tree

6 files changed

+104
-14
lines changed

6 files changed

+104
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.1 - WIP
4+
- Adding `isExternalUrl()` helper method for Asset
5+
- Full test coverage for Asset and Assets classes
6+
37
## 1.1.0 - 2026-01-25
48
- Moving from PestPHP to PHPUnit (version 12)
59
- Deprecate API factory methods, prefer dependency injection

phpunit.xml.dist

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.0/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
cacheDirectory=".phpunit.cache"
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.0/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
cacheDirectory=".phpunit.cache"
78
>
89
<testsuites>
910
<testsuite name="Feature">
1011
<directory suffix="ApiTest.php">./tests/Feature</directory>
1112
</testsuite>
1213
<testsuite name="Unit">
13-
<directory suffix="ApiTest.php">./tests/Unit</directory>
14+
<directory suffix="Test.php">./tests/Unit</directory>
1415
</testsuite>
1516
</testsuites>
1617
<source>

src/Data/Asset.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public function setExternalUrl(string $url): self
9191
return $this;
9292
}
9393

94+
public function isExternalUrl(): bool
95+
{
96+
return $this->getBoolean("is_external_url", false);
97+
}
98+
9499
public static function emptyAsset(): Asset
95100
{
96101
return self::make([

tests/TestCase.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,28 @@ abstract class TestCase extends BaseTestCase
1313
* @param array<string, int|string> $headers
1414
*/
1515
protected function mockResponse(
16-
string $mockfile = 'test',
16+
string $mockfile = "test",
1717
int $statusCode = 200,
1818
array $headers = [],
1919
): MockResponse {
20-
$path = sprintf('./tests/Feature/Data/%s.json', $mockfile);
20+
$content = $this->mockData($mockfile);
21+
22+
return new MockResponse($content, [
23+
"http_code" => $statusCode,
24+
"response_headers" => $headers,
25+
]);
26+
}
27+
28+
protected function mockData(string $mockfile = "test"): string
29+
{
30+
$path = sprintf("./tests/Feature/Data/%s.json", $mockfile);
2131
$content = file_get_contents($path);
2232
if ($content === false) {
23-
throw new \RuntimeException(sprintf('Failed to read mock file: %s', $path));
33+
throw new \RuntimeException(
34+
sprintf("Failed to read mock file: %s", $path),
35+
);
2436
}
2537

26-
return new MockResponse($content, [
27-
'http_code' => $statusCode,
28-
'response_headers' => $headers,
29-
]);
38+
return $content;
3039
}
3140
}

tests/Unit/AssetDataTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature;
6+
7+
use Exception;
8+
use Storyblok\ManagementApi\Data\Asset;
9+
use Storyblok\ManagementApi\Data\Assets;
10+
use Storyblok\ManagementApi\Exceptions\StoryblokFormatException;
11+
use Tests\TestCase;
12+
13+
final class AssetDataTest extends TestCase
14+
{
15+
public function testAssetData(): void
16+
{
17+
$contentString = $this->mockData("one-asset");
18+
$content = json_decode($contentString, true);
19+
$this->assertIsArray($content);
20+
21+
$asset = Asset::make($content);
22+
23+
$this->assertSame(
24+
"https://s3.amazonaws.com/a.storyblok.com/f/222/3799x6005/3af265ee08/mypic.jpg",
25+
$asset->filename(),
26+
);
27+
$this->assertFalse($asset->isExternalUrl());
28+
$asset->setExternalUrl("https://storyblok.com/some.jpg");
29+
$this->assertSame("https://storyblok.com/some.jpg", $asset->filename());
30+
$this->assertTrue($asset->isExternalUrl());
31+
}
32+
33+
public function testNotValidAssetData(): void
34+
{
35+
$this->expectException(StoryblokFormatException::class);
36+
$this->expectExceptionMessage("Asset is not valid");
37+
Asset::make([]);
38+
}
39+
40+
public function testEmptyAssetData(): void
41+
{
42+
$asset = Asset::emptyAsset();
43+
$this->assertSame("", $asset->filename());
44+
}
45+
}

tests/Unit/AssetsDataTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature;
6+
7+
use Exception;
8+
use Storyblok\ManagementApi\Data\Assets;
9+
use Tests\TestCase;
10+
11+
final class AssetsDataTest extends TestCase
12+
{
13+
public function testAssetsData(): void
14+
{
15+
$contentString = $this->mockData("list-assets");
16+
$content = json_decode($contentString, true);
17+
// chewck if is array
18+
$this->assertIsArray($content);
19+
$this->assertArrayHasKey("assets", $content);
20+
$assets = Assets::make($content["assets"]);
21+
22+
$this->assertCount(2, $assets);
23+
$assets = Assets::makeFromResponse($content);
24+
$this->assertCount(2, $assets);
25+
}
26+
}

0 commit comments

Comments
 (0)