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