From d6aba2e43b1a23e87a52b5e395173258891daa55 Mon Sep 17 00:00:00 2001 From: Carsten Jonstrup Date: Fri, 25 Jan 2019 19:34:13 +0100 Subject: [PATCH] Use interface for item (#4) * Updated item to use ItemInterface * Fix style * Update readme * added more ignore path * fixes from php cs-fixer * added cs fixer * Update config * update from cs fixer * Styles and files * Added new rules * Added ItemInterface * Apply diff from styleCI * More fixes * Fix style * Update types * Fix docblock --- .gitignore | 1 + README.md | 30 ++++------ docker-compose.yml | 6 ++ docker/csfixer/Dockerfile | 27 +++++++++ fix_cs | 81 +++++++++++++++++++++++++ phpunit.xml | 1 + src/Basket.php | 45 ++++++++------ src/Identifier/Runtime.php | 1 + src/Item.php | 55 ++++++++--------- src/ItemInterface.php | 112 ++++++++++++++++++++++++++++++++++ src/Storage/Runtime.php | 12 +++- src/StorageInterface.php | 6 +- src/Tax.php | 5 ++ tests/BasketTest.php | 120 ++++++++++++++++--------------------- tests/ItemTest.php | 29 +++------ 15 files changed, 368 insertions(+), 163 deletions(-) create mode 100644 docker-compose.yml create mode 100644 docker/csfixer/Dockerfile create mode 100755 fix_cs create mode 100755 src/ItemInterface.php diff --git a/.gitignore b/.gitignore index 7ca48fc..cbb3c94 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ composer.phar composer.lock priv-test.php .idea +.php_cs.cache diff --git a/README.md b/README.md index 7f3f70a..3c99d15 100755 --- a/README.md +++ b/README.md @@ -35,13 +35,13 @@ $basket = new Basket(new Session, new Cookie); Inserting an item into the basket is easy. The required keys are id, name, price, weight and quantity, although you can pass over any custom data that you like. ```php -$basket->insert([ +$basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 2, 'weight' => 300 -]); +])); ``` @@ -49,7 +49,7 @@ $basket->insert([ Inserting an item into the basket is easy. The required keys are id, name, price and quantity, although you can pass over any custom data that you like. If option items contains price or weight there values are added to the total weight / price of the product. ```php -$basket->insert([ +$basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, @@ -63,7 +63,7 @@ $basket->insert([ 'price' => 10 ], ], -]); +])); ``` ### Setting the tax rate for an item @@ -73,14 +73,14 @@ the price of the item. In the below example we will use 25% for the tax rate. ```php -$basket->insert([ +$basket->insert(new Item([ 'id' => 'mouseid', 'name' => 'Mouse', 'price' => 100, 'quantity' => 1, 'tax' => 25, 'weight' => 200 -]); +])); ``` ### Updating items in the basket @@ -93,14 +93,6 @@ foreach ($basket->contents() as $item) { } ``` -### Removing basket items -You can remove any items in your basket by using the ```remove()``` method on any basket item. -```php -foreach ($basket->contents() as $item) { - $item->remove(); -} -``` - ### Destroying/emptying the basket You can completely empty/destroy the basket by using the ```destroy()``` method. ```php @@ -140,6 +132,11 @@ $basket->total(false); $basket->has($itemIdentifier); ``` +### Remove an item from the Basket +```php +$basket->remove($identifier) +``` + ### Retrieve an item object by identifier ```php $basket->item($itemIdentifier); @@ -159,11 +156,6 @@ foreach ($basket->contents() as $item) { } ``` -### Remove an item from the Basket -```php -$item->remove(); -``` - ### You can also get the total weight for a single item ```php $item->weight(); diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ff98f92 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +version: '2' + +services: # sorted alphabetically + csfixer: + build: ./docker/csfixer + image: docker.lenius.dk/csfixer diff --git a/docker/csfixer/Dockerfile b/docker/csfixer/Dockerfile new file mode 100644 index 0000000..2794cac --- /dev/null +++ b/docker/csfixer/Dockerfile @@ -0,0 +1,27 @@ +FROM php:7.2-fpm + +RUN apt-get update && \ + apt-get install git libpq-dev libldap2-dev zlib1g-dev libcurl4-gnutls-dev libicu-dev libmcrypt-dev libxml2-dev g++ -y && \ + rm -rf /var/lib/apt/lists/* && \ + docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \ + docker-php-ext-install -j$(nproc) zip ldap intl curl pdo pdo_mysql pdo_pgsql pgsql bcmath soap zip sockets && \ + pecl install redis apcu && \ + docker-php-ext-enable redis apcu + +# Unzip is required for composer. +RUN apt-get update && apt-get install -y unzip + +# Install composer +ADD https://getcomposer.org/installer /composer-setup.php + +RUN php /composer-setup.php \ + --install-dir=/usr/local/bin/ \ + --filename=composer \ + && rm /composer-setup.php + +# Allow Composer to be run as root. +ENV COMPOSER_ALLOW_SUPERUSER 1 + +ENV PATH="~/.composer/vendor/bin:${PATH}" + +RUN composer global require friendsofphp/php-cs-fixer diff --git a/fix_cs b/fix_cs new file mode 100755 index 0000000..1a65230 --- /dev/null +++ b/fix_cs @@ -0,0 +1,81 @@ +#!/bin/bash -e + +# Directory or file - Should be relative path +PROJECT_PATH="${1}" + +# Default +if [[ ${PROJECT_PATH} = "" ]]; then + + DEFAULT_DIRS=(src tests) + + echo "Running $0 on default directories: ${DEFAULT_DIRS[@]}" + + for dir_to_check in "${DEFAULT_DIRS[@]}"; do + $0 "${dir_to_check}" + done + + exit 0 +fi + +# Sanity check +if [[ ! -e ${PROJECT_PATH} ]]; then + echo "Doesn't seem to exist: ${PROJECT_PATH}" >&2 + exit 1 +fi + +PROJECT_DIR=${PROJECT_PATH} + +if [[ -f ${PROJECT_PATH} ]]; then + PROJECT_DIR=$(dirname ${PROJECT_PATH}) +fi + +echo "Fixing: ${PROJECT_PATH}" + +docker run \ + --rm \ + -v "$(pwd)/${PROJECT_DIR}":/source/${PROJECT_DIR} \ + docker.lenius.dk/csfixer \ + /root/.composer/vendor/bin/php-cs-fixer \ + --verbose \ + --using-cache=yes \ + --cache-file=/source/${PROJECT_DIR}/.php_cs.cache \ + --rules=' + { + "@PSR2": true, + "array_indentation": true, + "binary_operator_spaces": { + "operators": {"=>": "align_single_space"} + }, + "blank_line_after_opening_tag": false, + "blank_line_before_statement": {"statements": ["do", "for", "foreach", "if", "switch", "throw", "while", "return"]}, + "concat_space": {"spacing": "one"}, + "no_blank_lines_after_phpdoc": false, + "no_blank_lines_after_class_opening": true, + "no_whitespace_in_blank_line": true, + "no_trailing_comma_in_singleline_array": true, + "no_trailing_comma_in_list_call": true, + "no_extra_blank_lines": true, + "no_empty_phpdoc": true, + "no_unused_imports": true, + "method_argument_space": true, + "method_separation": true, + "phpdoc_indent": true, + "phpdoc_scalar": true, + "phpdoc_order": true, + "phpdoc_align": true, + "phpdoc_separation": true, + "phpdoc_inline_tag": true, + "phpdoc_no_access": true, + "phpdoc_no_package": true, + "phpdoc_separation": true, + "phpdoc_summary": true, + "phpdoc_to_comment": true, + "phpdoc_trim": true, + "phpdoc_order": true, + "single_quote": {"strings_containing_single_quote_chars": true}, + "whitespace_after_comma_in_array": true, + "concat_space": { + "spacing": "none" + } + }' \ + fix /source/${PROJECT_PATH} diff --git a/phpunit.xml b/phpunit.xml index bc32345..8c49a15 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,6 +16,7 @@ ./vendor ./src/IdentifierInterface.php ./src/StorageInterface.php + ./src/ItemInterface.php diff --git a/src/Basket.php b/src/Basket.php index c07545d..0b0725c 100755 --- a/src/Basket.php +++ b/src/Basket.php @@ -27,11 +27,16 @@ */ class Basket { + /** @var string $id */ protected $id; + /** @var IdentifierInterface $identifier */ protected $identifier; + + /** @var StorageInterface $store */ protected $store; + /** @var array $requiredParams */ protected $requiredParams = [ 'id', 'name', @@ -78,24 +83,26 @@ public function &contents($asArray = false) /** * Insert an item into the basket. * - * @param array $item An array of item data + * @param ItemInterface $item * * @return string A unique item identifier */ - public function insert(array $item) + public function insert(ItemInterface $item) { $this->checkArgs($item); $itemIdentifier = $this->createItemIdentifier($item); - if ($this->has($itemIdentifier) && $this->item($itemIdentifier) instanceof Item) { - $item['quantity'] = $this->item($itemIdentifier)->quantity + $item['quantity']; + if ($this->has($itemIdentifier) && $this->item($itemIdentifier) instanceof ItemInterface) { + $this->item($itemIdentifier)->quantity; + + $item->quantity = $this->item($itemIdentifier)->quantity + $item->quantity; $this->update($itemIdentifier, $item); return $itemIdentifier; } - $item = new Item($itemIdentifier, $item, $this->store); + $item->setIdentifier($itemIdentifier); $this->store->insertUpdate($item); @@ -105,9 +112,9 @@ public function insert(array $item) /** * Update an item. * - * @param string $itemIdentifier The unique item identifier - * @param string|array $key The key to update, or an array of key-value pairs - * @param mixed $value The value to set $key to + * @param string $itemIdentifier The unique item identifier + * @param mixed $key The key to update, or an array of key-value pairs + * @param mixed $value The value to set $key to * * @return void */ @@ -269,32 +276,36 @@ public function setIdentifier($identifier) /** * Create a unique item identifier. * - * @param array $item An array of item data + * @param ItemInterface $item * * @return string An md5 hash of item */ - protected function createItemIdentifier(array $item) + protected function createItemIdentifier(ItemInterface $item) { - if (!array_key_exists('options', $item)) { - $item['options'] = []; + if (!array_key_exists('options', $item->toArray())) { + $item->options = []; } - ksort($item['options']); + $options = $item->options; + + ksort($options); + + $item->options = $options; - return md5($item['id'].serialize($item['options'])); + return md5($item->id.serialize($item->options)); } /** * Check if a basket item has the required parameters. * - * @param array $item An array of item data + * @param ItemInterface $item * * @return void */ - protected function checkArgs(array $item) + protected function checkArgs(ItemInterface $item) { foreach ($this->requiredParams as $param) { - if (!array_key_exists($param, $item)) { + if (!array_key_exists($param, $item->toArray())) { throw new InvalidArgumentException("The '{$param}' field is required"); } } diff --git a/src/Identifier/Runtime.php b/src/Identifier/Runtime.php index f5201fe..91e86f0 100755 --- a/src/Identifier/Runtime.php +++ b/src/Identifier/Runtime.php @@ -27,6 +27,7 @@ */ class Runtime implements IdentifierInterface { + /** @var string $identifier */ protected static $identifier; /** diff --git a/src/Item.php b/src/Item.php index c47b402..ca26f34 100755 --- a/src/Item.php +++ b/src/Item.php @@ -28,14 +28,11 @@ * @property-read int $quantity * @property-read float $weight */ -class Item +class Item implements ItemInterface { /** @var string $identifier */ protected $identifier; - /** @var StorageInterface $store */ - protected $store; - /** @var Tax $tax */ protected $tax; @@ -45,16 +42,10 @@ class Item /** * Construct the item. * - * @param string $identifier - * @param array $item - * @param StorageInterface $store + * @param array $item */ - public function __construct($identifier, array $item, StorageInterface $store) + public function __construct(array $item) { - $this->identifier = $identifier; - - $this->store = $store; - foreach ($item as $key => $value) { $this->data[$key] = $value; } @@ -64,10 +55,22 @@ public function __construct($identifier, array $item, StorageInterface $store) $this->tax = new Tax($item['tax']); } + /** + * Set identifier. + * + * @param mixed $identifier + * + * @return mixed|void + */ + public function setIdentifier($identifier) + { + $this->identifier = $identifier; + } + /** * Return the value of protected methods. * - * @param string $param + * @param mixed $param * * @return mixed */ @@ -85,21 +88,12 @@ public function __get($param) public function __set($param, $value) { $this->data[$param] = $value; + if ($param == 'tax') { $this->tax = new Tax($value); } } - /** - * Removes the current item from the cart. - * - * @return void - */ - public function remove() - { - $this->store->remove($this->identifier); - } - /** * Return the total tax for this item. * @@ -192,22 +186,23 @@ public function single($includeTax = true) /** * Update a single key for this item, or multiple. * - * @param array|string $key The array key to update, or an array of key-value pairs to update - * @param null $value + * @param mixed $key The array key to update, or an array of key-value pairs to update + * @param mixed $value * * @return void */ public function update($key, $value = null) { - if (is_array($key)) { - foreach ($key as $updateKey => $updateValue) { + if ($key instanceof ItemInterface) { + foreach ($key->toArray() as $updateKey => $updateValue) { $this->update($updateKey, $updateValue); } } else { - // Update the item - $this->data[$key] = $value; if ($key == 'tax' && is_numeric($value)) { - $this->tax = new Tax($value); + $this->tax = new Tax(floatval($value)); + } else { + // update the item + $this->data[$key] = $value; } } } diff --git a/src/ItemInterface.php b/src/ItemInterface.php new file mode 100755 index 0000000..bc2ea63 --- /dev/null +++ b/src/ItemInterface.php @@ -0,0 +1,112 @@ + + * @copyright 2017 Lenius. + * + * @version production + * + * @link https://github.com/lenius/basket + */ + +namespace Lenius\Basket; + +/** + * Interface ItemInterface. + * + * @property string $id + * @property string $identifier + * @property int $quantity + * @property array $options + */ +interface ItemInterface +{ + /** + * @param mixed $identifier + * + * @return mixed + */ + public function setIdentifier($identifier); + + /** + * Return the total tax for this item. + * + * @return float + */ + public function tax(); + + /** + * Return the total price for this item. + * + * @param bool $includeTax + * + * @return float + */ + public function total($includeTax = true); + + /** + * Return the total weight of the item. + * + * @return float + */ + public function weight(); + + /** + * Return the total of the item, with or without tax. + * + * @param bool $includeTax Whether or not to include tax + * + * @return float The total, as a float + */ + public function single($includeTax = true); + + /** + * Update a single key for this item, or multiple. + * + * @param mixed $key The array key to update, or an array of key-value pairs to update + * @param mixed $value + * + * @return void + */ + public function update($key, $value = null); + + /** + * Check if this item has options. + * + * @return bool Yes or no? + */ + public function hasOptions(); + + /** + * Convert the item into an array. + * + * @return array The item data + */ + public function toArray(); + + /** + * Update data array using set magic method. + * + * @param mixed $param + * @param mixed $value + */ + public function __set($param, $value); + + /** + * Return the value of protected methods. + * + * @param mixed $param + * + * @return mixed + */ + public function __get($param); +} diff --git a/src/Storage/Runtime.php b/src/Storage/Runtime.php index 5d84800..e07099c 100755 --- a/src/Storage/Runtime.php +++ b/src/Storage/Runtime.php @@ -21,6 +21,7 @@ namespace Lenius\Basket\Storage; use Lenius\Basket\Item; +use Lenius\Basket\ItemInterface; use Lenius\Basket\StorageInterface; /** @@ -28,18 +29,23 @@ */ class Runtime implements StorageInterface { + /** @var null|string $identifier */ protected $identifier; + + /** @var array $cart */ protected static $cart = []; + + /** @var string $id */ protected $id = 'basket'; /** * Add or update an item in the cart. * - * @param Item $item The item to insert or update + * @param ItemInterface $item The item to insert or update * * @return void */ - public function insertUpdate(Item $item) + public function insertUpdate(ItemInterface $item) { static::$cart[$this->id][$item->identifier] = $item; } @@ -154,6 +160,8 @@ public function destroy() * @param string $id * * @internal param string $identifier + * + * @return void */ public function setIdentifier($id) { diff --git a/src/StorageInterface.php b/src/StorageInterface.php index 177923a..e7bdf1d 100755 --- a/src/StorageInterface.php +++ b/src/StorageInterface.php @@ -28,11 +28,11 @@ interface StorageInterface /** * Add or update an item in the cart. * - * @param Item $item The item to insert or update + * @param ItemInterface $item The item to insert or update * * @return void */ - public function insertUpdate(Item $item); + public function insertUpdate(ItemInterface $item); /** * Retrieve the cart data. @@ -103,7 +103,7 @@ public function setIdentifier($identifier); /** * Return the current cart identifier. * - * @return void + * @return string */ public function getIdentifier(); } diff --git a/src/Tax.php b/src/Tax.php index e06a76a..fa231c8 100755 --- a/src/Tax.php +++ b/src/Tax.php @@ -25,8 +25,13 @@ */ class Tax { + /** @var float $percentage */ protected $percentage; + + /** @var float $deductModifier */ protected $deductModifier; + + /** @var float $addModifier */ protected $addModifier; /** diff --git a/tests/BasketTest.php b/tests/BasketTest.php index 1fb9e71..ec06c92 100755 --- a/tests/BasketTest.php +++ b/tests/BasketTest.php @@ -27,6 +27,7 @@ class BasketTest extends TestCase { + /** @var Basket */ private $basket; public function setUp() @@ -41,13 +42,13 @@ public function tearDown() public function testInsert() { - $actualId = $this->basket->insert([ + $actualId = $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 1, 'weight' => 200, - ]); + ])); $identifier = md5('foo'.serialize([])); @@ -56,36 +57,36 @@ public function testInsert() public function testInsertIncrements() { - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 150, 'quantity' => 1, 'weight' => 200, - ]); + ])); $this->assertEquals(150, $this->basket->total()); - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 150, 'quantity' => 1, 'weight' => 200, - ]); + ])); $this->assertEquals(300, $this->basket->total()); } public function testUpdate() { - $actualId = $this->basket->insert([ + $actualId = $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 1, 'weight' => 200, - ]); + ])); $this->basket->update($actualId, 'name', 'baz'); @@ -94,13 +95,13 @@ public function testUpdate() public function testMagicUpdate() { - $actualId = $this->basket->insert([ + $actualId = $this->basket->insert(new Item([ 'id' => 'zxy', 'name' => 'dolly', 'price' => 120, 'quantity' => 3, 'weight' => 400, - ]); + ])); foreach ($this->basket->contents() as $item) { $item->name = 'bim'; @@ -113,7 +114,7 @@ public function testWeight() { $weight = rand(200, 300); - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, @@ -125,9 +126,9 @@ public function testWeight() 'price' => 50, ], ], - ]); + ])); - // Test that the total weight is being calculated successfully + // test that the total weight is being calculated successfully $this->assertEquals($weight, $this->basket->weight()); } @@ -137,7 +138,7 @@ public function testWeightOption() $weight_option = rand(50, 800); $quantity = rand(1, 10); - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, @@ -150,9 +151,9 @@ public function testWeightOption() 'weight' => $weight_option, ], ], - ]); + ])); - // Test that the total weight is being calculated successfully + // test that the total weight is being calculated successfully $this->assertEquals(($weight + $weight_option) * $quantity, $this->basket->weight()); } @@ -161,7 +162,7 @@ public function testPriceOption() $weight = rand(200, 300); $weight_option = rand(50, 100); - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, @@ -180,9 +181,9 @@ public function testPriceOption() 'weight' => $weight_option, ], ], - ]); + ])); - // Test that the total price is being calculated successfully + // test that the total price is being calculated successfully $this->assertEquals(250, $this->basket->total(true)); $this->assertEquals(200, $this->basket->total(false)); } @@ -192,7 +193,7 @@ public function testPriceDistractOption() $weight = rand(400, 500); $weight_option = rand(200, 300); - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'zxy', 'name' => 'pink', 'price' => 100, @@ -211,22 +212,22 @@ public function testPriceDistractOption() 'weight' => $weight_option, ], ], - ]); + ])); - // Test that the total price is being calculated successfully + // test that the total price is being calculated successfully $this->assertEquals(162.50, $this->basket->total(true)); $this->assertEquals(130, $this->basket->total(false)); } public function testFind() { - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 1, 'weight' => 200, - ]); + ])); $this->assertInstanceOf('\Lenius\Basket\Item', $this->basket->find('foo')); } @@ -237,15 +238,15 @@ public function testTotals() $price = rand(20, 99999); $quantity = rand(1, 10); - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => $price, 'quantity' => $quantity, 'weight' => 200, - ]); + ])); - // Test that the total is being calculated successfully + // test that the total is being calculated successfully $this->assertEquals($price * $quantity, $this->basket->total()); } @@ -257,13 +258,13 @@ public function testTotalItems() for ($i = 1; $i <= $adding; $i++) { $quantity = rand(1, 20); - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => uniqid(), 'name' => 'bar', 'price' => 100, 'quantity' => $quantity, 'weight' => 200, - ]); + ])); $actualTotal += $quantity; } @@ -272,36 +273,15 @@ public function testTotalItems() $this->assertEquals($this->basket->totalItems(true), $adding); } - public function testItemRemoval() - { - $this->basket->insert([ - 'id' => 'foo', - 'name' => 'bar', - 'price' => 100, - 'quantity' => 1, - 'weight' => 200, - ]); - - $contents = &$this->basket->contents(); - - $this->assertNotEmpty($contents); - - foreach ($contents as $item) { - $item->remove(); - } - - $this->assertEmpty($contents); - } - public function testAlternateItemRemoval() { - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 1, 'weight' => 200, - ]); + ])); $contents = &$this->basket->contents(); @@ -316,26 +296,26 @@ public function testAlternateItemRemoval() public function testItemToArray() { - $actualId = $this->basket->insert([ + $actualId = $this->basket->insert(new Item([ 'id' => 'ase', 'name' => 'fly', 'price' => 160, 'quantity' => 12, 'weight' => 123, - ]); + ])); $this->assertTrue(is_array($this->basket->item($actualId)->toArray())); } public function testbasketToArray() { - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'asb', 'name' => 'shuttle', 'price' => 130, 'quantity' => 11, 'weight' => 60, - ]); + ])); foreach ($this->basket->contents(true) as $item) { $this->assertTrue(is_array($item)); @@ -344,14 +324,14 @@ public function testbasketToArray() public function testTax() { - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 1, 'tax' => 20, 'weight' => 200, - ]); + ])); // Test that the tax is being calculated successfully $this->assertEquals(20, $this->basket->tax()); @@ -359,7 +339,7 @@ public function testTax() public function testTaxOptions() { - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, @@ -372,49 +352,49 @@ public function testTaxOptions() 'value' => 'L', 'weight' => 50, 'price' => 100, - ], - ], - ]); + ], + ], + ])); - // Test that the tax is being calculated successfully + // test that the tax is being calculated successfully $this->assertEquals(40, $this->basket->tax()); } public function testTaxMultiply() { - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 2, 'tax' => 20, 'weight' => 200, - ]); + ])); - // Test that the tax is being calculated successfully + // test that the tax is being calculated successfully $this->assertEquals(240, $this->basket->total()); - // Test that the total method can also return the pre-tax price if false is passed + // test that the total method can also return the pre-tax price if false is passed $this->assertEquals(200, $this->basket->total(false)); } public function testTaxUpdate() { - $this->basket->insert([ + $this->basket->insert(new Item([ 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 1, 'tax' => 20, 'weight' => 200, - ]); + ])); $identifier = md5('foo'.serialize([])); /** @var Item $item */ $item = $this->basket->item($identifier); - // Test that the tax is being calculated successfully + // test that the tax is being calculated successfully $item->update('tax', 0); $this->assertEquals(100, $item->total()); $this->assertEquals(100, $item->total(false)); diff --git a/tests/ItemTest.php b/tests/ItemTest.php index 67bf124..e674482 100755 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -21,7 +21,6 @@ */ use Lenius\Basket\Basket; use Lenius\Basket\Item; -use Lenius\Basket\Storage\Runtime as RuntimeStore; use PHPUnit\Framework\TestCase; class ItemTest extends TestCase @@ -39,8 +38,6 @@ public function tearDown() public function testTaxUpdate() { - $identifier = 1; - $item = [ 'id' => 'foo', 'name' => 'bar', @@ -49,7 +46,7 @@ public function testTaxUpdate() 'weight' => 200, ]; - $this->item = new Item($identifier, $item, new RuntimeStore()); + $this->item = new Item($item); $this->item->update('tax', 0); $this->assertEquals(100, $this->item->total()); @@ -62,8 +59,6 @@ public function testTaxUpdate() public function testWeight() { - $identifier = 1; - $weight = rand(200, 300); $quantity = rand(1, 10); @@ -75,14 +70,12 @@ public function testWeight() 'weight' => $weight, ]; - $this->item = new Item($identifier, $item, new RuntimeStore()); + $this->item = new Item($item); $this->assertEquals(($weight * $quantity), $this->item->weight()); } public function testWeightWithOption() { - $identifier = 1; - $weight = rand(200, 300); $weight_option_one = rand(50, 100); $weight_option_two = rand(150, 200); @@ -110,14 +103,12 @@ public function testWeightWithOption() ], ]; - $this->item = new Item($identifier, $item, new RuntimeStore()); + $this->item = new Item($item); $this->assertEquals(($weight + ($weight_option_one + $weight_option_two)) * $quantity, $this->item->weight()); } public function testHasOption() { - $identifier = 1; - $item = [ 'id' => 'foo', 'name' => 'bar', @@ -134,14 +125,12 @@ public function testHasOption() ], ]; - $this->item = new Item($identifier, $item, new RuntimeStore()); + $this->item = new Item($item); $this->assertTrue($this->item->hasOptions()); } public function testHasNoOption() { - $identifier = 1; - $item = [ 'id' => 'foo', 'name' => 'bar', @@ -150,14 +139,12 @@ public function testHasNoOption() 'weight' => 200, ]; - $this->item = new Item($identifier, $item, new RuntimeStore()); + $this->item = new Item($item); $this->assertFalse($this->item->hasOptions()); } public function testItemTotalPrice() { - $identifier = 1; - $item = [ 'id' => 'foo', 'name' => 'bar', @@ -166,15 +153,13 @@ public function testItemTotalPrice() 'weight' => 200, ]; - $this->item = new Item($identifier, $item, new RuntimeStore()); + $this->item = new Item($item); $this->assertEquals(100, $this->item->single()); } public function testItemSetTax() { - $identifier = 1; - $item = [ 'id' => 'foo', 'name' => 'bar', @@ -183,7 +168,7 @@ public function testItemSetTax() 'weight' => 200, ]; - $this->item = new Item($identifier, $item, new RuntimeStore()); + $this->item = new Item($item); $this->item->tax = 25;