-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ulteriori unit test per aggregazioni
- Loading branch information
Showing
12 changed files
with
333 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
use App\Models\Concerns\TracksUpdater; | ||
use App\Models\Concerns\ModifiableTrait; | ||
use App\Events\SluggableCreating; | ||
|
||
class Circle extends Model | ||
{ | ||
use GASModel, SluggableID, TracksUpdater, ModifiableTrait; | ||
|
||
public $incrementing = false; | ||
protected $keyType = 'string'; | ||
|
||
protected $dispatchesEvents = [ | ||
'creating' => SluggableCreating::class | ||
]; | ||
|
||
protected static function boot() | ||
{ | ||
parent::boot(); | ||
static::initTrackingEvents(); | ||
} | ||
|
||
public function group(): BelongsTo | ||
{ | ||
return $this->belongsTo(Group::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
<?php | ||
|
||
namespace Tests\Services; | ||
|
||
use Tests\TestCase; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
|
||
class AggregationsTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
private function createBasicGroup($params) | ||
{ | ||
$this->actingAs($this->userAdmin); | ||
|
||
$groups_service = app()->make('GroupsService'); | ||
$circles_service = app()->make('CirclesService'); | ||
|
||
$group = $groups_service->store(['name' => 'Gruppo Test']); | ||
$groups_service->update($group->id, array_merge(['name' => 'Gruppo Test'], $params)); | ||
|
||
$this->nextRound(); | ||
|
||
$circles_service->store([ | ||
'name' => 'Cerchio Test 1', | ||
'group_id' => $group->id, | ||
]); | ||
|
||
$this->nextRound(); | ||
|
||
$circles_service->store([ | ||
'name' => 'Cerchio Test 2', | ||
'group_id' => $group->id, | ||
]); | ||
|
||
$this->nextRound(); | ||
|
||
return $group; | ||
} | ||
|
||
public function testCreate() | ||
{ | ||
$group = $this->createBasicGroup([ | ||
'context' => 'user', | ||
'cardinality' => 'single', | ||
'filters_orders' => true, | ||
]); | ||
|
||
$this->assertFalse(is_null($group)); | ||
$this->assertEquals('Gruppo Test', $group->name); | ||
$this->assertEquals(2, $group->circles()->count()); | ||
$this->assertEquals(1, $this->userAdmin->circles()->count()); | ||
} | ||
|
||
private function attachModifier($circle, $amount) | ||
{ | ||
$this->actingAs($this->userAdmin); | ||
|
||
app()->make('ModifierTypesService')->update('spese-trasporto', [ | ||
'classes' => ['App\Booking', 'App\Product', 'App\Circle'], | ||
]); | ||
|
||
$this->nextRound(); | ||
|
||
$test_shipping_value = 10; | ||
$modifiers = $circle->applicableModificationTypes(); | ||
$modifier = null; | ||
|
||
foreach ($modifiers as $mod) { | ||
if ($mod->id == 'spese-trasporto') { | ||
$mod = $circle->modifiers()->where('modifier_type_id', $mod->id)->first(); | ||
app()->make('ModifiersService')->update($mod->id, [ | ||
'value' => 'absolute', | ||
'arithmetic' => 'sum', | ||
'scale' => 'minor', | ||
'applies_type' => 'none', | ||
'applies_target' => 'booking', | ||
'distribution_type' => 'none', | ||
'simplified_amount' => $test_shipping_value, | ||
]); | ||
|
||
$modifier = $mod; | ||
break; | ||
} | ||
} | ||
|
||
$this->assertNotNull($modifier); | ||
|
||
$this->nextRound(); | ||
return $modifier; | ||
} | ||
|
||
public function testUserModifiers() | ||
{ | ||
$group = $this->createBasicGroup([ | ||
'context' => 'user', | ||
'cardinality' => 'single', | ||
'filters_orders' => false, | ||
]); | ||
|
||
$right_circle = $group->circles()->where('is_default', false)->first(); | ||
$wrong_circle = $group->circles()->where('circles.id', '!=', $right_circle->id)->first(); | ||
|
||
$order = $this->initOrder(null); | ||
$this->populateOrder($order); | ||
|
||
$this->nextRound(); | ||
|
||
$target_user = $this->users->first(); | ||
$target_user->circles()->sync([$right_circle->id]); | ||
|
||
$test_shipping_value = 10; | ||
$mod = $this->attachModifier($right_circle, $test_shipping_value); | ||
|
||
$order = app()->make('OrdersService')->show($order->id); | ||
$redux = $order->aggregate->reduxData(); | ||
$this->assertNotEquals($redux->price, 0.0); | ||
$checked = false; | ||
|
||
foreach($order->bookings as $booking) { | ||
$mods = $booking->applyModifiers($redux, true); | ||
|
||
if ($booking->user->id != $target_user->id) { | ||
$this->assertEquals($mods->count(), 0); | ||
} | ||
else { | ||
$this->assertEquals($mods->count(), 1); | ||
|
||
foreach($mods as $m) { | ||
$this->assertEquals($m->effective_amount, $test_shipping_value); | ||
$this->assertEquals($m->modifier_id, $mod->id); | ||
} | ||
|
||
$checked = true; | ||
} | ||
} | ||
|
||
$this->assertTrue($checked); | ||
} | ||
|
||
public function testBookingModifiers() | ||
{ | ||
$group = $this->createBasicGroup([ | ||
'context' => 'booking', | ||
]); | ||
|
||
$right_circle = $group->circles()->where('is_default', false)->first(); | ||
$wrong_circle = $group->circles()->where('circles.id', '!=', $right_circle->id)->first(); | ||
|
||
$order = $this->initOrder(null); | ||
$this->populateOrder($order); | ||
|
||
$this->nextRound(); | ||
|
||
$order = app()->make('OrdersService')->show($order->id); | ||
|
||
do { | ||
$target_bookings = []; | ||
|
||
foreach($order->bookings as $booking) { | ||
if (rand() % 2 == 0) { | ||
$booking->circles()->sync([$right_circle->id]); | ||
$target_bookings[] = $booking->id; | ||
} | ||
else { | ||
$booking->circles()->sync([$wrong_circle->id]); | ||
} | ||
} | ||
} while(empty($target_bookings) && count($target_bookings) != $order->bookings->count()); | ||
|
||
$test_shipping_value = 10; | ||
$mod = $this->attachModifier($right_circle, $test_shipping_value); | ||
|
||
$order = app()->make('OrdersService')->show($order->id); | ||
$redux = $order->aggregate->reduxData(); | ||
$this->assertNotEquals($redux->price, 0.0); | ||
$checked = false; | ||
|
||
foreach($order->bookings as $booking) { | ||
$mods = $booking->applyModifiers($redux, true); | ||
|
||
if (in_array($booking->id, $target_bookings) == false) { | ||
$this->assertEquals($mods->count(), 0); | ||
} | ||
else { | ||
$this->assertEquals($mods->count(), 1); | ||
|
||
foreach($mods as $m) { | ||
$this->assertEquals($m->effective_amount, $test_shipping_value); | ||
$this->assertEquals($m->modifier_id, $mod->id); | ||
} | ||
|
||
$checked = true; | ||
} | ||
} | ||
|
||
$this->assertTrue($checked); | ||
} | ||
|
||
public function testAssociateOrder() | ||
{ | ||
$group = $this->createBasicGroup([ | ||
'context' => 'user', | ||
'cardinality' => 'single', | ||
'filters_orders' => true, | ||
]); | ||
|
||
$right_circle = $group->circles()->first(); | ||
$wrong_circle = $group->circles()->where('circles.id', '!=', $right_circle->id)->first(); | ||
|
||
$order = $this->initOrder(null); | ||
|
||
$this->actingAs($this->userReferrer); | ||
app()->make('OrdersService')->update($order->id, [ | ||
'circles' => [$right_circle->id], | ||
]); | ||
|
||
$this->nextRound(); | ||
|
||
$order = app()->make('OrdersService')->show($order->id); | ||
$this->assertEquals(1, $order->circles()->count()); | ||
$this->assertEquals($right_circle->id, $order->circles()->first()->id); | ||
|
||
$this->nextRound(); | ||
|
||
$user_yes = $this->createRoleAndUser($this->gas, 'supplier.book'); | ||
$user_yes->circles()->sync([$right_circle->id]); | ||
|
||
$user_no = $this->createRoleAndUser($this->gas, 'supplier.book'); | ||
$user_no->circles()->sync([$wrong_circle->id]); | ||
|
||
$this->nextRound(); | ||
|
||
$this->actingAs($user_yes); | ||
$orders = getOrdersByStatus($user_yes, 'open'); | ||
$this->assertEquals(1, $orders->count()); | ||
|
||
$this->nextRound(); | ||
|
||
$this->actingAs($user_no); | ||
$orders = getOrdersByStatus($user_no, 'open'); | ||
$this->assertEquals(0, $orders->count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.