From 3c27fbab4a05b98af4d7aaeb5351384540e5528e Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Wed, 15 May 2024 09:47:59 +0200 Subject: [PATCH] unit test per generazione notifiche ordini aperti e chiusi --- code/app/Console/Commands/CloseOrders.php | 4 +- code/app/Jobs/NotifyNewOrder.php | 8 ++- code/app/Order.php | 2 +- code/phpunit.xml | 1 + code/tests/Services/OrdersServiceTest.php | 70 ++++++++++++++++++++--- 5 files changed, 75 insertions(+), 10 deletions(-) diff --git a/code/app/Console/Commands/CloseOrders.php b/code/app/Console/Commands/CloseOrders.php index b8e6ebf1..7e558b30 100644 --- a/code/app/Console/Commands/CloseOrders.php +++ b/code/app/Console/Commands/CloseOrders.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use Carbon\Carbon; use App\Jobs\NotifyClosedOrder; use App\Order; @@ -14,7 +15,8 @@ class CloseOrders extends Command public function handle() { - $orders = Order::withoutGlobalScopes()->where('status', 'open')->where('end', '<', date('Y-m-d'))->get(); + $today = Carbon::today()->format('Y-m-d'); + $orders = Order::withoutGlobalScopes()->where('status', 'open')->where('end', '<', $today)->get(); $closed = []; foreach($orders as $order) { diff --git a/code/app/Jobs/NotifyNewOrder.php b/code/app/Jobs/NotifyNewOrder.php index fd38af68..953ebe3c 100644 --- a/code/app/Jobs/NotifyNewOrder.php +++ b/code/app/Jobs/NotifyNewOrder.php @@ -26,7 +26,13 @@ public function handle() { $order = Order::find($this->order_id); - if (is_null($order) || is_null($order->first_notify) == false) { + if (is_null($order)) { + \Log::warning('Richiesta notifica creazione ordine non esistente'); + return; + } + + if (is_null($order->first_notify) == false) { + \Log::warning('Richiesta notifica creazione ordine giĆ  inoltrata'); return; } diff --git a/code/app/Order.php b/code/app/Order.php index d3fd0f0b..33026404 100644 --- a/code/app/Order.php +++ b/code/app/Order.php @@ -443,7 +443,7 @@ public function notifiableUsers($gas) { $order = $this; - if ($gas->getConfig('notify_all_new_orders')) { + if ($gas->notify_all_new_orders) { $query_users = User::whereNull('parent_id'); } else { diff --git a/code/phpunit.xml b/code/phpunit.xml index 6cb067b3..8cc1967e 100644 --- a/code/phpunit.xml +++ b/code/phpunit.xml @@ -9,6 +9,7 @@ + diff --git a/code/tests/Services/OrdersServiceTest.php b/code/tests/Services/OrdersServiceTest.php index df414f71..c2c8a563 100644 --- a/code/tests/Services/OrdersServiceTest.php +++ b/code/tests/Services/OrdersServiceTest.php @@ -6,11 +6,13 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use Artisan; -use Bus; +use Illuminate\Support\Facades\Bus; +use Illuminate\Support\Facades\Notification; use Illuminate\Database\Eloquent\ModelNotFoundException; use App\Exceptions\AuthException; +use App\Gas; use App\User; use App\Delivery; use App\Booking; @@ -37,9 +39,9 @@ public function testFailsToStore() $this->expectException(AuthException::class); $this->actingAs($this->userWithNoPerms); - app()->make('OrdersService')->store(array( + app()->make('OrdersService')->store([ 'supplier_id' => $this->order->supplier_id, - )); + ]); } /* @@ -47,29 +49,46 @@ public function testFailsToStore() */ public function testStore() { - Bus::fake(); + Notification::fake(); + $this->gas->setConfig('notify_all_new_orders', '1'); + $this->userWithNoPerms->addContact('email', fake()->email()); + $this->userWithBasePerms->addContact('email', fake()->email()); + + $this->nextRound(); + + $this->userReferrer = User::find($this->userReferrer->id); + $this->gas = Gas::find($this->gas->id); + app()->make('GlobalScopeHub')->setGas($this->gas); $this->actingAs($this->userReferrer); + $this->assertEquals($this->gas->id, $this->userReferrer->gas->id); $start = date('Y-m-d'); $end = date('Y-m-d', strtotime('+20 days')); $shipping = date('Y-m-d', strtotime('+30 days')); - $aggregate = app()->make('OrdersService')->store(array( + $aggregate = app()->make('OrdersService')->store([ 'supplier_id' => $this->order->supplier_id, 'comment' => 'Commento di prova', 'start' => printableDate($start), 'end' => printableDate($end), 'shipping' => printableDate($shipping), 'status' => 'open', - )); + ]); - Bus::assertDispatched(\App\Jobs\NotifyNewOrder::class); $this->assertEquals(1, $aggregate->orders->count()); $this->assertTrue($aggregate->isActive()); $this->assertTrue($aggregate->isRunning()); $this->assertFalse($aggregate->canShip()); + foreach($aggregate->orders as $order) { + $notifiable = $order->notifiableUsers($this->gas); + $this->assertTrue($notifiable->count() > 0); + foreach($notifiable as $not) { + Notification::assertSentTo([$not], \App\Notifications\NewOrderNotification::class); + } + } + $this->actingAs($this->userWithShippingPerms); $this->assertTrue($aggregate->canShip()); @@ -139,6 +158,43 @@ public function testUpdate() $this->assertEquals($order->end, $this->order->end); } + /* + Chiusura ordini automatica + */ + public function testAutoClose() + { + Notification::fake(); + $this->populateOrder($this->order); + + $this->nextRound(); + + $booking = $this->order->bookings()->first(); + + $booking->user->addContact('email', fake()->email()); + $this->userReferrer->addContact('email', fake()->email()); + + $this->gas->setConfig('auto_referent_order_summary', '1'); + $this->gas->setConfig('auto_user_order_summary', '1'); + $this->order->supplier->notify_on_close_enabled = 'shipping_summary'; + $this->order->supplier->addContact('email', fake()->email()); + $this->order->supplier->save(); + + $this->nextRound(); + + $this->travel(6)->days(); + + $this->nextRound(); + + Artisan::call('close:orders'); + + $order = app()->make('OrdersService')->show($this->order->id); + $this->assertEquals('closed', $order->status); + + Notification::assertSentTo([$order->supplier], \App\Notifications\SupplierOrderShipping::class); + Notification::assertSentTo([$this->userReferrer], \App\Notifications\ClosedOrdersNotification::class); + Notification::assertSentTo([$booking->user], \App\Notifications\BookingNotification::class); + } + /* Cambio stato */