From 2ee29ac0ca01bc9e1fbe5a2a142e5b71b87dc298 Mon Sep 17 00:00:00 2001 From: shjchen Date: Sun, 28 Jan 2018 22:54:25 +0800 Subject: [PATCH] 1. add user_id and guard to databasestorage table. 2. update database test --- ...2_16_154403_create_shopping_cart_table.php | 5 ++-- phpunit.php | 1 + phpunit.xml | 2 ++ src/ServiceProvider.php | 23 ++++++++++++------- src/Storage/DatabaseStorage.php | 8 ++++++- tests/DatabaseCartTest.php | 22 +++++++++++++++++- tests/User.php | 18 +++++++++++++++ ...154403_create_shopping_cart_test_table.php | 7 +++--- 8 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 tests/User.php diff --git a/migrations/2016_12_16_154403_create_shopping_cart_table.php b/migrations/2016_12_16_154403_create_shopping_cart_table.php index 4dcfe33..7005739 100644 --- a/migrations/2016_12_16_154403_create_shopping_cart_table.php +++ b/migrations/2016_12_16_154403_create_shopping_cart_table.php @@ -23,6 +23,8 @@ public function up() Schema::create('shopping_cart', function (Blueprint $table) { $table->string('key'); $table->string('__raw_id'); + $table->string('guard')->nullable(); + $table->integer('user_id')->nullable(); $table->integer('id'); $table->string('name'); $table->integer('qty'); @@ -32,8 +34,7 @@ public function up() $table->string('type')->nullable(); $table->string('status')->nullable(); $table->text('attributes')->nullable(); - $table->index('key'); - $table->index('__raw_id'); + $table->primary(['key', '__raw_id']); $table->nullableTimestamps(); }); } diff --git a/phpunit.php b/phpunit.php index b5e561d..715bce6 100644 --- a/phpunit.php +++ b/phpunit.php @@ -15,3 +15,4 @@ require __DIR__.'/tests/CartTestTrait.php'; require __DIR__.'/tests/Product.php'; +require __DIR__.'/tests/User.php'; diff --git a/phpunit.xml b/phpunit.xml index 2655c54..0cd9421 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -24,6 +24,8 @@ ./src/Exception.php ./src/Facade.php ./src/ServiceProvider.php + ./src/Storage/DatabaseStorage.php + ./src/Storage/SessionStorage.php diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 6ea490b..40b2413 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -11,7 +11,7 @@ namespace iBrand\Shoppingcart; -use iBrand\Shoppingcart\Storage\DatabaseStorage; +use iBrand\Shoppingcart\Storage\SessionStorage; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; /** @@ -52,6 +52,15 @@ public function register() ); $this->app->singleton(Cart::class, function ($app) { + $storage = config('ibrand.cart.storage'); + + $cart = new Cart(new $storage(), $app['events']); + + if (SessionStorage::class == $storage) { + return $cart; + } + + //The below code is used of database storage $currentGuard = null; $user = null; @@ -63,13 +72,11 @@ public function register() } } - $storage = config('ibrand.cart.storage'); - - $cart = new Cart(new $storage(), $app['events']); - - if (DatabaseStorage::class == $storage && $user) { - $cart->name($currentGuard.$user->id); - $cart->saveFromSession(); + if ($user) { + //The cart name like `cart.{guard}.{user_id}`: cart.api.1 + $cart->name($currentGuard.'.'.$user->id); + }else{ + throw new Exception('Invalid auth.'); } return $cart; diff --git a/src/Storage/DatabaseStorage.php b/src/Storage/DatabaseStorage.php index 208f5dd..bfae51f 100644 --- a/src/Storage/DatabaseStorage.php +++ b/src/Storage/DatabaseStorage.php @@ -44,13 +44,19 @@ public function set($key, $values) $rawIds = $values->pluck('__raw_id')->toArray(); + //Delete the data that has been removed from cart. DB::table($this->table)->whereNotIn('__raw_id', $rawIds)->where('key', $key)->delete(); + $keys = explode('.', $key); + + $userId = end($keys); + $guard = prev($keys); + $values = $values->toArray(); foreach ($values as $value) { $item = array_only($value, $this->filed); $attr = json_encode(array_except($value, $this->filed)); - $insert = array_merge($item, ['attributes' => $attr, 'key' => $key]); + $insert = array_merge($item, ['attributes' => $attr, 'key' => $key, 'guard' => $guard, 'user_id' => $userId]); if (DB::table($this->table)->where(['key' => $key, '__raw_id' => $item['__raw_id']])->first()) { DB::table($this->table)->where(['key' => $key, '__raw_id' => $item['__raw_id']]) ->update(array_except($insert, ['key', '__raw_id'])); diff --git a/tests/DatabaseCartTest.php b/tests/DatabaseCartTest.php index 4fd79ef..7231ca8 100644 --- a/tests/DatabaseCartTest.php +++ b/tests/DatabaseCartTest.php @@ -9,6 +9,7 @@ * file that was distributed with this source code. */ +use App\Models\User; use iBrand\ShoppingCart\Cart; use Illuminate\Foundation\Testing\DatabaseMigrations; @@ -32,6 +33,7 @@ protected function getEnvironmentSetUp($app) 'database' => ':memory:', 'prefix' => '', ]); + } protected function loadMigrationsFrom($app) @@ -48,9 +50,27 @@ protected function setUp() { parent::setUp(); // TODO: Change the autogenerated stub + + $user = new User([ + 'id' => 1, + 'name' => 'ibrand' + ]); + + $this->be($user,'api'); + $this->loadMigrationsFrom(__DIR__.'/database'); - //dd($this->app['config']->get('database')); $this->cart = $this->app['cart']; + + } + + /** + * test name(). + */ + public function testName() + { + $this->assertEquals('cart.api.1', $this->cart->getName()); + $this->cart->name('ibrand'); + $this->assertEquals('cart.ibrand', $this->cart->getName()); } } diff --git a/tests/User.php b/tests/User.php new file mode 100644 index 0000000..22f34e5 --- /dev/null +++ b/tests/User.php @@ -0,0 +1,18 @@ +string('key'); $table->string('__raw_id'); + $table->string('guard')->nullable(); + $table->integer('user_id')->nullable(); $table->integer('id'); $table->string('name'); $table->integer('qty'); $table->decimal('price'); $table->decimal('total'); $table->string('__model')->nullable(); - $table->string('type'); + $table->string('type')->nullable(); $table->string('status')->nullable(); $table->text('attributes')->nullable(); - $table->index('key'); - $table->index('__raw_id'); + $table->primary(['key', '__raw_id']); $table->nullableTimestamps(); }); }