Skip to content

Commit

Permalink
unit test per generazione notifiche ordini aperti e chiusi
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed May 15, 2024
1 parent 9f985a5 commit 3c27fba
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
4 changes: 3 additions & 1 deletion code/app/Console/Commands/CloseOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;

use App\Jobs\NotifyClosedOrder;
use App\Order;
Expand All @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion code/app/Jobs/NotifyNewOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion code/app/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions code/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="MAIL_MAILER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MODEL_CACHE_ENABLED" value="true"/>
Expand Down
70 changes: 63 additions & 7 deletions code/tests/Services/OrdersServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,39 +39,56 @@ 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,
));
]);
}

/*
Creazione Ordine
*/
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());

Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 3c27fba

Please sign in to comment.