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
@@ -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();
}
}