7

I don't understand how mock this : $requestStack->getCurrentRequest()->getContent()

there are 2 methods : getCurrentRequest()->getContent() and it return a json object (POST Response)

I use symfony with RequestStack class.

The entire code is

class MessageReceivedService
{
    protected $message;

    public function __construct(RequestStack $requestStack)
    {
        $this->message = json_decode($requestStack->getCurrentRequest()->getContent());
    }

    public function getMessage()
    {
        return $this->message;
    }
}
6
  • You do work in your constructor. This is a bad thing, it is better to delay this until the information is really needed. That way you only spend time for things that really need to be done. Commented Apr 28, 2016 at 22:25
  • Thanks @Sven for your expertise, so for you I should use setter and not constructor ? The methods in this service don't work if I don't have the json response of the Api. Thanks Commented Apr 29, 2016 at 6:32
  • In that case you could create a method parseResponse which is called before any other method is executed (in the methods themselves) and caches the returned value. Commented Apr 29, 2016 at 7:59
  • Thanks, Could you give me a example ? If I understand, I create one method who called in first in each method of my service ? Or I must create event system ? Thanks Commented Apr 30, 2016 at 7:39
  • You are passing the $requestStack. Store it. Only start decoding the json from it when getMessage is called. It basically is shifting the work from the constructor to the method that needs the info that is not created in the constructor anymore. If more than one place needs that info, create a private function that returns the data (and maybe stores it if re-creating it is expensive). Commented May 1, 2016 at 23:40

1 Answer 1

22

You don't actually have to mock the RequestStack class, since it's basically a container.

If you want to test with the RequestStack, you could do something like this:

<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class MessageReceivedServiceTest extends \PHPUnit_Framework_TestCase
{
    public function test()
    {
        $request = new Request([], [], [], [], [], [], [], json_encode([
            'foo' => 'bar'
        ]));

        $requestStack = new RequestStack();
        $requestStack->push($request);

        // Do your tests
    }
}

When you call currentRequest on the $requestStack variable, it should return the $request variable.

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

2 Comments

Thanks but, normally in unit test, we don't have to use new Class(), no ?
I usually only really mock things that I can't really test (fast, which is a requirement for unit tests). For example: database queries, network requests or classes with a lot of other dependencies. For a simple container class like RequestStack, I'd just use the actual class since it does nothing really exciting.

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.