B
B
BonBon Slick2018-05-10 18:09:58
symfony
BonBon Slick, 2018-05-10 18:09:58

Create mock Request?

$request = $this->createMock(Request::class);
        $request->request = $this->createMock(ParameterBag::class);
        $request->request->set('mykey', 'myvalue');
        dump($request->request); //параметры пустые

        $filter =  UserFilter::fromRequest($request); // сюда надо передать праметры mykey

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
voronkovich, 2018-05-11
@BonBonSlick

$request = $this->createMock(Request::class);

$request->request = $this->createMock(ParameterBag::class);
$request->request->method('get')->will($this->returnValueMap([
    [ 'key1', null, 'value1' ],
    [ 'key2', null, 'value2' ],
    [ 'key3', null, 'value3' ],
    [ 'key4', null, 'value4' ],
]));

$this->assertEquals('value3', $request->request->get('key3'));

However, in this case, creating a stub, instead of directly using the Request instance, does not justify itself. more code than:
$request = new Request();
$request->request->set('key1', 'value1');
$request->request->set('key2', 'value2');
$request->request->set('key3', 'value3');
$request->request->set('key4', 'value4');

Although, I met people who even mocked for ArrayCollection. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question