Skip to content

Commit

Permalink
ulteriori unit test per aggregazioni
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed May 4, 2024
1 parent 7bb8172 commit 831b06e
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 96 deletions.
33 changes: 33 additions & 0 deletions code/app/Circle.php
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);
}
}
2 changes: 1 addition & 1 deletion code/app/Observers/UserObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function createdNormal($user)
foreach($groups as $group) {
$circle = $group->circles()->where('is_default', true)->first();
if ($circle) {
$circles[] = $circle;
$circles[] = $circle->id;
}
}

Expand Down
2 changes: 2 additions & 0 deletions code/app/Providers/ServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ private function services(): array
*/
return [
\App\Services\BookingsService::class,
\App\Services\CirclesService::class,
\App\Services\DatesService::class,
\App\Services\DynamicBookingsService::class,
\App\Services\FastBookingsService::class,
\App\Services\GroupsService::class,
\App\Services\InvoicesService::class,
\App\Services\ModifiersService::class,
\App\Services\ModifierTypesService::class,
Expand Down
2 changes: 1 addition & 1 deletion code/app/Services/BookingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function handleBookingUpdate($request, $user, $order, $target_user, $deli
$booking = $this->readBooking($request, $order, $booking, $delivering);

if ($booking) {
$booking->circles()->sync($request['circles']);
$booking->circles()->sync($request['circles'] ?? []);

if ($delivering) {
BookingDelivered::dispatch($booking, $request['action'], $user);
Expand Down
21 changes: 21 additions & 0 deletions code/app/Services/CirclesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\DB;

use App\User;
use App\Circle;
use App\Group;

Expand All @@ -29,10 +30,22 @@ public function store(array $request)

if ($group->circles()->count() == 0) {
$c->is_default = true;
$was_first = true;
}
else {
$was_first = false;
}

$c->save();

if ($was_first) {
if ($group->context == 'user') {
foreach(User::all() as $user) {
$user->circles()->attach($c->id);
}
}
}

return $c;
}

Expand Down Expand Up @@ -67,6 +80,14 @@ public function destroy($id)
$new_default = $other->first();
$new_default->is_default = true;
$new_default->save();

$users = User::whereHas('circles', function($query) use ($c) {
$query->where('circles.id', $c->id);
})->get();

foreach($users as $user) {
$user->circles()->attach($new_default->id);
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions code/app/Services/GroupsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ public function update($id, array $request)

$g = Group::findOrFail($id);
$this->setIfSet($g, $request, 'name');
$this->setIfSet($g, $request, 'context');
$this->setIfSet($g, $request, 'context', 'user');

$context = $request['context'] ?? 'user';
switch($context) {
switch($g->context) {
case 'user':
$this->setIfSet($g, $request, 'cardinality');
$this->boolIfSet($g, $request, 'filters_orders');
Expand Down
244 changes: 244 additions & 0 deletions code/tests/Features/AggregationsTest.php
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());
}
}
4 changes: 2 additions & 2 deletions code/tests/Services/ModifierTypesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public function testStore()
{
$this->actingAs($this->userWithAdminPerm);

$type = app()->make('ModifierTypesService')->store(array(
$type = app()->make('ModifierTypesService')->store([
'name' => 'Donazione',
'classes' => ['App\Booking'],
));
]);

$this->assertTrue($type->exists);

Expand Down
Loading

0 comments on commit 831b06e

Please sign in to comment.