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