Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced asserts with type hints #304

Merged
merged 2 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions spec/Command/AddCouponSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class AddCouponSpec extends ObjectBehavior
{
Expand All @@ -27,13 +28,13 @@ public function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 'COUPON_CODE');

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I would even think about removal of these tests right now. Strict types and scalar typehints should do the trick :)

}

public function it_throws_an_exception_if_coupon_code_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', new \stdClass());

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}
}
3 changes: 2 additions & 1 deletion spec/Command/AddressOrderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpSpec\ObjectBehavior;
use Sylius\ShopApiPlugin\Model\Address;
use TypeError;

final class AddressOrderSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -89,6 +90,6 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
])
);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}
}
6 changes: 4 additions & 2 deletions spec/Command/ChangeItemQuantitySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace spec\Sylius\ShopApiPlugin\Command;

use InvalidArgumentException;
use PhpSpec\ObjectBehavior;
use TypeError;

final class ChangeItemQuantitySpec extends ObjectBehavior
{
Expand Down Expand Up @@ -32,13 +34,13 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 'T_SHIRT_CODE', 1);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_quantity_is_not_less_then_1()
{
$this->beConstructedWith('ORDERTOKEN', 'T_SHIRT_CODE', 0);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(InvalidArgumentException::class)->duringInstantiation();
}
}
5 changes: 3 additions & 2 deletions spec/Command/ChoosePaymentMethodSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class ChoosePaymentMethodSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -32,13 +33,13 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 1, 'COD_METHOD');

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_payment_method_code_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', 1, new \stdClass());

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}
}
5 changes: 3 additions & 2 deletions spec/Command/ChooseShippingMethodSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class ChooseShippingMethodSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -32,13 +33,13 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 1, 'DHL_METHOD');

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_shipping_method_code_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', 1, new \stdClass());

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}
}
7 changes: 4 additions & 3 deletions spec/Command/CompleteOrderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class CompleteOrderSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -34,20 +35,20 @@ function it_throws_an_exception_if_notes_are_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', 'example@customer.com', new \stdClass());

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 'example@customer.com');

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_email_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', new \stdClass());

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}
}
3 changes: 2 additions & 1 deletion spec/Command/DropCartSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class DropCartSpec extends ObjectBehavior
{
Expand All @@ -22,6 +23,6 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass());

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}
}
5 changes: 3 additions & 2 deletions spec/Command/PickupCartSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class PickupCartSpec extends ObjectBehavior
{
Expand All @@ -27,13 +28,13 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 'CHANNEL_CODE');

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_channel_code_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', new \stdClass());

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}
}
5 changes: 3 additions & 2 deletions spec/Command/PutOptionBasedConfigurableItemToCartSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class PutOptionBasedConfigurableItemToCartSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -37,14 +38,14 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 'T_SHIRT_CODE', ['RED_OPTION_CODE'], 1);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_product_code_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', new \stdClass(), ['RED_OPTION_CODE'], 1);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_options_are_empty()
Expand Down
5 changes: 3 additions & 2 deletions spec/Command/PutSimpleItemToCartSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class PutSimpleItemToCartSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -32,14 +33,14 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 'T_SHIRT_CODE', 1);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_product_code_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', new \stdClass(), 1);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_quantity_is_not_less_then_0()
Expand Down
7 changes: 4 additions & 3 deletions spec/Command/PutVariantBasedConfigurableItemToCartSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Sylius\ShopApiPlugin\Command;

use PhpSpec\ObjectBehavior;
use TypeError;

final class PutVariantBasedConfigurableItemToCartSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -37,21 +38,21 @@ function it_throws_an_exception_if_order_token_is_not_a_string()
{
$this->beConstructedWith(new \stdClass(), 'T_SHIRT_CODE', 'RED_SMALL_T_SHIRT_CODE', 1);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_product_code_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', new \stdClass(), 'RED_SMALL_T_SHIRT_CODE', 1);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_product_variant_code_is_not_a_string()
{
$this->beConstructedWith('ORDERTOKEN', 'T_SHIRT_CODE', new \stdClass(), 1);

$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
$this->shouldThrow(TypeError::class)->duringInstantiation();
}

function it_throws_an_exception_if_quantity_is_not_less_then_0()
Expand Down
9 changes: 3 additions & 6 deletions src/Command/AddCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Sylius\ShopApiPlugin\Command;

use Webmozart\Assert\Assert;

final class AddCoupon
{
/**
Expand All @@ -22,25 +20,24 @@ final class AddCoupon
* @param string $orderToken
* @param string $couponCode
*/
public function __construct($orderToken, $couponCode)
public function __construct(string $orderToken, string $couponCode)
{
Assert::allString([$orderToken, $couponCode]);
$this->orderToken = $orderToken;
$this->couponCode = $couponCode;
}

/**
* @return string
*/
public function orderToken()
public function orderToken(): string
{
return $this->orderToken;
}

/**
* @return string
*/
public function couponCode()
public function couponCode(): string
{
return $this->couponCode;
}
Expand Down
11 changes: 4 additions & 7 deletions src/Command/AddressOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Sylius\ShopApiPlugin\Command;

use Sylius\ShopApiPlugin\Model\Address;
use Webmozart\Assert\Assert;

final class AddressOrder
{
Expand All @@ -29,10 +28,8 @@ final class AddressOrder
* @param Address $shippingAddress
* @param Address $billingAddress
*/
public function __construct($orderToken, Address $shippingAddress, Address $billingAddress)
public function __construct(string $orderToken, Address $shippingAddress, Address $billingAddress)
{
Assert::string($orderToken);

$this->orderToken = $orderToken;
$this->address = $shippingAddress;
$this->billingAddress = $billingAddress;
Expand All @@ -41,23 +38,23 @@ public function __construct($orderToken, Address $shippingAddress, Address $bill
/**
* @return string
*/
public function orderToken()
public function orderToken(): string
{
return $this->orderToken;
}

/**
* @return Address
*/
public function shippingAddress()
public function shippingAddress(): Address
{
return $this->address;
}

/**
* @return Address
*/
public function billingAddress()
public function billingAddress(): Address
{
return $this->billingAddress;
}
Expand Down
8 changes: 3 additions & 5 deletions src/Command/ChangeItemQuantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ final class ChangeItemQuantity
* @param mixed $itemIdentifier
* @param int $quantity
*/
public function __construct($orderToken, $itemIdentifier, $quantity)
public function __construct(string $orderToken, $itemIdentifier, int $quantity)
{
Assert::string($orderToken, 'Expected order token to be string, got %s');
Assert::integer($quantity, 'Expected quantity to be integer, got %s');
Assert::greaterThan($quantity, 0, 'Quantity should be greater than 0');

$this->orderToken = $orderToken;
Expand All @@ -42,7 +40,7 @@ public function __construct($orderToken, $itemIdentifier, $quantity)
/**
* @return string
*/
public function orderToken()
public function orderToken(): string
{
return $this->orderToken;
}
Expand All @@ -58,7 +56,7 @@ public function itemIdentifier()
/**
* @return int
*/
public function quantity()
public function quantity(): int
{
return $this->quantity;
}
Expand Down
Loading