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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Release](https://img.shields.io/github/v/release/sni10/keepa-api-data-extractor?style=for-the-badge&logo=github&logoColor=white)](https://github.com/sni10/keepa-api-data-extractor/releases)
[![Release Workflow](https://img.shields.io/github/actions/workflow/status/sni10/keepa-api-data-extractor/release.yml?style=for-the-badge&logo=githubactions&logoColor=white&label=Release)](https://github.com/sni10/keepa-api-data-extractor/actions/workflows/release.yml)
[![Tests](https://img.shields.io/github/actions/workflow/status/sni10/keepa-api-data-extractor/tests.yml?style=for-the-badge&logo=githubactions&logoColor=white&label=Tests)](https://github.com/sni10/keepa-api-data-extractor/actions/workflows/tests.yml)
[![Coverage](https://img.shields.io/badge/Coverage-TBD-yellow?style=for-the-badge&logo=codecov&logoColor=white)](https://github.com/sni10/keepa-api-data-extractor/actions/workflows/tests.yml)
[![Coverage](https://img.shields.io/badge/Coverage-89%25-yellow?style=for-the-badge&logo=codecov&logoColor=white)](https://github.com/sni10/keepa-api-data-extractor/actions/workflows/tests.yml)
[![PHP](https://img.shields.io/badge/PHP-8.3%2B-777BB4?style=for-the-badge&logo=php&logoColor=white)](https://www.php.net/)
[![Symfony](https://img.shields.io/badge/Symfony-7.2-000000?style=for-the-badge&logo=Symfony&logoColor=white)](https://symfony.com/)
[![Docker](https://img.shields.io/badge/Docker-Ready-2496ED?style=for-the-badge&logo=docker&logoColor=white)](https://www.docker.com/)
Expand Down
98 changes: 98 additions & 0 deletions tests/Unit/Dto/KeepaInputDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,102 @@ public function testGetBrandReturnsNormalizedBrand(): void

self::assertSame('Nike', $dto->getBrand());
}

public function testFromArrayWithNullVersion(): void
{
$dto = KeepaInputDto::fromArray([
'id' => 5,
'domain_id' => 2,
'brand' => 'Puma',
'time_from' => '2025-03-01',
'time_to' => '2025-03-31',
'status' => 'PENDING',
'step' => 0,
]);

self::assertNull($dto->version);
self::assertSame(0, $dto->step);
}

public function testFromArrayWithDifferentStatuses(): void
{
$statuses = ['PENDING', 'IN_PROGRESS', 'COMPLETED', 'FINISHED', 'FAILED', 'CANCELLED'];

foreach ($statuses as $status) {
$dto = KeepaInputDto::fromArray([
'id' => 1,
'domain_id' => 1,
'brand' => 'Test',
'time_from' => '2025-01-01',
'time_to' => '2025-01-31',
'status' => $status,
'step' => 0,
]);

self::assertSame($status, $dto->status);
}
}

public function testFromArrayHandlesEmptyBrand(): void
{
$dto = KeepaInputDto::fromArray([
'id' => 1,
'domain_id' => 1,
'brand' => '',
'time_from' => '2025-01-01',
'time_to' => '2025-01-31',
'status' => 'PENDING',
'step' => 0,
]);

self::assertSame('', $dto->brand);
}

public function testFromArrayCastsStepToZeroWhenMissing(): void
{
$dto = KeepaInputDto::fromArray([
'id' => 1,
'domain_id' => 1,
'brand' => 'Test',
'time_from' => '2025-01-01',
'time_to' => '2025-01-31',
'status' => 'PENDING',
]);

self::assertSame(0, $dto->step);
}

public function testFromArrayTrimsTimeFields(): void
{
$dto = KeepaInputDto::fromArray([
'id' => 1,
'domain_id' => 1,
'brand' => 'Test',
'time_from' => ' 2025-01-01 ',
'time_to' => ' 2025-01-31 ',
'status' => 'PENDING',
'step' => 0,
]);

self::assertSame('2025-01-01', $dto->time_from);
self::assertSame('2025-01-31', $dto->time_to);
}

public function testToArrayIncludesNullVersion(): void
{
$dto = new KeepaInputDto();
$dto->id = 1;
$dto->domain_id = 1;
$dto->brand = 'Test';
$dto->time_from = '2025-01-01';
$dto->time_to = '2025-01-31';
$dto->version = null;
$dto->status = 'PENDING';
$dto->step = 0;

$array = $dto->toArray();

self::assertArrayHasKey('version', $array);
self::assertNull($array['version']);
}
}
154 changes: 154 additions & 0 deletions tests/Unit/Exception/ExceptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace App\Tests\Unit\Exception;

use App\Exception\KeepaRequestFailedException;
use App\Exception\KeepaTokenTimeoutException;
use App\Exception\NotEnoughTokenError;
use PHPUnit\Framework\TestCase;

class ExceptionsTest extends TestCase
{
public function testKeepaRequestFailedExceptionCanBeCreated(): void
{
$exception = new KeepaRequestFailedException('Request failed');

self::assertInstanceOf(\Exception::class, $exception);
self::assertInstanceOf(KeepaRequestFailedException::class, $exception);
self::assertSame('Request failed', $exception->getMessage());
self::assertSame(0, $exception->getCode());
}

public function testKeepaRequestFailedExceptionWithCodeAndPrevious(): void
{
$previous = new \RuntimeException('Previous error');
$exception = new KeepaRequestFailedException('Request failed', 500, $previous);

self::assertSame('Request failed', $exception->getMessage());
self::assertSame(500, $exception->getCode());
self::assertSame($previous, $exception->getPrevious());
}

public function testKeepaRequestFailedExceptionCanBeThrown(): void
{
$this->expectException(KeepaRequestFailedException::class);
$this->expectExceptionMessage('API error occurred');

throw new KeepaRequestFailedException('API error occurred');
}

public function testNotEnoughTokenErrorCanBeCreated(): void
{
$exception = new NotEnoughTokenError('Not enough tokens available');

self::assertInstanceOf(\Exception::class, $exception);
self::assertInstanceOf(NotEnoughTokenError::class, $exception);
self::assertSame('Not enough tokens available', $exception->getMessage());
self::assertSame(0, $exception->getCode());
}

public function testNotEnoughTokenErrorWithCodeAndPrevious(): void
{
$previous = new \RuntimeException('Token limit reached');
$exception = new NotEnoughTokenError('Not enough tokens', 429, $previous);

self::assertSame('Not enough tokens', $exception->getMessage());
self::assertSame(429, $exception->getCode());
self::assertSame($previous, $exception->getPrevious());
}

public function testNotEnoughTokenErrorCanBeThrown(): void
{
$this->expectException(NotEnoughTokenError::class);
$this->expectExceptionMessage('Tokens depleted');

throw new NotEnoughTokenError('Tokens depleted');
}

public function testKeepaTokenTimeoutExceptionCanBeCreated(): void
{
$exception = new KeepaTokenTimeoutException('Token timeout');

self::assertInstanceOf(\Exception::class, $exception);
self::assertInstanceOf(KeepaTokenTimeoutException::class, $exception);
self::assertSame('Token timeout', $exception->getMessage());
self::assertSame(0, $exception->getCode());
}

public function testKeepaTokenTimeoutExceptionWithCodeAndPrevious(): void
{
$previous = new \RuntimeException('Timeout occurred');
$exception = new KeepaTokenTimeoutException('Token wait timeout', 408, $previous);

self::assertSame('Token wait timeout', $exception->getMessage());
self::assertSame(408, $exception->getCode());
self::assertSame($previous, $exception->getPrevious());
}

public function testKeepaTokenTimeoutExceptionCanBeThrown(): void
{
$this->expectException(KeepaTokenTimeoutException::class);
$this->expectExceptionMessage('Waited too long for tokens');

throw new KeepaTokenTimeoutException('Waited too long for tokens');
}

public function testExceptionsAreDistinct(): void
{
$exception1 = new KeepaRequestFailedException('Error 1');
$exception2 = new NotEnoughTokenError('Error 2');
$exception3 = new KeepaTokenTimeoutException('Error 3');

self::assertNotInstanceOf(NotEnoughTokenError::class, $exception1);
self::assertNotInstanceOf(KeepaTokenTimeoutException::class, $exception1);

self::assertNotInstanceOf(KeepaRequestFailedException::class, $exception2);
self::assertNotInstanceOf(KeepaTokenTimeoutException::class, $exception2);

self::assertNotInstanceOf(KeepaRequestFailedException::class, $exception3);
self::assertNotInstanceOf(NotEnoughTokenError::class, $exception3);
}

public function testExceptionMessagesCanBeEmpty(): void
{
$exception1 = new KeepaRequestFailedException('');
$exception2 = new NotEnoughTokenError('');
$exception3 = new KeepaTokenTimeoutException('');

self::assertSame('', $exception1->getMessage());
self::assertSame('', $exception2->getMessage());
self::assertSame('', $exception3->getMessage());
}

public function testExceptionsCanHaveCustomCodes(): void
{
$exception1 = new KeepaRequestFailedException('Request failed', 1001);
$exception2 = new NotEnoughTokenError('No tokens', 1002);
$exception3 = new KeepaTokenTimeoutException('Timeout', 1003);

self::assertSame(1001, $exception1->getCode());
self::assertSame(1002, $exception2->getCode());
self::assertSame(1003, $exception3->getCode());
}

public function testExceptionsCatchAsGenericException(): void
{
try {
throw new KeepaRequestFailedException('Test');
} catch (\Exception $e) {
self::assertInstanceOf(KeepaRequestFailedException::class, $e);
}

try {
throw new NotEnoughTokenError('Test');
} catch (\Exception $e) {
self::assertInstanceOf(NotEnoughTokenError::class, $e);
}

try {
throw new KeepaTokenTimeoutException('Test');
} catch (\Exception $e) {
self::assertInstanceOf(KeepaTokenTimeoutException::class, $e);
}
}
}
Loading