Files
exchange-2.0/tests/Feature/Modules/Segments/ControllersLogic/CreateSegmentLogicTest.php
T

91 lines
2.9 KiB
PHP

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