mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ccba20119 | ||
|
|
313a4f7aa2 | ||
|
|
4a312a37e2 | ||
|
|
274a8504af | ||
|
|
4f22672ca5 | ||
|
|
61475a7060 | ||
|
|
6604cf984d | ||
|
|
7d955d722c | ||
|
|
54d94d8d12 |
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "wearepixel/laravel-cart",
|
"name": "wearepixel/laravel-cart",
|
||||||
"version": "1.0.4",
|
"version": "1.0.8",
|
||||||
"description": "A cart implementation for Laravel",
|
"description": "A cart implementation for Laravel",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"laravel",
|
"laravel",
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
"require-dev": {
|
"require-dev": {
|
||||||
"mockery/mockery": "^1.4.2",
|
"mockery/mockery": "^1.4.2",
|
||||||
"laravel/pint": "dev-main",
|
"laravel/pint": "dev-main",
|
||||||
"pestphp/pest": "2.35.0",
|
"pestphp/pest": "2.35.1",
|
||||||
"larapack/dd": "dev-master"
|
"larapack/dd": "dev-master"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
Generated
+28
-28
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "f22ad76e04818683f42bf13aa7a5e4ef",
|
"content-hash": "726c11e0474359fea45e3d6790b14426",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "brick/math",
|
"name": "brick/math",
|
||||||
@@ -2963,21 +2963,21 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "pestphp/pest",
|
"name": "pestphp/pest",
|
||||||
"version": "v2.35.0",
|
"version": "v2.35.1",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/pestphp/pest.git",
|
"url": "https://github.com/pestphp/pest.git",
|
||||||
"reference": "d0ff2c8ec294b7aa7fcb0f3ddc4fdec864234646"
|
"reference": "b13acb630df52c06123588d321823c31fc685545"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/pestphp/pest/zipball/d0ff2c8ec294b7aa7fcb0f3ddc4fdec864234646",
|
"url": "https://api.github.com/repos/pestphp/pest/zipball/b13acb630df52c06123588d321823c31fc685545",
|
||||||
"reference": "d0ff2c8ec294b7aa7fcb0f3ddc4fdec864234646",
|
"reference": "b13acb630df52c06123588d321823c31fc685545",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"brianium/paratest": "^7.3.1",
|
"brianium/paratest": "^7.3.1",
|
||||||
"nunomaduro/collision": "^7.10.0|^8.3.0",
|
"nunomaduro/collision": "^7.10.0|^8.4.0",
|
||||||
"nunomaduro/termwind": "^1.15.1|^2.0.1",
|
"nunomaduro/termwind": "^1.15.1|^2.0.1",
|
||||||
"pestphp/pest-plugin": "^2.1.1",
|
"pestphp/pest-plugin": "^2.1.1",
|
||||||
"pestphp/pest-plugin-arch": "^2.7.0",
|
"pestphp/pest-plugin-arch": "^2.7.0",
|
||||||
@@ -3055,7 +3055,7 @@
|
|||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/pestphp/pest/issues",
|
"issues": "https://github.com/pestphp/pest/issues",
|
||||||
"source": "https://github.com/pestphp/pest/tree/v2.35.0"
|
"source": "https://github.com/pestphp/pest/tree/v2.35.1"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -3067,7 +3067,7 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2024-08-02T10:57:29+00:00"
|
"time": "2024-08-20T21:41:50+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "pestphp/pest-plugin",
|
"name": "pestphp/pest-plugin",
|
||||||
@@ -3561,28 +3561,28 @@
|
|||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
|
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
|
||||||
"reference": "09d5f55e54c20f2353a790d09772db3ad577b9ac"
|
"reference": "7e308268858ed6baedc8704a304727d20bc07c77"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/09d5f55e54c20f2353a790d09772db3ad577b9ac",
|
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/7e308268858ed6baedc8704a304727d20bc07c77",
|
||||||
"reference": "09d5f55e54c20f2353a790d09772db3ad577b9ac",
|
"reference": "7e308268858ed6baedc8704a304727d20bc07c77",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"ext-dom": "*",
|
"ext-dom": "*",
|
||||||
"ext-libxml": "*",
|
"ext-libxml": "*",
|
||||||
"ext-xmlwriter": "*",
|
"ext-xmlwriter": "*",
|
||||||
"nikic/php-parser": "^4.18 || ^5.0",
|
"nikic/php-parser": "^4.19.1 || ^5.1.0",
|
||||||
"php": ">=8.1",
|
"php": ">=8.1",
|
||||||
"phpunit/php-file-iterator": "^4.0",
|
"phpunit/php-file-iterator": "^4.1.0",
|
||||||
"phpunit/php-text-template": "^3.0",
|
"phpunit/php-text-template": "^3.0.1",
|
||||||
"sebastian/code-unit-reverse-lookup": "^3.0",
|
"sebastian/code-unit-reverse-lookup": "^3.0.0",
|
||||||
"sebastian/complexity": "^3.0",
|
"sebastian/complexity": "^3.2.0",
|
||||||
"sebastian/environment": "^6.0",
|
"sebastian/environment": "^6.1.0",
|
||||||
"sebastian/lines-of-code": "^2.0",
|
"sebastian/lines-of-code": "^2.0.2",
|
||||||
"sebastian/version": "^4.0",
|
"sebastian/version": "^4.0.1",
|
||||||
"theseer/tokenizer": "^1.2.0"
|
"theseer/tokenizer": "^1.2.3"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^10.1"
|
"phpunit/phpunit": "^10.1"
|
||||||
@@ -3623,7 +3623,7 @@
|
|||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
|
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
|
||||||
"security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy",
|
"security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy",
|
||||||
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1"
|
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.16"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -3631,7 +3631,7 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2024-07-17T05:15:22+00:00"
|
"time": "2024-08-22T04:31:57+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/php-file-iterator",
|
"name": "phpunit/php-file-iterator",
|
||||||
@@ -3985,12 +3985,12 @@
|
|||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/php-fig/log.git",
|
"url": "https://github.com/php-fig/log.git",
|
||||||
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001"
|
"reference": "79dff0b268932c640297f5208d6298f71855c03e"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001",
|
"url": "https://api.github.com/repos/php-fig/log/zipball/79dff0b268932c640297f5208d6298f71855c03e",
|
||||||
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001",
|
"reference": "79dff0b268932c640297f5208d6298f71855c03e",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -4026,9 +4026,9 @@
|
|||||||
"psr-3"
|
"psr-3"
|
||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"source": "https://github.com/php-fig/log/tree/3.0.0"
|
"source": "https://github.com/php-fig/log/tree/3.0.1"
|
||||||
},
|
},
|
||||||
"time": "2021-07-14T16:46:02+00:00"
|
"time": "2024-08-21T13:31:24+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "sebastian/cli-parser",
|
"name": "sebastian/cli-parser",
|
||||||
@@ -5765,7 +5765,7 @@
|
|||||||
"prefer-stable": false,
|
"prefer-stable": false,
|
||||||
"prefer-lowest": false,
|
"prefer-lowest": false,
|
||||||
"platform": {
|
"platform": {
|
||||||
"php": ">=8.0"
|
"php": ">=8.1"
|
||||||
},
|
},
|
||||||
"platform-dev": [],
|
"platform-dev": [],
|
||||||
"plugin-api-version": "2.6.0"
|
"plugin-api-version": "2.6.0"
|
||||||
|
|||||||
+14
-5
@@ -153,7 +153,7 @@ public function add(
|
|||||||
$this->add(
|
$this->add(
|
||||||
$item['id'],
|
$item['id'],
|
||||||
$item['name'],
|
$item['name'],
|
||||||
$item['price'],
|
isset($item['price']) ? $item['price'] : null,
|
||||||
$item['quantity'],
|
$item['quantity'],
|
||||||
Helpers::issetAndHasValueOrAssignDefault($item['attributes'], []),
|
Helpers::issetAndHasValueOrAssignDefault($item['attributes'], []),
|
||||||
Helpers::issetAndHasValueOrAssignDefault($item['conditions'], []),
|
Helpers::issetAndHasValueOrAssignDefault($item['conditions'], []),
|
||||||
@@ -164,7 +164,7 @@ public function add(
|
|||||||
$this->add(
|
$this->add(
|
||||||
$id['id'],
|
$id['id'],
|
||||||
$id['name'],
|
$id['name'],
|
||||||
$id['price'],
|
isset($id['price']) ? $id['price'] : null,
|
||||||
$id['quantity'],
|
$id['quantity'],
|
||||||
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], []),
|
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], []),
|
||||||
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], []),
|
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], []),
|
||||||
@@ -698,15 +698,24 @@ protected function validate($item)
|
|||||||
{
|
{
|
||||||
$rules = [
|
$rules = [
|
||||||
'id' => 'required',
|
'id' => 'required',
|
||||||
'price' => 'required|numeric',
|
|
||||||
'quantity' => 'required|numeric|min:0.1',
|
|
||||||
'name' => 'required',
|
'name' => 'required',
|
||||||
|
'quantity' => 'required|numeric|min:0.1',
|
||||||
];
|
];
|
||||||
|
|
||||||
$validator = CartItemValidator::make($item, $rules);
|
$validator = CartItemValidator::make($item, $rules);
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
throw new InvalidItemException($validator->messages()->first());
|
if ($validator->errors()->first('id') === 'validation.required') {
|
||||||
|
throw new InvalidItemException('The id field is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($validator->errors()->first('name') === 'validation.required') {
|
||||||
|
throw new InvalidItemException('The name field is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($validator->errors()->first('quantity') === 'validation.min.numeric') {
|
||||||
|
throw new InvalidItemException('The quantity must be at least 0.1.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class Helpers
|
|||||||
/**
|
/**
|
||||||
* Normalize prices
|
* Normalize prices
|
||||||
*/
|
*/
|
||||||
public static function normalizePrice($price): float
|
public static function normalizePrice($price): ?float
|
||||||
{
|
{
|
||||||
return (is_string($price)) ? floatval($price) : $price;
|
return (is_string($price)) ? floatval($price) : $price;
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-15
@@ -49,6 +49,19 @@
|
|||||||
expect($this->cart->getTotal())->toEqual(0, 'Total should be 0');
|
expect($this->cart->getTotal())->toEqual(0, 'Total should be 0');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('can add an item an array with no price', function () {
|
||||||
|
$this->cart->add([
|
||||||
|
'id' => 455,
|
||||||
|
'name' => 'Sample Item',
|
||||||
|
'quantity' => 2,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||||
|
expect($this->cart->getContent()->count())->toEqual(1, 'Cart content should be 1');
|
||||||
|
expect($this->cart->getContent()->first()['id'])->toEqual(455, 'Item added has ID of 455 so first content ID should be 455');
|
||||||
|
expect($this->cart->getTotal())->toEqual(0, 'Total should be 0');
|
||||||
|
});
|
||||||
|
|
||||||
test('can add an item without attributes', function () {
|
test('can add an item without attributes', function () {
|
||||||
$item = [
|
$item = [
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
@@ -283,6 +296,24 @@
|
|||||||
|
|
||||||
expect($item['quantity'])->toEqual(9.9, 'Item quantity should be 9.9');
|
expect($item['quantity'])->toEqual(9.9, 'Item quantity should be 9.9');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should throw an exception when no quantity is provided', function () {
|
||||||
|
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||||
|
$this->expectExceptionMessage('The quantity must be at least 0.1.');
|
||||||
|
$this->cart->add(455, 'Sample Item', 100.99, 0, []);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should throw an exception when no id is provided', function () {
|
||||||
|
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||||
|
$this->expectExceptionMessage('The id field is required.');
|
||||||
|
$this->cart->add('', 'Sample Item', 100.99, 2, []);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should throw an exception when no name is provided', function () {
|
||||||
|
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||||
|
$this->expectExceptionMessage('The name field is required.');
|
||||||
|
$this->cart->add(523, '', 100.99, 2, []);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('cart items attributes', function () {
|
test('cart items attributes', function () {
|
||||||
@@ -542,21 +573,6 @@
|
|||||||
expect($item['quantity'])->toEqual(3, 'Item quantity of with item ID of 456 should now be reduced to 2');
|
expect($item['quantity'])->toEqual(3, 'Item quantity of with item ID of 456 should now be reduced to 2');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should throw exception when provided invalid values scenario one', function () {
|
|
||||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
|
||||||
$this->cart->add(455, 'Sample Item', 100.99, 0, []);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should throw exception when provided invalid values scenario two', function () {
|
|
||||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
|
||||||
$this->cart->add('', 'Sample Item', 100.99, 2, []);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should throw exception when provided invalid values scenario three', function () {
|
|
||||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
|
||||||
$this->cart->add(523, '', 100.99, 2, []);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('clearing cart', function () {
|
test('clearing cart', function () {
|
||||||
$items = [
|
$items = [
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in New Issue
Block a user