Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions src/DTO/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ final class Blog
* @param array<string> $images
*/
public function __construct(
public readonly ?int $id = null,
public readonly ?string $guid = null,
public readonly string $id,
public readonly bool $isVisible,
public readonly LocalizedString $name,
public readonly LocalizedString $url,
public readonly ?string $category = null,
public readonly bool $isVisible = true,
public readonly ?LocalizedString $name = null,
public readonly ?LocalizedString $url = null,
public readonly ?LocalizedString $description = null,
public readonly ?LocalizedString $seoTitle = null,
public readonly ?LocalizedString $seoDescription = null,
Expand All @@ -33,25 +32,16 @@ public function __construct(
public function toArray(): array
{
$data = [
'id' => $this->id,
'is_visible' => $this->isVisible,
'name' => $this->name->toArray(),
'url' => $this->url->toArray(),
];

if ($this->guid !== null) {
$data['guid'] = $this->guid;
}

if ($this->category !== null) {
$data['category'] = $this->category;
}

if ($this->name !== null) {
$data['name'] = $this->name->toArray();
}

if ($this->url !== null) {
$data['url'] = $this->url->toArray();
}

if ($this->description !== null) {
$data['description'] = $this->description->toArray();
}
Expand All @@ -77,12 +67,11 @@ public function toArray(): array
public static function fromArray(array $data): self
{
return new self(
id: $data['id'] ?? null,
guid: $data['guid'] ?? null,
id: $data['id'],
isVisible: $data['is_visible'],
name: LocalizedString::fromArray($data['name']),
url: LocalizedString::fromArray($data['url']),
category: $data['category'] ?? null,
isVisible: $data['is_visible'] ?? true,
name: isset($data['name']) ? LocalizedString::fromArray($data['name']) : null,
url: isset($data['url']) ? LocalizedString::fromArray($data['url']) : null,
description: isset($data['description']) ? LocalizedString::fromArray($data['description']) : null,
seoTitle: isset($data['seo_title']) ? LocalizedString::fromArray($data['seo_title']) : null,
seoDescription: isset($data['seo_description']) ? LocalizedString::fromArray($data['seo_description']) : null,
Expand Down
40 changes: 16 additions & 24 deletions tests/BlogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ final class BlogTest extends TestCase
public function testCreateBlogWithAllFields(): void
{
$blog = new Blog(
id: 123,
guid: '550e8400-e29b-41d4-a716-446655440000',
category: 'news',
id: 'BLOG-123',
isVisible: true,
name: LocalizedString::create('Blog Title')
->withTranslation(Language::CS, 'Název blogu'),
url: LocalizedString::create('https://example.com/blog')
->withTranslation(Language::CS, 'https://example.com/cs/blog'),
category: 'news',
description: LocalizedString::create('<p>Description</p>'),
seoTitle: LocalizedString::create('SEO Title'),
seoDescription: LocalizedString::create('SEO Description'),
images: ['https://example.com/image.jpg'],
);

$this->assertSame(123, $blog->id);
$this->assertSame('550e8400-e29b-41d4-a716-446655440000', $blog->guid);
$this->assertSame('BLOG-123', $blog->id);
$this->assertSame('news', $blog->category);
$this->assertTrue($blog->isVisible);
$this->assertSame('Blog Title', $blog->name->getDefault());
Expand All @@ -40,18 +38,18 @@ public function testCreateBlogWithAllFields(): void
public function testBlogToArray(): void
{
$blog = new Blog(
guid: '550e8400-e29b-41d4-a716-446655440000',
category: 'tips',
id: 'BLOG-001',
isVisible: true,
name: LocalizedString::create('Blog Title'),
url: LocalizedString::create('https://example.com/blog'),
category: 'tips',
description: LocalizedString::create('<p>Description</p>'),
images: ['https://example.com/image.jpg'],
);

$array = $blog->toArray();

$this->assertSame('550e8400-e29b-41d4-a716-446655440000', $array['guid']);
$this->assertSame('BLOG-001', $array['id']);
$this->assertSame('tips', $array['category']);
$this->assertTrue($array['is_visible']);
$this->assertArrayHasKey('name', $array);
Expand All @@ -63,14 +61,17 @@ public function testBlogToArray(): void
public function testBlogToArrayExcludesNullFields(): void
{
$blog = new Blog(
id: 'BLOG-001',
isVisible: true,
name: LocalizedString::create('Blog Title'),
url: LocalizedString::create('https://example.com/blog'),
);

$array = $blog->toArray();

$this->assertArrayNotHasKey('guid', $array);
$this->assertArrayHasKey('id', $array);
$this->assertArrayHasKey('name', $array);
$this->assertArrayHasKey('url', $array);
$this->assertArrayNotHasKey('category', $array);
$this->assertArrayNotHasKey('description', $array);
$this->assertArrayNotHasKey('seo_title', $array);
Expand All @@ -81,8 +82,7 @@ public function testBlogToArrayExcludesNullFields(): void
public function testBlogFromArray(): void
{
$data = [
'id' => 456,
'guid' => '550e8400-e29b-41d4-a716-446655440001',
'id' => 'BLOG-456',
'category' => 'news',
'is_visible' => true,
'name' => ['default' => 'Blog Title', 'cs' => 'Název blogu'],
Expand All @@ -98,8 +98,7 @@ public function testBlogFromArray(): void

$blog = Blog::fromArray($data);

$this->assertSame(456, $blog->id);
$this->assertSame('550e8400-e29b-41d4-a716-446655440001', $blog->guid);
$this->assertSame('BLOG-456', $blog->id);
$this->assertSame('news', $blog->category);
$this->assertTrue($blog->isVisible);
$this->assertSame('Blog Title', $blog->name->getDefault());
Expand All @@ -112,7 +111,7 @@ public function testBlogFromArray(): void
public function testBlogFromArrayWithContent(): void
{
$data = [
'id' => 789,
'id' => 'BLOG-789',
'is_visible' => true,
'name' => ['default' => 'Blog'],
'url' => ['default' => 'https://example.com'],
Expand All @@ -138,17 +137,16 @@ public function testBlogFromArrayWithContent(): void
public function testBlogFromArrayWithMinimalData(): void
{
$data = [
'id' => 1,
'id' => 'BLOG-001',
'is_visible' => false,
'name' => ['default' => 'Minimal Blog'],
'url' => ['default' => 'https://example.com/minimal'],
];

$blog = Blog::fromArray($data);

$this->assertSame(1, $blog->id);
$this->assertSame('BLOG-001', $blog->id);
$this->assertFalse($blog->isVisible);
$this->assertNull($blog->guid);
$this->assertNull($blog->category);
$this->assertNull($blog->description);
$this->assertNull($blog->seoTitle);
Expand All @@ -157,13 +155,6 @@ public function testBlogFromArrayWithMinimalData(): void
$this->assertSame([], $blog->images);
}

public function testBlogDefaultIsVisible(): void
{
$blog = new Blog();

$this->assertTrue($blog->isVisible);
}

public function testBlogImagesArray(): void
{
$images = [
Expand All @@ -173,6 +164,7 @@ public function testBlogImagesArray(): void
];

$blog = new Blog(
id: 'BLOG-001',
isVisible: true,
name: LocalizedString::create('Blog'),
url: LocalizedString::create('https://example.com'),
Expand Down
8 changes: 4 additions & 4 deletions tests/PoboClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function testImportBlogsThrowsExceptionForTooManyItems(): void
$blogs = [];
for ($i = 0; $i < 101; $i++) {
$blogs[] = [
'guid' => sprintf('550e8400-e29b-41d4-a716-4466554400%02d', $i),
'id' => sprintf('BLOG-%03d', $i),
'is_visible' => true,
'name' => ['default' => sprintf('Blog %d', $i)],
'url' => ['default' => sprintf('https://example.com/blog/%d', $i)],
Expand All @@ -233,16 +233,16 @@ public function testImportBlogsThrowsExceptionForTooManyItems(): void
public function testBlogDtoIsConvertedToArray(): void
{
$blog = new Blog(
guid: '550e8400-e29b-41d4-a716-446655440000',
category: 'news',
id: 'BLOG-001',
isVisible: true,
name: LocalizedString::create('Test Blog'),
url: LocalizedString::create('https://example.com/blog'),
category: 'news',
);

$array = $blog->toArray();

$this->assertSame('550e8400-e29b-41d4-a716-446655440000', $array['guid']);
$this->assertSame('BLOG-001', $array['id']);
$this->assertSame('news', $array['category']);
$this->assertTrue($array['is_visible']);
}
Expand Down