Skip to content

Commit

Permalink
Implemented wait for
Browse files Browse the repository at this point in the history
  • Loading branch information
eerison committed Feb 5, 2021
1 parent 333c8fc commit ffa855a
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Enum/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
class Status
{
public const ASSEMBLY_COMPLETED = 'ASSEMBLY_COMPLETED';
public const ASSEMBLY_UPLOADING = 'ASSEMBLY_UPLOADING';
public const ASSEMBLY_EXECUTING = 'ASSEMBLY_EXECUTING';
}
4 changes: 2 additions & 2 deletions src/Factory/AssemblyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Transloadit\Factory;

use Transloadit\Model\Parameter;
use Transloadit\Model\Contracts\ParameterInterface;
use Transloadit\Model\Resource\Assembly;
use Transloadit\Model\Resource\Contracts\AssemblyInterface;

class AssemblyFactory
{
public static function create(Parameter $parameter = null): AssemblyInterface
public static function create(ParameterInterface $parameter = null): AssemblyInterface
{
return new Assembly($parameter);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Factory/AssemblyResourceServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Transloadit\Model\Contracts\AuthInterface;
use Transloadit\Service\AssemblyResourceService;
use Transloadit\Service\Contracts\AssemblyResourceServiceInterface;

class AssemblyResourceServiceFactory
{
public static function create(
AuthInterface $auth,
HttpClientInterface $client = null,
SerializerInterface $serializer = null
): AssemblyResourceService {
): AssemblyResourceServiceInterface {
$client = $client ?? TransloaditHttpClientFactory::create();
$serializer = $serializer ?? SerializerFactory::create();

Expand Down
24 changes: 24 additions & 0 deletions src/Factory/AssemblyWaitForResourceServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Transloadit\Factory;

use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Transloadit\Model\Contracts\AuthInterface;
use Transloadit\Service\AssemblyResourceService;
use Transloadit\Service\AssemblyWaitForResourceService;
use Transloadit\Service\Contracts\AssemblyResourceServiceInterface;

class AssemblyWaitForResourceServiceFactory
{
public static function create(
AuthInterface $auth,
HttpClientInterface $client = null,
SerializerInterface $serializer = null
): AssemblyResourceServiceInterface {
$client = $client ?? TransloaditHttpClientFactory::create();
$serializer = $serializer ?? SerializerFactory::create();

return new AssemblyWaitForResourceService($auth, $client, $serializer);
}
}
2 changes: 1 addition & 1 deletion src/Service/AssemblyResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Class AssemblyService
* @package Transloadit\Service
*/
final class AssemblyResourceService extends AbstractResourceService implements AssemblyResourceServiceInterface
class AssemblyResourceService extends AbstractResourceService implements AssemblyResourceServiceInterface
{
private const URL = 'assemblies';

Expand Down
22 changes: 22 additions & 0 deletions src/Service/AssemblyWaitForResourceService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Transloadit\Service;

use Transloadit\Enum\Status;
use Transloadit\Model\Resource\Contracts\AssemblyInterface;
use Transloadit\Model\Resource\Contracts\ResourceInterface;

class AssemblyWaitForResourceService extends AssemblyResourceService
{
public function create(AssemblyInterface $assembly): ResourceInterface
{
$assembly = parent::create($assembly);

while (in_array($assembly->getStatus(), [Status::ASSEMBLY_EXECUTING, Status::ASSEMBLY_UPLOADING])) {
sleep(1);
$assembly = $this->getById($assembly->getId());
}

return $assembly;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Transloadit\Factory\ParameterFactory;
use DateTime;

class AssemblyServiceTest extends TestCase
class AssemblyResourceServiceTest extends TestCase
{
private $auth;

Expand Down
70 changes: 70 additions & 0 deletions tests/Functional/Service/AssemblyWaitForResourceServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Transloadit\Tests\Functional\Service;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Transloadit\Enum\Status;
use Transloadit\Factory\AssemblyWaitForResourceServiceFactory;
use Transloadit\Model\Contracts\AuthInterface;
use Transloadit\Model\Resource\Contracts\AssemblyInterface;
use Transloadit\Service\AssemblyWaitForResourceService;

class AssemblyWaitForResourceServiceTest extends TestCase
{
/**
* @dataProvider provider
* @param string $firstAssemblyStatus
* @param string $secondAssemblyStatus
* @param $execution
*/
public function testCanCreateAnAssembly(string $firstAssemblyStatus, string $secondAssemblyStatus, $execution)
{
$auth = $this->createMock(AuthInterface::class);
$client = $this->createMock(HttpClientInterface::class);
$serializer = $this->createMock(SerializerInterface::class);

$assembly1 = $this->createMock(AssemblyInterface::class);
$assembly1
->method('getStatus')
->willReturn($firstAssemblyStatus);

$assembly2 = $this->createMock(AssemblyInterface::class);
$assembly2
->method('getStatus')
->willReturn($secondAssemblyStatus);

$assemblyService = $this
->getMockBuilder(AssemblyWaitForResourceService::class)
->setConstructorArgs([$auth, $client, $serializer])
->onlyMethods(['getById', 'requestResource', 'deserializeResource'])
->getMock()
;
$assemblyService
->expects($execution)
->method('getById')
->willReturn($assembly2)
;
$assemblyService
->method('deserializeResource')
->willReturn($assembly1);

$assemblyService->create($assembly1);
}

public function testFactory()
{
$auth = $this->createMock(AuthInterface::class);
$assemblyService = AssemblyWaitForResourceServiceFactory::create($auth);

$this->assertInstanceOf(AssemblyWaitForResourceService::class, $assemblyService);
}

public function provider()
{
yield [Status::ASSEMBLY_EXECUTING, Status::ASSEMBLY_COMPLETED, $this->once()];
yield [Status::ASSEMBLY_UPLOADING, Status::ASSEMBLY_COMPLETED, $this->once()];
yield [Status::ASSEMBLY_COMPLETED, Status::ASSEMBLY_COMPLETED, $this->never()];
}
}

0 comments on commit ffa855a

Please sign in to comment.