We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to assert view data from Tests

The current response returned from Phalcon\Http\Client\Response is contains body, header, statusCode etc but how can we test the data passed into the view file?

One solution can be asserting the html but that doesn't work with paginated views.

Any suggestions?

you can use an event to do this

Is there a way to send it back with the response directly?

This is my test:

public function homepage_renders_propoerly()
{
  $provider = Request::getProvider();
  $provider->setBaseUri('https://localhost:8000/');

  $response = $provider->get('/');

  $this->assertEquals(200, $response->header->statusCode);
}

Just reached half of the solution:

If I use the application handle method instead of the provider(got this from the phalcon incubator code) then the di is getting updated and the view params can be fetched

$response = $this->application->handle('/');

$this->assertContains('test variable', $this->di->getView()->getParamsToView());

However I'm not able to create a request as post, put or patch. Any solutions for that? Is this approach correct?



1.3k
Accepted
answer

I'm able to make it a post request by overriding the $_SERVER variables.

so the solution is:

$_SERVER['REQUEST_METHOD'] = 'POST';
$response = $this->application->handle('/');

$this->assertContains('test variable', $this->di->getView()->getParamsToView());