mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-20 04:53:56 +00:00
Update: laravel 8 to 12, php 7.3 to php 8.3, Jenkinsfile, Unit/Feature testing, vapor, docker
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Currencies\Services\FetchesCurrency;
|
||||
use App\Classes\Modules\Segments\Services\ConvertsConstantDetailsToResource;
|
||||
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
||||
use App\Http\Resources\CurrencyResource;
|
||||
use App\Models\SegmentConstant;
|
||||
use App\Models\Currency;
|
||||
use App\Models\Country;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
use Mockery;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class ConvertsConstantDetailsToResourceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test it returns detail directly if not SUPPLIER_CURRENCIES.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_returns_detail_directly_if_not_supplier_currencies()
|
||||
{
|
||||
// A R R A N G E
|
||||
$mockFetchesCurrency = Mockery::mock(FetchesCurrency::class);
|
||||
$service = new ConvertsConstantDetailsToResource($mockFetchesCurrency);
|
||||
|
||||
$constant = new SegmentConstant();
|
||||
$constant->reference = 'test_it_returns_detail_directly_if_not_supplier_currencies';
|
||||
$constant->detail = (object) ['foo' => 'bar'];
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($constant);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertEquals($constant->detail, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it returns CurrencyResource if reference is SUPPLIER_CURRENCIES.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_returns_currency_resource_if_reference_is_supplier_currencies()
|
||||
{
|
||||
// A R R A N G E
|
||||
$currencyId = 123;
|
||||
$mockCurrency = new Currency();
|
||||
$mockCurrency->id = $currencyId;
|
||||
$mockCurrency->name = 'test_it_returns_currency_resource_if_reference_is_supplier_currencies';
|
||||
|
||||
$mockFetchesCurrency = Mockery::mock(FetchesCurrency::class);
|
||||
$mockFetchesCurrency->shouldReceive('execute')
|
||||
->with(['id' => $currencyId])
|
||||
->once()
|
||||
->andReturn($mockCurrency);
|
||||
|
||||
$service = new ConvertsConstantDetailsToResource($mockFetchesCurrency);
|
||||
|
||||
$constant = new SegmentConstant();
|
||||
$constant->reference = SegmentConstants::SUPPLIER_CURRENCIES;
|
||||
$constant->detail = (object) ['id' => $currencyId];
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($constant);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(CurrencyResource::class, $result);
|
||||
$this->assertEquals($currencyId, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it returns empty string if SUPPLIER_CURRENCIES but no id in detail.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_returns_empty_if_no_id_in_supplier_currencies()
|
||||
{
|
||||
// A R R A N G E
|
||||
$mockFetchesCurrency = Mockery::mock(FetchesCurrency::class);
|
||||
$service = new ConvertsConstantDetailsToResource($mockFetchesCurrency);
|
||||
|
||||
$constant = new SegmentConstant();
|
||||
$constant->reference = SegmentConstants::SUPPLIER_CURRENCIES;
|
||||
$constant->detail = (object) ['some' => 'other'];
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($constant);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertEquals('', $result);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
|
||||
use App\Classes\Modules\Segments\Services\CreatesConstant;
|
||||
use App\Models\Segment;
|
||||
use App\Models\SegmentConstant;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class CreatesConstantTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can create a segment constant.
|
||||
*
|
||||
* @return void
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function test_it_can_create_a_segment_constant()
|
||||
{
|
||||
// A R R A N G E
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_create_a_segment_constant_SEG';
|
||||
$segment->save();
|
||||
|
||||
$detail = ['key' => 'value'];
|
||||
$object = new ConstantObject('test_it_can_create_a_segment_constant_CON', 'test_it_can_create_a_segment_constant_REF', $detail);
|
||||
$service = new CreatesConstant();
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($segment, $object);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(SegmentConstant::class, $result);
|
||||
$this->assertEquals('test_it_can_create_a_segment_constant_CON', $result->name);
|
||||
$this->assertEquals('test_it_can_create_a_segment_constant_REF', $result->reference);
|
||||
$this->assertEquals((object) $detail, $result->detail);
|
||||
$this->assertEquals($segment->id, $result->segment_id);
|
||||
|
||||
$this->assertDatabaseHas('segment_constants', [
|
||||
'id' => $result->id,
|
||||
'name' => 'test_it_can_create_a_segment_constant_CON',
|
||||
'reference' => 'test_it_can_create_a_segment_constant_REF',
|
||||
'segment_id' => $segment->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\DataTransferObjects\SeasonalSegmentObject;
|
||||
use App\Classes\Modules\Segments\Services\CreatesSeasonalSegment;
|
||||
use App\Models\Company;
|
||||
use App\Models\Segment;
|
||||
use App\Models\SeasonalSegment;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
use Carbon\Carbon;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class CreatesSeasonalSegmentTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can create a seasonal segment.
|
||||
*
|
||||
* @return void
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function test_it_can_create_a_seasonal_segment()
|
||||
{
|
||||
// A R R A N G E
|
||||
$company = new Company();
|
||||
$company->name = 'test_it_can_create_a_seasonal_segment_COMP';
|
||||
$company->reference = 'TEST-CSST';
|
||||
$company->business_type = 1;
|
||||
$company->save();
|
||||
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_create_a_seasonal_segment_SEG';
|
||||
$segment->save();
|
||||
|
||||
$startingOn = Carbon::now()->addDay()->format('Y-m-d H:i:s');
|
||||
$endingOn = Carbon::now()->addMonth()->format('Y-m-d H:i:s');
|
||||
|
||||
$object = new SeasonalSegmentObject(
|
||||
$company->id,
|
||||
$segment->id,
|
||||
$startingOn,
|
||||
$endingOn
|
||||
);
|
||||
|
||||
$service = new CreatesSeasonalSegment();
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($object);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(SeasonalSegment::class, $result);
|
||||
$this->assertEquals($company->id, $result->company_id);
|
||||
$this->assertEquals($segment->id, $result->segment_id);
|
||||
$this->assertEquals($startingOn, $result->starting_on->format('Y-m-d H:i:s'));
|
||||
$this->assertEquals($endingOn, $result->ending_on->format('Y-m-d H:i:s'));
|
||||
|
||||
$this->assertDatabaseHas('seasonal_segment', [
|
||||
'id' => $result->id,
|
||||
'company_id' => $company->id,
|
||||
'segment_id' => $segment->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\DataTransferObjects\SegmentObject;
|
||||
use App\Classes\Modules\Segments\Services\CreatesSegment;
|
||||
use App\Models\Segment;
|
||||
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class CreatesSegmentTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can create a segment.
|
||||
*
|
||||
* @return void
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function test_it_can_create_a_segment()
|
||||
{
|
||||
// A R R A N G E
|
||||
$name = 'test_it_can_create_a_segment';
|
||||
$type = SegmentConstants::STANDARD_SEGMENT;
|
||||
$object = new SegmentObject($name, $type);
|
||||
$service = new CreatesSegment();
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($object);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(Segment::class, $result);
|
||||
$this->assertEquals($name, $result->name);
|
||||
$this->assertEquals($type, $result->type);
|
||||
|
||||
$this->assertDatabaseHas('segments', [
|
||||
'id' => $result->id,
|
||||
'name' => $name,
|
||||
'type' => $type,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\Services\DeletesSegment;
|
||||
use App\Models\Segment;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class DeletesSegmentTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can delete a segment.
|
||||
*
|
||||
* @return void
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function test_it_can_delete_a_segment()
|
||||
{
|
||||
// A R R A N G E
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_delete_a_segment';
|
||||
$segment->save();
|
||||
|
||||
$service = new DeletesSegment();
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($segment);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertIsArray($result);
|
||||
|
||||
// Since Segment uses SoftDeletes, check if it's still in DB but trashed
|
||||
$this->assertSoftDeleted('segments', [
|
||||
'id' => $segment->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\Services\FetchesConstant;
|
||||
use App\Models\Segment;
|
||||
use App\Models\SegmentConstant;
|
||||
use App\Classes\Exceptions\ResourceNotFoundException;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class FetchesConstantTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can fetch a segment constant by ID.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_can_fetch_a_segment_constant_by_id()
|
||||
{
|
||||
// A R R A N G E
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_fetch_a_segment_constant_by_id_SEG';
|
||||
$segment->save();
|
||||
|
||||
$constant = new SegmentConstant();
|
||||
$constant->segment_id = $segment->id;
|
||||
$constant->name = 'test_it_can_fetch_a_segment_constant_by_id_CON';
|
||||
$constant->reference = 'test_it_can_fetch_a_segment_constant_by_id_REF';
|
||||
$constant->detail = (object) ['foo' => 'bar'];
|
||||
$constant->save();
|
||||
|
||||
$service = new FetchesConstant(new SegmentConstant());
|
||||
|
||||
// A C T
|
||||
$result = $service->execute(['id' => $constant->id]);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(SegmentConstant::class, $result);
|
||||
$this->assertEquals($constant->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it can fetch a segment constant by reference.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_can_fetch_a_segment_constant_by_reference()
|
||||
{
|
||||
// A R R A N G E
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_fetch_a_segment_constant_by_reference_SEG';
|
||||
$segment->save();
|
||||
|
||||
$constant = new SegmentConstant();
|
||||
$constant->segment_id = $segment->id;
|
||||
$constant->name = 'test_it_can_fetch_a_segment_constant_by_reference_CON';
|
||||
$constant->reference = 'test_it_can_fetch_a_segment_constant_by_reference_REF';
|
||||
$constant->detail = (object) ['foo' => 'bar'];
|
||||
$constant->save();
|
||||
|
||||
$service = new FetchesConstant(new SegmentConstant());
|
||||
|
||||
// A C T
|
||||
$result = $service->execute(['reference' => 'test_it_can_fetch_a_segment_constant_by_reference_REF']);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(SegmentConstant::class, $result);
|
||||
$this->assertEquals($constant->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it throws exception if constant not found.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_throws_exception_if_constant_not_found()
|
||||
{
|
||||
// A R R A N G E
|
||||
$service = new FetchesConstant(new SegmentConstant());
|
||||
|
||||
// A S S E R T
|
||||
$this->expectException(ResourceNotFoundException::class);
|
||||
|
||||
// A C T
|
||||
$service->execute(['id' => 99999]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\Services\FetchesSegment;
|
||||
use App\Models\Segment;
|
||||
use App\Classes\Exceptions\ResourceNotFoundException;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class FetchesSegmentTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can fetch a segment by ID.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_can_fetch_a_segment_by_id()
|
||||
{
|
||||
// A R R A N G E
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_fetch_a_segment_by_id';
|
||||
$segment->save();
|
||||
|
||||
$service = new FetchesSegment(new Segment());
|
||||
|
||||
// A C T
|
||||
$result = $service->execute(['id' => $segment->id]);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(Segment::class, $result);
|
||||
$this->assertEquals($segment->id, $result->id);
|
||||
$this->assertEquals('test_it_can_fetch_a_segment_by_id', $result->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it can fetch a segment by name.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_can_fetch_a_segment_by_name()
|
||||
{
|
||||
// A R R A N G E
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_fetch_a_segment_by_name';
|
||||
$segment->save();
|
||||
|
||||
$service = new FetchesSegment(new Segment());
|
||||
|
||||
// A C T
|
||||
$result = $service->execute(['name' => 'test_it_can_fetch_a_segment_by_name']);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(Segment::class, $result);
|
||||
$this->assertEquals($segment->id, $result->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it throws exception if segment not found.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_throws_exception_if_segment_not_found()
|
||||
{
|
||||
// A R R A N G E
|
||||
$service = new FetchesSegment(new Segment());
|
||||
|
||||
// A S S E R T
|
||||
$this->expectException(ResourceNotFoundException::class);
|
||||
|
||||
// A C T
|
||||
$service->execute(['id' => 99999]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\Services\ListsConstants;
|
||||
use App\Models\Segment;
|
||||
use App\Models\SegmentConstant;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class ListsConstantsTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can list segment constants.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_can_list_segment_constants()
|
||||
{
|
||||
// A R R A N G E
|
||||
SegmentConstant::query()->delete(); // Clear existing
|
||||
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_list_segment_constants_SEG';
|
||||
$segment->save();
|
||||
|
||||
$c1 = new SegmentConstant();
|
||||
$c1->segment_id = $segment->id;
|
||||
$c1->name = 'test_it_can_list_segment_constants_A';
|
||||
$c1->reference = 'test_it_can_list_segment_constants_REF_A';
|
||||
$c1->detail = (object) ['foo' => 'bar'];
|
||||
$c1->save();
|
||||
|
||||
$c2 = new SegmentConstant();
|
||||
$c2->segment_id = $segment->id;
|
||||
$c2->name = 'test_it_can_list_segment_constants_B';
|
||||
$c2->reference = 'test_it_can_list_segment_constants_REF_B';
|
||||
$c2->detail = (object) ['foo' => 'bar'];
|
||||
$c2->save();
|
||||
|
||||
$service = new ListsConstants(new SegmentConstant());
|
||||
|
||||
// A C T
|
||||
$result = $service->execute();
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(Collection::class, $result);
|
||||
$this->assertCount(2, $result);
|
||||
$this->assertTrue($result->contains('name', 'test_it_can_list_segment_constants_A'));
|
||||
$this->assertTrue($result->contains('name', 'test_it_can_list_segment_constants_B'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it can list segment constants with filters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_can_filter_segment_constants_by_segment_id()
|
||||
{
|
||||
// A R R A N G E
|
||||
SegmentConstant::query()->delete();
|
||||
|
||||
$segment1 = new Segment();
|
||||
$segment1->name = 'test_it_can_filter_segment_constants_by_segment_id_SEG1';
|
||||
$segment1->save();
|
||||
|
||||
$segment2 = new Segment();
|
||||
$segment2->name = 'test_it_can_filter_segment_constants_by_segment_id_SEG2';
|
||||
$segment2->save();
|
||||
|
||||
$c1 = new SegmentConstant();
|
||||
$c1->segment_id = $segment1->id;
|
||||
$c1->name = 'test_it_can_filter_segment_constants_by_segment_id_C1';
|
||||
$c1->reference = 'test_it_can_filter_segment_constants_by_segment_id_REF1';
|
||||
$c1->detail = (object) ['foo' => 'bar'];
|
||||
$c1->save();
|
||||
|
||||
$c2 = new SegmentConstant();
|
||||
$c2->segment_id = $segment2->id;
|
||||
$c2->name = 'test_it_can_filter_segment_constants_by_segment_id_C2';
|
||||
$c2->reference = 'test_it_can_filter_segment_constants_by_segment_id_REF2';
|
||||
$c2->detail = (object) ['foo' => 'bar'];
|
||||
$c2->save();
|
||||
|
||||
$service = new ListsConstants(new SegmentConstant());
|
||||
|
||||
// A C T
|
||||
$result = $service->execute(['segment_id' => $segment1->id]);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals('test_it_can_filter_segment_constants_by_segment_id_REF1', $result->first()->reference);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\Services\ListsSegments;
|
||||
use App\Models\Segment;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class ListsSegmentsTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can list segments.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_can_list_segments()
|
||||
{
|
||||
// A R R A N G E
|
||||
Segment::query()->delete(); // Clear existing
|
||||
|
||||
$segment1 = new Segment();
|
||||
$segment1->name = 'test_it_can_list_segments_1';
|
||||
$segment1->save();
|
||||
|
||||
$segment2 = new Segment();
|
||||
$segment2->name = 'test_it_can_list_segments_2';
|
||||
$segment2->save();
|
||||
|
||||
$service = new ListsSegments(new Segment());
|
||||
|
||||
// A C T
|
||||
$result = $service->execute();
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(Collection::class, $result);
|
||||
$this->assertCount(2, $result);
|
||||
$this->assertTrue($result->contains('name', 'test_it_can_list_segments_1'));
|
||||
$this->assertTrue($result->contains('name', 'test_it_can_list_segments_2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it can list segments with pagination.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_it_can_paginate_segments()
|
||||
{
|
||||
// A R R A N G E
|
||||
Segment::query()->delete();
|
||||
|
||||
for ($i = 1; $i <= 5; $i++) {
|
||||
$s = new Segment();
|
||||
$s->name = "test_it_can_paginate_segments_$i";
|
||||
$s->save();
|
||||
}
|
||||
|
||||
$service = new ListsSegments(new Segment());
|
||||
|
||||
// A C T
|
||||
$result = $service->execute(['per_page' => 2]);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(LengthAwarePaginator::class, $result);
|
||||
$this->assertEquals(2, $result->perPage());
|
||||
$this->assertEquals(5, $result->total());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
|
||||
use App\Classes\Modules\Segments\Services\UpdatesConstant;
|
||||
use App\Models\Segment;
|
||||
use App\Models\SegmentConstant;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class UpdatesConstantTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can update a segment constant.
|
||||
*
|
||||
* @return void
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function test_it_can_update_a_segment_constant()
|
||||
{
|
||||
// A R R A N G E
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_update_a_segment_constant_SEG';
|
||||
$segment->save();
|
||||
|
||||
$constant = new SegmentConstant();
|
||||
$constant->segment_id = $segment->id;
|
||||
$constant->name = 'test_it_can_update_a_segment_constant_OLD';
|
||||
$constant->reference = 'old-reference';
|
||||
$constant->detail = (object) ['old' => 'detail'];
|
||||
$constant->save();
|
||||
|
||||
$newDetail = ['new' => 'detail'];
|
||||
$object = new ConstantObject('test_it_can_update_a_segment_constant_NEW', 'test_it_can_update_a_segment_constant_REF', $newDetail);
|
||||
$service = new UpdatesConstant();
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($constant, $object);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(SegmentConstant::class, $result);
|
||||
$this->assertEquals('test_it_can_update_a_segment_constant_NEW', $result->name);
|
||||
$this->assertEquals('test_it_can_update_a_segment_constant_REF', $result->reference);
|
||||
$this->assertEquals((object) $newDetail, $result->detail);
|
||||
$this->assertEquals($constant->id, $result->id);
|
||||
|
||||
$this->assertDatabaseHas('segment_constants', [
|
||||
'id' => $constant->id,
|
||||
'name' => 'test_it_can_update_a_segment_constant_NEW',
|
||||
'reference' => 'test_it_can_update_a_segment_constant_REF',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Modules\Segments\Services;
|
||||
|
||||
use App\Classes\Modules\Segments\DataTransferObjects\SegmentObject;
|
||||
use App\Classes\Modules\Segments\Services\UpdatesSegment;
|
||||
use App\Models\Segment;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('unit')]
|
||||
#[Group('segments')]
|
||||
#[Group('services')]
|
||||
#[Group('ok_to_run')]
|
||||
class UpdatesSegmentTest extends TestCase
|
||||
{
|
||||
// use DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Test it can update a segment.
|
||||
*
|
||||
* @return void
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function test_it_can_update_a_segment()
|
||||
{
|
||||
// A R R A N G E
|
||||
$segment = new Segment();
|
||||
$segment->name = 'test_it_can_update_a_segment_ORIGINAL';
|
||||
$segment->save();
|
||||
|
||||
$newName = 'test_it_can_update_a_segment_UPDATED';
|
||||
$object = new SegmentObject($newName);
|
||||
$service = new UpdatesSegment();
|
||||
|
||||
// A C T
|
||||
$result = $service->execute($segment, $object);
|
||||
|
||||
// A S S E R T
|
||||
$this->assertInstanceOf(Segment::class, $result);
|
||||
$this->assertEquals($newName, $result->name);
|
||||
$this->assertEquals($segment->id, $result->id);
|
||||
|
||||
$this->assertDatabaseHas('segments', [
|
||||
'id' => $segment->id,
|
||||
'name' => $newName,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user