1

I would like to test one controller that it has:

$deviceGet = strtolower($request->query->get('device'));

My question is: How can I mock it to test in my controller?

class SetDeviceListenerTest extends \PHPUnit_Framework_TestCase {


     /**
     * @test
     * @dataProvider deviceGetCases
     */
public function shouldSetRequestDeviceWithDeviceGetOnMasterRequest(
    ?string $device
): void {
    $request = $this->getRequestMock();

    $request->expects($this->once())
        ->method('get')
        ->with('device')
        ->willReturn($device);


    /* @var Request $request */
    $request->server = new ServerBag();
    $request->headers = new HeaderBag();

    $event = $this->getEventMock();
    $event->expects($this->once())
        ->method('isMasterRequest')
        ->willReturn(true);

It return error because

 $request->expects($this->once())
        ->method('get')
        ->with('device')
        ->willReturn($device);

fails

thank you!!

1 Answer 1

6

I found the answer myself (I just needed a friend):

$request->query= new ParameterBag(['device'=>$device]);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.