-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from commercetools/add-fixtures-tests
- Loading branch information
Showing
29 changed files
with
2,747 additions
and
186 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
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()); | ||
} | ||
); | ||
} | ||
} |
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,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 | ||
); | ||
} | ||
} |
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,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; | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.