-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ad4d92
commit 2d5692e
Showing
9 changed files
with
306 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |