Skip to content

Commit

Permalink
Added 30% of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedmaazin committed Mar 9, 2023
1 parent 5ad4d92 commit 2d5692e
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 20 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="ReplicateUnitTests">
<testsuite name="ReplicatePHPTestSuite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
Expand Down
12 changes: 8 additions & 4 deletions src/Validators/ReplicateWebhookValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function validate(array $input): array
{
$filteredWebhookConfig = $this->validateWebhookConfig($input);

$this->validateWebhookUrl($filteredWebhookConfig['webhook']);
$this->validateWebhookEventFilters($filteredWebhookConfig['webhook_events_filter']);
$this->validateWebhookUrl($filteredWebhookConfig['webhook'] ?? []);
$this->validateWebhookEventFilters($filteredWebhookConfig['webhook_events_filter'] ?? null);

return [
'webhook' => $filteredWebhookConfig['webhook'],
Expand Down Expand Up @@ -74,13 +74,17 @@ private function validateWebhookUrl(string $url): void
/**
* Validates webhook event filters.
*
* @param array $filters
* @param array|null $filters
*
* @return void
* @throws ReplicateWebhookInputException
*/
private function validateWebhookEventFilters(array $filters): void
private function validateWebhookEventFilters(?array $filters): void
{
if (null === $filters) {
throw new ReplicateWebhookInputException('Empty webhook event filter provided');
}

$allowedWebhookEventFilters = ['start', 'output', 'logs', 'completed'];
$disallowedWebhookEventFilters = array_diff($filters, $allowedWebhookEventFilters);

Expand Down
15 changes: 0 additions & 15 deletions tests/BaseTest.php

This file was deleted.

56 changes: 56 additions & 0 deletions tests/Commands/GetPredictionCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Mazin\Replicate\Tests\Commands;

use DateTimeImmutable;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mazin\Replicate\Commands\GetPredictionCommand;
use Mazin\Replicate\Tests\Fixtures\PredictionDataFixture;
use PHPUnit\Framework\TestCase;

class GetPredictionCommandTest extends TestCase
{
public function testGetPredictionCommand(): void
{
$responseBody = json_encode(PredictionDataFixture::get(), JSON_THROW_ON_ERROR);

$mockResponse = new Response(
200,
['Content-Type' => 'application/json'],
$responseBody
);

$mockClient = $this->createMock(Client::class);
$mockClient->expects($this->once())
->method('get')
->with('predictions/1')
->willReturn($mockResponse);

$command = new GetPredictionCommand($mockClient);
$result = $command->handle('1');

$this->assertSame('1', $result->id);
$this->assertSame('v1', $result->version);
$this->assertSame([
'get' => 'https://api.replicate.com/v1/predictions/foo',
'cancel' => 'https://api.replicate.com/v1/predictions/foo/cancel',
], $result->urls);
$this->assertSame('succeeded', $result->status);
$this->assertSame([
'prompt' => 'foo',
], $result->input);
$this->assertEquals(DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2022-01-01T01:23:45.678900Z'), $result->created_at);
$this->assertEquals(DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2022-01-01T01:23:46.678900Z'), $result->started_at);
$this->assertEquals(DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', '2022-01-01T01:23:47.678900Z'), $result->completed_at);
$this->assertSame('api', $result->source);
$this->assertSame([
"https://replicate.delivery/pbxt/foo/out-0.png"
], $result->output);
$this->assertSame("Using seed", $result->logs);
$this->assertSame([
'predict_time' => 0.95,
], $result->metrics);
$this->assertNull($result->error);
}
}
34 changes: 34 additions & 0 deletions tests/Fixtures/PredictionDataFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Mazin\Replicate\Tests\Fixtures;

final class PredictionDataFixture
{
public static function get(): array
{
return [
'id' => '1',
'version' => 'v1',
'urls' => [
'get' => 'https://api.replicate.com/v1/predictions/foo',
'cancel' => 'https://api.replicate.com/v1/predictions/foo/cancel',
],
'status' => 'succeeded',
'input' => [
'prompt' => 'foo',
],
'created_at' => '2022-01-01T01:23:45.678900Z',
'started_at' => '2022-01-01T01:23:46.678900Z',
'completed_at' => '2022-01-01T01:23:47.678900Z',
'source' => 'api',
'output' => [
"https://replicate.delivery/pbxt/foo/out-0.png"
],
'logs' => "Using seed",
'metrics' => [
'predict_time' => 0.95,
],
'error' => null,
];
}
}
64 changes: 64 additions & 0 deletions tests/Fixtures/PredictionsDataFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Mazin\Replicate\Tests\Fixtures;

final class PredictionsDataFixture
{
public static function get(): array
{
return [
'previous' => 'https://api.replicate.com/v1/predictions',
'next' => 'https://api.replicate.com/v1/predictions?cursor=foo',
'results' => [
[
'id' => '1',
'version' => 'v1',
'urls' => [
'get' => 'https://api.replicate.com/v1/predictions/foo',
'cancel' => 'https://api.replicate.com/v1/predictions/foo/cancel',
],
'status' => 'succeeded',
'input' => [
'prompt' => 'foo',
],
'created_at' => '2022-01-01T01:23:45.678900Z',
'started_at' => '2022-01-01T01:23:46.678900Z',
'completed_at' => '2022-01-01T01:23:47.678900Z',
'source' => 'api',
'output' => [
"https://replicate.delivery/pbxt/foo/out-0.png"
],
'logs' => "Using seed",
'metrics' => [
'predict_time' => 0.95,
],
'error' => null,
],
[
'id' => '2',
'version' => 'v1',
'urls' => [
'get' => 'https://api.replicate.com/v1/predictions/foo',
'cancel' => 'https://api.replicate.com/v1/predictions/foo/cancel',
],
'status' => 'succeeded',
'input' => [
'prompt' => 'foo',
],
'created_at' => '2022-01-01T01:23:45.678900Z',
'started_at' => '2022-01-01T01:23:46.678900Z',
'completed_at' => '2022-01-01T01:23:47.678900Z',
'source' => 'api',
'output' => [
"https://replicate.delivery/pbxt/foo/out-0.png"
],
'logs' => "Using seed",
'metrics' => [
'predict_time' => 0.95,
],
'error' => null,
],
],
];
}
}
41 changes: 41 additions & 0 deletions tests/Mappers/PredictionMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Mazin\Replicate\Tests\Mappers;

use DateTimeImmutable;
use Mazin\Replicate\Mappers\PredictionMapper;
use Mazin\Replicate\Tests\Fixtures\PredictionDataFixture;
use PHPUnit\Framework\TestCase;

class PredictionMapperTest extends TestCase
{
private PredictionMapper $predictionMapper;

protected function setUp(): void
{
$this->predictionMapper = new PredictionMapper();
}

public function testMap(): void
{
$data = PredictionDataFixture::get();

$prediction = $this->predictionMapper->map($data);

$this->assertSame('1', $prediction->id);
$this->assertSame('v1', $prediction->version);
$this->assertSame($data['urls'], $prediction->urls);
$this->assertSame('succeeded', $prediction->status);
$this->assertSame($data['input'], $prediction->input);
$this->assertEquals(DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', $data['created_at']), $prediction->created_at);
$this->assertEquals(DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', $data['started_at']), $prediction->started_at);
$this->assertEquals(DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.u\Z', $data['completed_at']), $prediction->completed_at);
$this->assertSame($data['source'], $prediction->source);
$this->assertSame($data['output'], $prediction->output);
$this->assertSame($data['logs'], $prediction->logs);
$this->assertSame($data['metrics'], $prediction->metrics);
$this->assertSame($data['error'], $prediction->error);
}
}
26 changes: 26 additions & 0 deletions tests/Model/PredictionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Mazin\Replicate\Tests\Model;

use Mazin\Replicate\Model\Prediction;
use Mazin\Replicate\Model\Predictions;
use Mazin\Replicate\Tests\Fixtures\PredictionsDataFixture;
use PHPUnit\Framework\TestCase;

class PredictionsTest extends TestCase
{
public function testFromApiResponse(): void
{
$response = PredictionsDataFixture::get();

$predictions = Predictions::fromApiResponse($response);

$this->assertEquals('https://api.replicate.com/v1/predictions', $predictions->previous);
$this->assertEquals('https://api.replicate.com/v1/predictions?cursor=foo', $predictions->next);
$this->assertCount(2, $predictions->results);
$this->assertInstanceOf(Prediction::class, $predictions->results[0]);
$this->assertInstanceOf(Prediction::class, $predictions->results[1]);
$this->assertEquals('1', $predictions->results[0]->id);
$this->assertEquals('2', $predictions->results[1]->id);
}
}
76 changes: 76 additions & 0 deletions tests/Validators/ReplicateWebhookValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Mazin\Replicate\Tests\Validators;

use Mazin\Replicate\Exceptions\ReplicateWebhookInputException;
use Mazin\Replicate\Validators\ReplicateWebhookValidator;
use PHPUnit\Framework\TestCase;

class ReplicateWebhookValidatorTest extends TestCase
{
public function test_validate(): void
{
$validator = new ReplicateWebhookValidator();

// Test valid input
$input = [
'webhook' => 'https://example.com',
'webhook_events_filter' => ['start', 'output', 'logs', 'completed']
];
$expectedResult = [
'webhook' => 'https://example.com',
'webhook_events_filter' => ['start', 'output', 'logs', 'completed']
];
$this->assertSame($expectedResult, $validator->validate($input));
}

public function test_validate_with_missing_key(): void
{
$validator = new ReplicateWebhookValidator();

$input = [
'webhook' => 'https://example.com',
];
$this->expectException(ReplicateWebhookInputException::class);
$validator->validate($input);
}

public function test_validate_with_extra_key(): void
{
$validator = new ReplicateWebhookValidator();

$input = [
'webhook' => 'https://example.com',
'webhook_events_filter' => ['start', 'output', 'logs', 'completed'],
'extra_key' => 'extra_value'
];
$this->expectException(ReplicateWebhookInputException::class);
$validator->validate($input);
}

public function test_validate_with_invalid_webhook_url(): void
{
$validator = new ReplicateWebhookValidator();

$input = [
'webhook' => 'invalid_url',
'webhook_events_filter' => ['start', 'output', 'logs', 'completed']
];
$this->expectException(ReplicateWebhookInputException::class);
$validator->validate($input);
}

public function test_validate_with_invalid_webhook_event_filter(): void
{
$validator = new ReplicateWebhookValidator();

$input = [
'webhook' => 'https://example.com',
'webhook_events_filter' => ['start', 'output', 'invalid_filter', 'completed']
];
$this->expectException(ReplicateWebhookInputException::class);
$validator->validate($input);
}
}

0 comments on commit 2d5692e

Please sign in to comment.