Skip to content

Commit

Permalink
Bring back data providers, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wiktor6 committed Oct 2, 2024
1 parent 72d1292 commit aeea620
Show file tree
Hide file tree
Showing 16 changed files with 725 additions and 624 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
backupGlobals="false"
beStrictAboutOutputDuringTests="true"
Expand Down
4 changes: 2 additions & 2 deletions src/LuigisBoxBundle/Exception/ApiErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ApiErrorException extends \RuntimeException
{
public function __construct(
string $message,
public readonly Request $request)
{
public readonly Request $request,
) {
parent::__construct($message);
}
}
4 changes: 2 additions & 2 deletions src/LuigisBoxBundle/Exception/MalformedResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function __construct(
string $message,
public readonly ResponseInterface $response,
public readonly Request $request,
?\Throwable $previous = null)
{
?\Throwable $previous = null,
) {
parent::__construct($message, 0, $previous);
}
}
3 changes: 3 additions & 0 deletions src/LuigisBoxBundle/ValueObject/ContentRemovalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

readonly class ContentRemovalCollection implements ObjectsInterface, \Countable
{
/**
* @param ContentRemoval[] $objects
*/
public function __construct(public array $objects)
{
Assert::allIsInstanceOf($objects, ContentRemoval::class);
Expand Down
8 changes: 0 additions & 8 deletions src/LuigisBoxBundle/ValueObject/SearchUrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ public function setFilters(array $filters): self

public function addMustFilter(string $key, bool|int|string $value): self
{
if (!is_bool($value)
&& !is_int($value)
&& !is_string($value)) {
throw new \InvalidArgumentException(
sprintf('Value must be bool|int|string, %s provided.', gettype($value))
);
}

$this->mustFilters[$key] = $this->mustFilters[$key] ?? [];
if (\is_string($this->mustFilters[$key])) {
$this->mustFilters[$key] = [$this->mustFilters[$key]];
Expand Down
156 changes: 6 additions & 150 deletions tests/Acceptance/Service/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@
use Answear\LuigisBoxBundle\Service\LuigisBoxSerializer;
use Answear\LuigisBoxBundle\Service\Request;
use Answear\LuigisBoxBundle\Service\RequestInterface;
use Answear\LuigisBoxBundle\Tests\DataProvider\RequestDataProvider;
use Answear\LuigisBoxBundle\Tests\ExampleConfiguration;
use Answear\LuigisBoxBundle\ValueObject\ContentAvailability;
use Answear\LuigisBoxBundle\ValueObject\ContentAvailabilityCollection;
use Answear\LuigisBoxBundle\ValueObject\ContentRemoval;
use Answear\LuigisBoxBundle\ValueObject\ContentRemovalCollection;
use Answear\LuigisBoxBundle\ValueObject\ContentUpdate;
use Answear\LuigisBoxBundle\ValueObject\ContentUpdateCollection;
use Answear\LuigisBoxBundle\ValueObject\PartialContentUpdate;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Uri;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

Expand All @@ -38,7 +35,7 @@ protected function setUp(): void
}

#[Test]
#[DataProvider('forContentUpdate')]
#[DataProviderExternal(RequestDataProvider::class, 'forContentUpdate')]
public function contentUpdateRequestPassed(
string $httpMethod,
ContentUpdateCollection $collection,
Expand All @@ -55,55 +52,8 @@ public function contentUpdateRequestPassed(
self::assertSame([], $response->errors);
}

public static function forContentUpdate(): iterable
{
yield [
'POST',
new ContentUpdateCollection([new ContentUpdate('title', 'product/1', 'products', [])]),
'{"objects":[{"url":"product\/1","type":"products","fields":{"title":"title"}}]}',
[
'ok_count' => 1,
],
];

$collection = new ContentUpdateCollection(
[new ContentUpdate('title', 'product/1', 'products', ['availability' => 1])]
);
yield [
'POST',
$collection,
'{"objects":[{"url":"product\/1","type":"products","fields":{"availability":1,"title":"title"}}]}',
[
'ok_count' => 1,
],
];

$contentUpdate1 = new ContentUpdate('title', 'product/2', 'products', ['availability' => 1]);
$contentUpdate1->setActiveTo('2019-12-12 00:01:02');
$contentUpdate1->setAutocompleteType(
[
'categories',
'other',
]
);
$contentUpdate2 = new ContentUpdate('title', 'product/1', 'products', ['availability' => 0]);
$contentUpdate2->setGeneration('one');
$contentUpdate2->setNested([$contentUpdate1]);
$collection = new ContentUpdateCollection(
[$contentUpdate1, $contentUpdate2]
);
yield [
'POST',
$collection,
'{"objects":[{"autocomplete_type":["categories","other"],"active_to":"2019-12-12 00:01:02","url":"product\/2","type":"products","fields":{"availability":1,"title":"title"}},{"generation":"one","nested":[{"autocomplete_type":["categories","other"],"active_to":"2019-12-12 00:01:02","url":"product\/2","type":"products","fields":{"availability":1,"title":"title"}}],"url":"product\/1","type":"products","fields":{"availability":0,"title":"title"}}]}',
[
'ok_count' => 2,
],
];
}

#[Test]
#[DataProvider('forPartialContentUpdate')]
#[DataProviderExternal(RequestDataProvider::class, 'forPartialContentUpdate')]
public function partialContentUpdateRequestPassed(
string $httpMethod,
ContentUpdateCollection $collection,
Expand All @@ -120,58 +70,8 @@ public function partialContentUpdateRequestPassed(
self::assertSame([], $response->errors);
}

public static function forPartialContentUpdate(): iterable
{
yield [
'PATCH',
new ContentUpdateCollection([new PartialContentUpdate('product/1', 'products', ['brand' => 'brand name'])]),
'{"objects":[{"url":"product\/1","type":"products","fields":{"brand":"brand name"}}]}',
[
'ok_count' => 1,
],
];

yield [
'PATCH',
new ContentUpdateCollection([new PartialContentUpdate('product/1', 'products', ['title' => 'title'])]),
'{"objects":[{"url":"product\/1","type":"products","fields":{"title":"title"}}]}',
[
'ok_count' => 1,
],
];

$collection = new ContentUpdateCollection(
[new PartialContentUpdate('product/1', 'products', ['title' => 'title', 'availability' => 1])]
);
yield [
'PATCH',
$collection,
'{"objects":[{"url":"product\/1","type":"products","fields":{"title":"title","availability":1}}]}',
[
'ok_count' => 1,
],
];

$contentUpdate1 = new PartialContentUpdate('product/2', 'products', ['title' => 'title', 'availability' => 1]);
$contentUpdate1->setActiveTo('2019-12-12 00:01:02');
$contentUpdate2 = new PartialContentUpdate('product/1', 'products', ['title' => 'title', 'availability' => 0]);
$contentUpdate2->setGeneration('one');
$contentUpdate2->setNested([$contentUpdate1]);
$collection = new ContentUpdateCollection(
[$contentUpdate1, $contentUpdate2]
);
yield [
'PATCH',
$collection,
'{"objects":[{"active_to":"2019-12-12 00:01:02","url":"product\/2","type":"products","fields":{"title":"title","availability":1}},{"generation":"one","nested":[{"active_to":"2019-12-12 00:01:02","url":"product\/2","type":"products","fields":{"title":"title","availability":1}}],"url":"product\/1","type":"products","fields":{"title":"title","availability":0}}]}',
[
'ok_count' => 2,
],
];
}

#[Test]
#[DataProvider('forContentRemoval')]
#[DataProviderExternal(RequestDataProvider::class, 'forContentRemoval')]
public function contentRemovalRequestPassed(
string $httpMethod,
ContentRemovalCollection $collection,
Expand All @@ -188,20 +88,8 @@ public function contentRemovalRequestPassed(
self::assertSame([], $response->errors);
}

public static function forContentRemoval(): iterable
{
yield [
'DELETE',
new ContentRemovalCollection([new ContentRemoval('product/1', 'product')]),
'{"objects":[{"url":"product\/1","type":"product"}]}',
[
'ok_count' => 1,
],
];
}

#[Test]
#[DataProvider('forChangeAvailability')]
#[DataProviderExternal(RequestDataProvider::class, 'forChangeAvailability')]
public function changeAvailabilityRequestPassed(
string $httpMethod,
$collection,
Expand All @@ -221,38 +109,6 @@ public function changeAvailabilityRequestPassed(
self::assertSame([], $response->errors);
}

public static function forChangeAvailability(): iterable
{
yield [
'PATCH',
new ContentAvailabilityCollection([new ContentAvailability('product/1', true)]),
'{"objects":[{"url":"product\/1","fields":{"availability":1}}]}',
[
'ok_count' => 1,
],
];

yield [
'PATCH',
new ContentAvailabilityCollection(
[new ContentAvailability('product/1', true), new ContentAvailability('product/2', false)]
),
'{"objects":[{"url":"product\/1","fields":{"availability":1}},{"url":"product\/2","fields":{"availability":0}}]}',
[
'ok_count' => 2,
],
];

yield [
'PATCH',
new ContentAvailability('product/1', true),
'{"objects":[{"url":"product\/1","fields":{"availability":1}}]}',
[
'ok_count' => 1,
],
];
}

private function getRequestService(
string $httpMethod,
string $expectedContent,
Expand Down
76 changes: 4 additions & 72 deletions tests/Acceptance/Service/UpdateByQueryRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
use Answear\LuigisBoxBundle\Service\LuigisBoxSerializer;
use Answear\LuigisBoxBundle\Service\UpdateByQueryRequest;
use Answear\LuigisBoxBundle\Service\UpdateByQueryRequestInterface;
use Answear\LuigisBoxBundle\Tests\DataProvider\UpdateByQueryRequestDataProvider;
use Answear\LuigisBoxBundle\Tests\ExampleConfiguration;
use Answear\LuigisBoxBundle\ValueObject\UpdateByQuery;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Uri;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class UpdateByQueryRequestTest extends TestCase
{
#[Test]
#[DataProvider('forUpdate')]
#[DataProviderExternal(UpdateByQueryRequestDataProvider::class, 'forUpdate')]
public function updatePassed(
UpdateByQuery $updateByQuery,
string $expectedContent,
Expand All @@ -36,35 +37,8 @@ public function updatePassed(
$this->assertSame($jobId, $response->jobId);
}

public static function forUpdate(): iterable
{
yield [
new UpdateByQuery(
new UpdateByQuery\Search(['product'], ['color' => 'olive']),
new UpdateByQuery\Update(['color' => 'green']),
),
'{"search":{"partial":{"fields":{"color":"olive"}},"types":["product"]},"update":{"fields":{"color":"green"}}}',
[
'status_url' => '/v1/update_by_query?job_id=1',
],
1,
];

yield [
new UpdateByQuery(
new UpdateByQuery\Search(['product', 'brand'], ['color' => 'olive']),
new UpdateByQuery\Update(['color' => ['green', 'blue'], 'brand' => 'Star']),
),
'{"search":{"partial":{"fields":{"color":"olive"}},"types":["product","brand"]},"update":{"fields":{"color":["green","blue"],"brand":"Star"}}}',
[
'status_url' => '/v1/update_by_query?job_id=12',
],
12,
];
}

#[Test]
#[DataProvider('forUpdateStatus')]
#[DataProviderExternal(UpdateByQueryRequestDataProvider::class, 'forUpdateStatus')]
public function updateStatusPassed(
int $jobId,
array $apiResponse,
Expand Down Expand Up @@ -93,48 +67,6 @@ public function updateStatusPassed(
$this->assertSame($apiResponse, $response->rawResponse);
}

public static function forUpdateStatus(): iterable
{
yield [
1,
[
'tracker_id' => 'abcd',
'status' => 'complete',
'updates_count' => 5,
'failures_count' => 0,
'failures' => [],
],
];

yield [
111,
[
'tracker_id' => 'iabad',
'status' => 'in progress',
],
];

yield [
12,
[
'tracker_id' => 'abad',
'status' => 'complete',
'updates_count' => 5,
'failures_count' => 1,
'failures' => [
'/products/1' => [
'type' => 'data_schema_mismatch',
'reason' => 'failed to parse [attributes.price]',
'caused_by' => [
'type' => 'number_format_exception',
'reason' => 'For input string: "wrong sale price"',
],
],
],
],
];
}

private function getService(
string $httpMethod,
string $endpoint,
Expand Down
Loading

0 comments on commit aeea620

Please sign in to comment.