Update: laravel 8 to 12, php 7.3 to php 8.3, Jenkinsfile, Unit/Feature testing, vapor, docker

This commit is contained in:
Dillon Ngo
2026-04-03 06:59:39 +08:00
parent 58de1017d7
commit b34b7c19d6
142 changed files with 3334 additions and 1101 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ class CreateBookingTest extends DuskTestCase
$modal->click('#confirm-booking');
})
->pause(10000);
//Assert if booking created successfully
//A S S E R T if booking created successfully
});
}
@@ -0,0 +1,86 @@
<?php
namespace Tests\Feature\Accounts;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Hash;
use PHPUnit\Framework\Attributes\Group;
use App\Models\User;
use Tests\TestCase;
#[Group('unit')]
#[Group('segments')]
#[Group('services')]
#[Group('ok_to_run')]
class AuthenticationTest extends TestCase
{
// use DatabaseTransactions;
/**
* Test user can successfully authenticate with valid credentials.
*
* @return void
*/
public function test_user_can_authenticate_with_valid_credentials()
{
// A R R A N G E
$email = 'test_user_can_authenticate_with_valid_credentials@example.com';
$user = User::where('email', $email)->first();
if (!$user) {
$user = new User();
$user->email = $email;
}
$user->name = 'test_user_can_authenticate_with_valid_credentials';
$user->password = Hash::make('password123');
$user->type = \App\Classes\ValueObjects\Constants\RoleTypes::ADMIN;
$user->status = 1;
$user->save();
// A C T: Hit the login attempt API
$response = $this->postJson('/api/v1/account/authentication/login/attempt', [
'email' => $email,
'password' => 'password123',
]);
// A S S E R T: Expect 200 OK and a JSON payload structure
$response->assertStatus(200)
->assertJsonStructure([
'payload' => [
'access_token',
'redirect_url'
]
]);
}
/**
* Test user cannot authenticate with invalid password.
*
* @return void
*/
public function test_user_cannot_authenticate_with_invalid_credentials()
{
// A R R A N G E
$email = 'test_user_cannot_authenticate_with_invalid_credentials@example.com';
$user = User::where('email', $email)->first();
if (!$user) {
$user = new User();
$user->email = $email;
}
$user->name = 'test_user_cannot_authenticate_with_invalid_credentials';
$user->password = Hash::make('password123');
$user->type = \App\Classes\ValueObjects\Constants\RoleTypes::ADMIN;
$user->status = 1;
$user->save();
// A C T
$response = $this->postJson('/api/v1/account/authentication/login/attempt', [
'email' => $email,
'password' => 'wrongpassword',
]);
// A S S E R T
// Unprocessable Entity or Unauthorized depending on application logic
// 400/401/422 are common. Let's assert it just doesn't succeed (not 200)
$this->assertNotEquals(200, $response->getStatusCode());
}
}
@@ -0,0 +1,90 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\Modules\Segments\ControllersLogic\CreateSegmentLogic;
use App\Classes\Modules\Segments\DataTransferObjects\SegmentObject;
use App\Classes\Modules\Segments\Services\CreatesSegment;
use App\Classes\Modules\Segments\Standards\Rules\CanCreateSegment;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use App\Classes\Jobs\UserRiskAnalysis;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class CreateSegmentLogicTest extends TestCase
{
//cief todo: 137 - User of type 3 should not be able to update or create segment, CanCreateSegment set, this TODO should be okay
// use DatabaseTransactions;
/**
* Test CreateSegmentLogic execution.
*
* @return void
*/
public function test_create_segment_logic_execution()
{
// A R R A N G E
Bus::fake([UserRiskAnalysis::class]);
$email = 'test_create_segment_logic_execution@example.com';
$user = User::where('email', $email)->first();
if (!$user) {
$user = new User();
$user->name = 'test_create_segment_logic_execution';
$user->email = $email;
$user->password = bcrypt('password');
$user->save();
}
$this->actingAs($user);
// Mock CanCreateSegment to pass
$canCreateSegment = Mockery::mock(CanCreateSegment::class);
$canCreateSegment->shouldReceive('passes')->once()->andReturn(true);
// Mock CreatesSegment to return a Segment model
$segment = new Segment();
$segment->id = 1;
$segment->name = 'test_create_segment_logic_execution';
$segment->type = SegmentConstants::STANDARD_SEGMENT;
$createsSegment = Mockery::mock(CreatesSegment::class);
$createsSegment->shouldReceive('execute')
->once()
->with(Mockery::type(SegmentObject::class))
->andReturn($segment);
$logic = new CreateSegmentLogic($canCreateSegment, $createsSegment);
$request = Request::create('/api/segments', 'POST', [
'name' => 'test_create_segment_logic_execution',
'type' => SegmentConstants::STANDARD_SEGMENT
]);
$request->headers->set('captcha-token', 'fake-token');
// A C T
$response = $logic->execute($request);
// A S S E R T
$this->assertEquals(200, $response->getStatusCode());
$data = $response->getData(true);
$this->assertEquals('Created Segment Successful', $data['title']);
$this->assertEquals('test_create_segment_logic_execution', $data['payload']['data']['name']);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}
@@ -0,0 +1,94 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\Modules\Segments\ControllersLogic\DeleteSegmentLogic;
use App\Classes\Modules\Segments\Services\DeletesSegment;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\Modules\Segments\Standards\Rules\CanDeleteSegment;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use App\Classes\Jobs\UserRiskAnalysis;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class DeleteSegmentLogicTest extends TestCase
{
// use DatabaseTransactions;
/**
* Test DeleteSegmentLogic execution.
*
* @return void
*/
public function test_delete_segment_logic_execution()
{
// A R R A N G E
Bus::fake([UserRiskAnalysis::class]);
$email = 'test_delete_segment_logic_execution@example.com';
$user = User::where('email', $email)->first();
if (!$user) {
$user = new User();
$user->name = 'test_delete_segment_logic_execution';
$user->email = $email;
$user->password = bcrypt('password');
$user->save();
}
$this->actingAs($user);
$segment = new Segment();
$segment->id = 1;
$segment->name = 'test_delete_segment_logic_execution';
// Mock FetchesSegment
$fetchesSegment = Mockery::mock(FetchesSegment::class);
$fetchesSegment->shouldReceive('execute')
->once()
->with(['id' => 1])
->andReturn($segment);
// Mock CanDeleteSegment
$canDeleteSegment = Mockery::mock(CanDeleteSegment::class);
$canDeleteSegment->shouldReceive('passes')->once()->andReturn(true);
// Mock DeletesSegment
$deletesSegment = Mockery::mock(DeletesSegment::class);
$deletesSegment->shouldReceive('execute')
->once()
->with($segment)
->andReturn([]);
$logic = new DeleteSegmentLogic($canDeleteSegment, $deletesSegment, $fetchesSegment);
$request = Request::create('/api/segments/1', 'DELETE');
$request->headers->set('captcha-token', 'fake-token');
$request->setRouteResolver(function () {
$route = Mockery::mock(\Illuminate\Routing\Route::class);
$route->shouldReceive('parameter')->with('id', null)->andReturn(1);
return $route;
});
// A C T
$response = $logic->execute($request);
// A S S E R T
$this->assertEquals(200, $response->getStatusCode());
$data = $response->getData(true);
$this->assertEquals('Delete Segment Successful', $data['title']);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}
@@ -0,0 +1,85 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\Modules\Segments\ControllersLogic\FetchConstantLogic;
use App\Classes\Modules\Segments\Services\FetchesConstant;
use App\Models\SegmentConstant;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use App\Classes\Jobs\UserRiskAnalysis;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class FetchConstantLogicTest extends TestCase
{
// use DatabaseTransactions;
/**
* Test FetchConstantLogic execution.
*
* @return void
*/
public function test_fetch_constant_logic_execution()
{
// A R R A N G E
Bus::fake([UserRiskAnalysis::class]);
$email = 'test_fetch_constant_logic_execution@example.com';
$user = User::where('email', $email)->first();
if (!$user) {
$user = new User();
$user->name = 'test_fetch_constant_logic_execution';
$user->email = $email;
$user->password = bcrypt('password');
$user->save();
}
$this->actingAs($user);
$constant = new SegmentConstant();
$constant->id = 1;
$constant->name = 'test_fetch_constant_logic_execution';
$constant->reference = 'test_fetch_constant_logic_execution';
$constant->detail = (object) ['foo' => 'bar'];
// Mock FetchesConstant
$fetchesConstant = Mockery::mock(FetchesConstant::class);
$fetchesConstant->shouldReceive('execute')
->once()
->with(['segment_id' => 1, 'reference' => 'test_fetch_constant_logic_execution'])
->andReturn($constant);
$logic = new FetchConstantLogic($fetchesConstant);
$request = Request::create('/api/segments/1/constants/test_fetch_constant_logic_execution', 'GET');
$request->headers->set('captcha-token', 'fake-token');
$request->setRouteResolver(function () {
$route = Mockery::mock(\Illuminate\Routing\Route::class);
$route->shouldReceive('parameter')->with('id', null)->andReturn(1);
$route->shouldReceive('parameter')->with('reference', null)->andReturn('test_fetch_constant_logic_execution');
return $route;
});
// A C T
$response = $logic->execute($request);
// A S S E R T
$this->assertEquals(200, $response->getStatusCode());
$data = $response->getData(true);
$this->assertEquals('Fetch Segment Constant Successful', $data['title']);
$this->assertEquals('test_fetch_constant_logic_execution', $data['payload']['data']['reference']);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}
@@ -0,0 +1,87 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\Modules\Segments\ControllersLogic\FetchSegmentLogic;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\Modules\Segments\Standards\Rules\CanFetchSegment;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use App\Classes\Jobs\UserRiskAnalysis;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class FetchSegmentLogicTest extends TestCase
{
// use DatabaseTransactions;
/**
* Test FetchSegmentLogic execution.
*
* @return void
*/
public function test_fetch_segment_logic_execution()
{
// A R R A N G E
Bus::fake([UserRiskAnalysis::class]);
$email = 'test_fetch_segment_logic_execution@example.com';
$user = User::where('email', $email)->first();
if (!$user) {
$user = new User();
$user->name = 'test_fetch_segment_logic_execution';
$user->email = $email;
$user->password = bcrypt('password');
$user->save();
}
$this->actingAs($user);
$segment = new Segment();
$segment->id = 1;
$segment->name = 'test_fetch_segment_logic_execution';
// Mock CanFetchSegment
$canFetchSegment = Mockery::mock(CanFetchSegment::class);
$canFetchSegment->shouldReceive('passes')->once()->andReturn(true);
// Mock FetchesSegment
$fetchesSegment = Mockery::mock(FetchesSegment::class);
$fetchesSegment->shouldReceive('execute')
->once()
->with(['id' => 1])
->andReturn($segment);
$logic = new FetchSegmentLogic($canFetchSegment, $fetchesSegment);
$request = Request::create('/api/segments/1', 'GET');
$request->headers->set('captcha-token', 'fake-token');
$request->setRouteResolver(function () {
$route = Mockery::mock(\Illuminate\Routing\Route::class);
$route->shouldReceive('parameter')->with('id', null)->andReturn(1);
return $route;
});
// A C T
$response = $logic->execute($request);
// A S S E R T
$this->assertEquals(200, $response->getStatusCode());
$data = $response->getData(true);
$this->assertEquals('Retrieved Segment Successful', $data['title']);
$this->assertEquals('test_fetch_segment_logic_execution', $data['payload']['data']['name']);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}
@@ -0,0 +1,88 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\Modules\Segments\ControllersLogic\ListSegmentLogic;
use App\Classes\Modules\Segments\Services\ListsSegments;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use App\Classes\Jobs\UserRiskAnalysis;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class ListSegmentLogicTest extends TestCase
{
// use DatabaseTransactions;
/**
* Test ListSegmentLogic execution.
*
* @return void
*/
public function test_list_segment_logic_execution()
{
// A R R A N G E
Bus::fake([UserRiskAnalysis::class]);
$email = 'test_list_segment_logic_execution@example.com';
$user = User::where('email', $email)->first();
if (!$user) {
$user = new User();
$user->name = 'test_list_segment_logic_execution';
$user->email = $email;
$user->password = bcrypt('password');
$user->save();
}
$this->actingAs($user);
$s1 = new Segment();
$s1->id = 1;
$s1->name = 'test_list_segment_logic_execution_1';
$s2 = new Segment();
$s2->id = 2;
$s2->name = 'test_list_segment_logic_execution_2';
$segments = new Collection([$s1, $s2]);
// Mock ListsSegments
$listsSegments = Mockery::mock(ListsSegments::class);
$listsSegments->shouldReceive('deserializeFilters')
->once()
->with(null)
->andReturn([]);
$listsSegments->shouldReceive('execute')
->once()
->with([])
->andReturn($segments);
$logic = new ListSegmentLogic($listsSegments);
$request = Request::create('/api/segments', 'GET');
$request->headers->set('captcha-token', 'fake-token');
// A C T
$response = $logic->execute($request);
// A S S E R T
$this->assertEquals(200, $response->getStatusCode());
$data = $response->getData(true);
$this->assertEquals('Retrieved Segment Successful', $data['title']);
$this->assertCount(2, $data['payload']['data']);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}
@@ -0,0 +1,129 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Models\Segment;
use App\Models\SegmentConstant;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use PHPUnit\Framework\Attributes\Group;
use Spatie\Permission\Models\Permission;
use Tests\TestCase;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class UpdateConstantLogic2Test extends TestCase
{
/** @var User */
private $admin;
/** @var Segment */
private $segment;
protected function setUp(): void
{
parent::setUp();
// A R R A N G E
// Create an admin user for authentication
$email = 'admin_update_constant_execution@example.com';
$this->admin = User::where('email', $email)->first();
if (!$this->admin) {
$this->admin = new User();
$this->admin->email = $email;
$this->admin->name = 'Admin Update Constant Execution';
$this->admin->password = Hash::make('password');
$this->admin->type = RoleTypes::ADMIN;
$this->admin->status = 1;
$this->admin->save();
}
// Ensure user has necessary permissions
$permission = Permission::firstOrCreate(['name' => 'edit segment_constant', 'guard_name' => 'api']);
$this->admin->givePermissionTo($permission);
$permission = Permission::firstOrCreate(['name' => 'add segment_constant', 'guard_name' => 'api']);
$this->admin->givePermissionTo($permission);
// Create a test segment
$this->segment = new Segment();
$this->segment->name = 'Update Constant Execution Segment';
$this->segment->save();
}
/**
* Test updating an existing segment constant without Mockery.
*
* @return void
*/
public function test_it_can_update_existing_constant()
{
// A R R A N G E
$reference = 'execution_test_ref';
$detail = ['foo' => 'bar'];
$constant = new SegmentConstant();
$constant->segment_id = $this->segment->id;
$constant->name = 'Old Name';
$constant->reference = $reference;
$constant->detail = (object) ['old' => 'data'];
$constant->save();
$payload = [
'name' => 'Updated Name',
'reference' => $reference,
'detail' => $detail
];
// A C T
$response = $this->actingAs($this->admin, 'api')
->putJson("/api/v1/segment/{$this->segment->id}/constant/update", $payload);
// A S S E R T
$response->assertStatus(200);
$response->assertJsonPath('payload.data.name', 'Updated Name');
$response->assertJsonPath('payload.data.reference', $reference);
$this->assertDatabaseHas('segment_constants', [
'segment_id' => $this->segment->id,
'reference' => $reference,
'name' => 'Updated Name'
]);
$updatedConstant = SegmentConstant::where('reference', $reference)->first();
$this->assertEquals((object) $detail, $updatedConstant->detail);
}
/**
* Test creating a constant if it doesn't exist during update.
*
* @return void
*/
public function test_it_creates_constant_if_not_exists()
{
// A R R A N G E
$reference = 'new_execution_test_ref';
$detail = ['new' => 'data'];
$payload = [
'name' => 'New Constant',
'reference' => $reference,
'detail' => $detail
];
// A C T
$response = $this->actingAs($this->admin, 'api')
->putJson("/api/v1/segment/{$this->segment->id}/constant/update", $payload);
// A S S E R T
$response->assertStatus(200);
$this->assertDatabaseHas('segment_constants', [
'segment_id' => $this->segment->id,
'reference' => $reference,
'name' => 'New Constant'
]);
}
}
@@ -0,0 +1,182 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\Exceptions\ResourceNotFoundException;
use App\Classes\Modules\Segments\ControllersLogic\UpdateConstantLogic;
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Classes\Modules\Segments\Services\CreatesConstant;
use App\Classes\Modules\Segments\Services\FetchesConstant;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\Modules\Segments\Services\UpdatesConstant;
use App\Classes\Modules\Segments\Standards\Rules\CanCreateConstant;
use App\Classes\Modules\Segments\Standards\Rules\CanUpdateConstant;
use App\Models\Segment;
use App\Models\SegmentConstant;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use App\Classes\Jobs\UserRiskAnalysis;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class UpdateConstantLogicTest extends TestCase
{
//cief todo: 137 - User of type 3 should not be able to update or create constants
// use DatabaseTransactions;
private $user;
private $segment;
protected function setUp(): void
{
parent::setUp();
$email = 'UpdateConstantLogicTest@example.com';
$this->user = User::where('email', $email)->first();
if (!$this->user) {
$this->user = new User();
$this->user->name = 'UpdateConstantLogicTest';
$this->user->email = $email;
$this->user->password = bcrypt('password');
$this->user->save();
}
$this->actingAs($this->user);
$this->segment = new Segment();
$this->segment->id = 1;
$this->segment->name = 'UpdateConstantLogicTest_Segment';
}
/**
* Test UpdateConstantLogic execution for updating existing constant.
*
* @return void
*/
public function test_update_constant_logic_updates_existing()
{
// A R R A N G E
Bus::fake([UserRiskAnalysis::class]);
$constant = new SegmentConstant();
$constant->id = 10;
$constant->segment_id = 1;
$constant->name = 'test_update_constant_logic_updates_existing';
$constant->reference = 'test_update_constant_logic_updates_existing';
$constant->detail = (object) ['foo' => 'bar'];
$fetchesSegment = Mockery::mock(FetchesSegment::class);
$fetchesSegment->shouldReceive('execute')->with(['id' => 1])->andReturn($this->segment);
$fetchesConstant = Mockery::mock(FetchesConstant::class);
$fetchesConstant->shouldReceive('execute')->with(['segment_id' => 1, 'reference' => 'test_update_constant_logic_updates_existing'])->andReturn($constant);
$canUpdateConstant = Mockery::mock(CanUpdateConstant::class);
$canUpdateConstant->shouldReceive('passes')->once()->andReturn(true);
$updatesConstant = Mockery::mock(UpdatesConstant::class);
$updatesConstant->shouldReceive('execute')->once()->andReturn($constant);
$canCreateConstant = Mockery::mock(CanCreateConstant::class);
$createsConstant = Mockery::mock(CreatesConstant::class);
$logic = new UpdateConstantLogic(
$canUpdateConstant,
$updatesConstant,
$fetchesSegment,
$fetchesConstant,
$canCreateConstant,
$createsConstant
);
$request = Request::create('/api/segments/1/constants', 'POST', [
'name' => 'test_update_constant_logic_updates_existing_NEW',
'reference' => 'test_update_constant_logic_updates_existing',
'detail' => ['new' => 'data']
]);
$request->headers->set('captcha-token', 'fake-token');
$request->setRouteResolver(function () {
$route = Mockery::mock(\Illuminate\Routing\Route::class);
$route->shouldReceive('parameter')->with('id', null)->andReturn(1);
return $route;
});
// A C T
$response = $logic->execute($request);
// A S S E R T
$this->assertEquals(200, $response->getStatusCode());
$data = $response->getData(true);
$this->assertEquals('Updated Segment Constant Successful', $data['title']);
}
/**
* Test UpdateConstantLogic execution for creating new constant if not found.
*
* @return void
*/
public function test_update_constant_logic_creates_new_if_not_found()
{
// A R R A N G E
Bus::fake([UserRiskAnalysis::class]);
$fetchesSegment = Mockery::mock(FetchesSegment::class);
$fetchesSegment->shouldReceive('execute')->andReturn($this->segment);
$fetchesConstant = Mockery::mock(FetchesConstant::class);
$fetchesConstant->shouldReceive('execute')->andThrow(new ResourceNotFoundException());
$canUpdateConstant = Mockery::mock(CanUpdateConstant::class);
$updatesConstant = Mockery::mock(UpdatesConstant::class);
$canCreateConstant = Mockery::mock(CanCreateConstant::class);
$canCreateConstant->shouldReceive('passes')->once()->andReturn(true);
$constant = new SegmentConstant();
$constant->id = 11;
$constant->name = 'test_update_constant_logic_creates_new_if_not_found';
$createsConstant = Mockery::mock(CreatesConstant::class);
$createsConstant->shouldReceive('execute')->once()->andReturn($constant);
$logic = new UpdateConstantLogic(
$canUpdateConstant,
$updatesConstant,
$fetchesSegment,
$fetchesConstant,
$canCreateConstant,
$createsConstant
);
$request = Request::create('/api/segments/1/constants', 'POST', [
'name' => 'test_update_constant_logic_creates_new_if_not_found',
'reference' => 'test_update_constant_logic_creates_new_if_not_found',
'detail' => [],
]);
$request->headers->set('captcha-token', 'fake-token');
$request->setRouteResolver(function () {
$route = Mockery::mock(\Illuminate\Routing\Route::class);
$route->shouldReceive('parameter')->with('id', null)->andReturn(1);
return $route;
});
// A C T
$response = $logic->execute($request);
// A S S E R T
$this->assertEquals(200, $response->getStatusCode());
$data = $response->getData(true);
$this->assertEquals('Updated Segment Constant Successful', $data['title']);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}
@@ -0,0 +1,80 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use PHPUnit\Framework\Attributes\Group;
use Spatie\Permission\Models\Permission;
use Tests\TestCase;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class UpdateSegmentLogic2Test extends TestCase
{
/** @var User */
private $admin;
/** @var Segment */
private $segment;
protected function setUp(): void
{
parent::setUp();
// A R R A N G E
// Create an admin user for authentication
$email = 'admin_update_segment_execution@example.com';
$this->admin = User::where('email', $email)->first();
if (!$this->admin) {
$this->admin = new User();
$this->admin->email = $email;
$this->admin->name = 'Admin Update Segment Execution';
$this->admin->password = Hash::make('password');
$this->admin->type = RoleTypes::ADMIN;
$this->admin->status = 1;
$this->admin->save();
}
// Grant permission to resolve 403 Forbidden
$permission = Permission::firstOrCreate(['name' => 'edit segment', 'guard_name' => 'api']);
$this->admin->givePermissionTo($permission);
// Create a test segment
$this->segment = new Segment();
$this->segment->name = 'Update Segment Execution Segment';
$this->segment->save();
}
/**
* Test updating a segment without Mockery.
*
* @return void
*/
public function test_it_can_update_segment()
{
// A R R A N G E
$newName = 'Updated Segment Name';
$payload = [
'name' => $newName
];
// A C T
$response = $this->actingAs($this->admin, 'api')
->putJson("/api/v1/segment/update/{$this->segment->id}", $payload);
// A S S E R T
$response->assertStatus(200);
$response->assertJsonPath('payload.data.name', $newName);
$this->assertDatabaseHas('segments', [
'id' => $this->segment->id,
'name' => $newName
]);
}
}
@@ -0,0 +1,96 @@
<?php
namespace Tests\Feature\Modules\Segments\ControllersLogic;
use App\Classes\Modules\Segments\ControllersLogic\UpdateSegmentLogic;
use App\Classes\Modules\Segments\DataTransferObjects\SegmentObject;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\Modules\Segments\Services\UpdatesSegment;
use App\Classes\Modules\Segments\Standards\Rules\CanUpdateSegment;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Http\Request;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;
#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class UpdateSegmentLogicTest extends TestCase
{
// use DatabaseTransactions;
/**
* Test UpdateSegmentLogic execution.
*
* @return void
*/
public function test_update_segment_logic_execution()
{
// A R R A N G E
$email = 'test_update_segment_logic_execution@example.com';
$user = User::where('email', $email)->first();
if (!$user) {
$user = new User();
$user->name = 'test_update_segment_logic_execution';
$user->email = $email;
$user->type = 1;
$user->password = bcrypt('password');
$user->save();
}
$this->actingAs($user);
$segment = new Segment();
$segment->id = 1;
$segment->name = 'test_update_segment_logic_execution';
// Mock FetchesSegment
$fetchesSegment = Mockery::mock(FetchesSegment::class);
$fetchesSegment->shouldReceive('execute')
->once()
->with(['id' => 1])
->andReturn($segment);
// Mock CanUpdateSegment
$canUpdateSegment = Mockery::mock(CanUpdateSegment::class);
$canUpdateSegment->shouldReceive('passes')
->once()
->with(Mockery::type(SegmentObject::class))
->andReturn(true);
// Mock UpdatesSegment
$updatesSegment = Mockery::mock(UpdatesSegment::class);
$updatesSegment->shouldReceive('execute')
->once()
->with($segment, Mockery::type(SegmentObject::class))
->andReturn($segment);
$logic = new UpdateSegmentLogic($canUpdateSegment, $updatesSegment, $fetchesSegment);
$request = Request::create('/api/segments/1', 'PUT', [
'name' => 'test_update_segment_logic_execution_NEW'
]);
$request->setRouteResolver(function () {
$route = Mockery::mock(\Illuminate\Routing\Route::class);
$route->shouldReceive('parameter')->with('id', null)->andReturn(1);
return $route;
});
// A C T
$response = $logic->execute($request);
// A S S E R T
$this->assertEquals(200, $response->getStatusCode());
$data = $response->getData(true);
$this->assertEquals('Updated Segment Successful', $data['title']);
}
protected function tearDown(): void
{
parent::tearDown();
Mockery::close();
}
}
@@ -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,
]);
}
}