Skip to content

Commit

Permalink
Merge pull request #103 from commercetools/add-fixtures-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Feb 21, 2023
2 parents 6919b10 + 48b71bf commit 3bd8ce2
Show file tree
Hide file tree
Showing 29 changed files with 2,747 additions and 186 deletions.
25 changes: 25 additions & 0 deletions test/integration/Api/Cart/CartCreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Commercetools\IntegrationTest\Api\Cart;

use Commercetools\Api\Models\Cart\Cart;
use Commercetools\IntegrationTest\ApiTestCase;

class CartCreateTest extends ApiTestCase
{
public function testCartIsCreated()
{
$builder = $this->getApiBuilder();

CartFixture::withCart(
$builder,
function (Cart $cart) use ($builder) {
$request = $builder->with()->carts()->withId($cart->getId())->get();
$result = $request->execute();

$this->assertInstanceOf(Cart::class, $result);
$this->assertSame($cart->getId(), $result->getId());
}
);
}
}
153 changes: 153 additions & 0 deletions test/integration/Api/Cart/CartFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Commercetools\IntegrationTest\Api\Cart;

use Commercetools\Api\Models\Cart\Cart;
use Commercetools\Api\Models\Cart\CartDraft;
use Commercetools\Api\Models\Cart\CartDraftBuilder;
use Commercetools\Api\Models\Common\AddressBuilder;
use Commercetools\Client\ApiRequestBuilder;
use Ramsey\Uuid\Uuid;

class CartFixture
{
final public static function uniqueCartString()
{
return 'test-' . Uuid::uuid4();
}

final public static function defaultCartDraftFunction()
{
$builder = CartDraftBuilder::of();
$builder
->withCurrency('EUR')
->withCountry('DE')
->withShippingAddress(
AddressBuilder::of()
->withCountry('DE')
->build()
);

return $builder;
}

final public static function defaultCartDraftBuilderFunction(CartDraftBuilder $draftBuilder)
{
return $draftBuilder->build();
}

final public static function defaultCartCreateFunction(ApiRequestBuilder $builder, CartDraft $draft)
{
$request = $builder->with()->carts()->post($draft);

return $request->execute();
}

final public static function defaultCartDeleteFunction(ApiRequestBuilder $builder, Cart $resource)
{
$request = $builder
->with()
->carts()
->withId($resource->getId())
->delete()
->withVersion($resource->getVersion());

return $request->execute();
}

final public static function withDraftCart(
ApiRequestBuilder $builder,
callable $draftBuilderFunction,
callable $assertFunction,
callable $createFunction = null,
callable $deleteFunction = null,
callable $draftFunction = null,
array $additionalResources = []
) {
if ($draftFunction == null) {
$draftFunction = [__CLASS__, 'defaultCartDraftFunction'];
}
if ($createFunction == null) {
$createFunction = [__CLASS__, 'defaultCartCreateFunction'];
}
if ($deleteFunction == null) {
$deleteFunction = [__CLASS__, 'defaultCartDeleteFunction'];
}
$initialDraft = call_user_func($draftFunction);

$resourceDraft = call_user_func($draftBuilderFunction, $initialDraft);

$resource = call_user_func($createFunction, $builder, $resourceDraft, ...$additionalResources);
try {
call_user_func($assertFunction, $resource, ...$additionalResources);
} finally {
call_user_func($deleteFunction, $builder, $resource);
}
}

final public static function withCart(
ApiRequestBuilder $builder,
callable $assertFunction,
callable $createFunction = null,
callable $deleteFunction = null,
callable $draftFunction = null
) {
self::withDraftCart(
$builder,
[__CLASS__, 'defaultCartDraftBuilderFunction'],
$assertFunction,
$createFunction,
$deleteFunction,
$draftFunction
);
}

final public static function withUpdateableDraftCart(
ApiRequestBuilder $builder,
callable $draftBuilderFunction,
callable $assertFunction,
callable $createFunction = null,
callable $deleteFunction = null,
callable $draftFunction = null,
array $additionalResources = []
) {
if ($draftFunction == null) {
$draftFunction = [__CLASS__, 'defaultCartDraftFunction'];
}
if ($createFunction == null) {
$createFunction = [__CLASS__, 'defaultCartCreateFunction'];
}
if ($deleteFunction == null) {
$deleteFunction = [__CLASS__, 'defaultCartDeleteFunction'];
}
$initialDraft = call_user_func($draftFunction);

$resourceDraft = call_user_func($draftBuilderFunction, $initialDraft);

$resource = call_user_func($createFunction, $builder, $resourceDraft, ...$additionalResources);

$updatedResource = null;
try {
$updatedResource = call_user_func($assertFunction, $resource, ...$additionalResources);
} finally {
call_user_func($deleteFunction, $builder, $updatedResource != null ? $updatedResource : $resource);
}
}

final public static function withUpdateableCart(
ApiRequestBuilder $builder,
callable $assertFunction,
callable $createFunction = null,
callable $deleteFunction = null,
callable $draftFunction = null
) {
self::withUpdateableDraftCart(
$builder,
[__CLASS__, 'defaultCartDraftBuilderFunction'],
$assertFunction,
$createFunction,
$deleteFunction,
$draftFunction
);
}
}
131 changes: 131 additions & 0 deletions test/integration/Api/Cart/CartUpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Commercetools\IntegrationTest\Api\Cart;

use Commercetools\Api\Models\Cart\Cart;
use Commercetools\Api\Models\Cart\CartAddLineItemActionModel;
use Commercetools\Api\Models\Cart\CartDraftBuilder;
use Commercetools\Api\Models\Cart\CartSetKeyActionModel;
use Commercetools\Api\Models\Cart\CartSetShippingAddressActionModel;
use Commercetools\Api\Models\Cart\CartUpdateActionCollection;
use Commercetools\Api\Models\Cart\CartUpdateBuilder;
use Commercetools\Api\Models\Common\AddressBuilder;
use Commercetools\Api\Models\Product\Product;
use Commercetools\IntegrationTest\Api\Product\ProductFixture;
use Commercetools\IntegrationTest\ApiTestCase;

class CartUpdateTest extends ApiTestCase
{
public function testSetKey()
{
$builder = $this->getApiBuilder();

CartFixture::withUpdateableCart(
$builder,
function (Cart $cart) use ($builder) {
$key = CartFixture::uniqueCartString();

$updateAction = new CartSetKeyActionModel();
$updateAction->setKey($key);

$updateActionCollection = new CartUpdateActionCollection();
$updateActionCollection->add($updateAction);

$cartUpdate = CartUpdateBuilder::of()
->withVersion($cart->getVersion())
->withActions($updateActionCollection)
->build();
$request = $builder
->with()
->carts()
->withId($cart->getId())
->post($cartUpdate);
$cartQueryResponse = $request->execute();

$this->assertSame($key, $cartQueryResponse->getKey());

return $cartQueryResponse;
}
);
}

public function testAddProduct()
{
$builder = $this->getApiBuilder();

ProductFixture::withPublishedProduct(
$builder,
function (Product $product) use ($builder) {
CartFixture::withUpdateableCart(
$builder,
function (Cart $cart) use ($builder, $product) {
$updateAction = new CartAddLineItemActionModel();
$updateAction->setProductId($product->getId());

$updateActionCollection = new CartUpdateActionCollection();
$updateActionCollection->add($updateAction);
$cartUpdate = CartUpdateBuilder::of()
->withVersion($cart->getVersion())
->withActions($updateActionCollection)
->build();
$request = $builder
->with()
->carts()
->withId($cart->getId())
->post($cartUpdate);
$cartQueryResponse = $request->execute();

$this->assertInstanceOf(Cart::class, $cartQueryResponse);
$this->assertNotNull($cartQueryResponse->getLineItems()->current()->getId());
$this->assertSame($product->getId(), $cartQueryResponse->getLineItems()->current()->getProductId());

return $cartQueryResponse;
}
);
}
);
}

public function testSetShippingAddress()
{
$builder = $this->getApiBuilder();
$key = CartFixture::uniqueCartString();

CartFixture::withUpdateableDraftCart(
$builder,
function (CartDraftBuilder $cartDraft) use ($key) {
$cartDraftBuilder = CartDraftBuilder::of()
->withCurrency('EUR')
->withKey($key);

return $cartDraftBuilder->build();
},
function (Cart $cart) use ($builder, $key) {
$address = AddressBuilder::of()
->withCountry('DE')
->build();
$updateAction = new CartSetShippingAddressActionModel();
$updateAction->setAddress($address);

$updateActionCollection = new CartUpdateActionCollection();
$updateActionCollection->add($updateAction);
$cartUpdate = CartUpdateBuilder::of()
->withVersion($cart->getVersion())
->withActions($updateActionCollection)
->build();
$request = $builder
->with()
->carts()
->withId($cart->getId())
->post($cartUpdate);
$cartQueryResponse = $request->execute();

$this->assertInstanceOf(Cart::class, $cartQueryResponse);
$this->assertSame($key, $cartQueryResponse->getKey());
$this->assertSame($address->getCountry(), $cartQueryResponse->getShippingAddress()->getCountry());

return $cartQueryResponse;
}
);
}
}
Loading

0 comments on commit 3bd8ce2

Please sign in to comment.