I've been using Laravel and Symfony for a while and I'm very happy with the testing with DomCrawler. Now at work I'm using CakePHP 3, and I'm not comfortable with the integration testing system, it's like this:
$this->get('/miweb/add-page/rates/2');
$this->assertResponseOk();
$this->assertResponseContains( 'precio' );
$this->post('/miweb/add-page/rates/2', $data);
$this->assertResponseContains( '30€' );
$this->assertResponseContains( '90€' );
I've been looking for a way to integrate DomCrawler in the testing system, so that I could use $crawler->filter('body > p');
, $form->submit()
and all the functionality, but I've accomplished nothing. Has anyone done this? Is it possible?
What I've done so far is this:
<?php
class BaseIntegrationTestCase extends IntegrationTestCase
{
public function get( $url )
{
$result = parent::get($url);
$crawler = new Crawler();
$crawler->addContent($this->_response);
return $crawler;
}
public function post($url, $data = [])
{
$result = parent::post($url, $data);
$crawler = new Crawler();
$crawler->addContent($this->_response);
return $crawler;
}
}
And then extend my Class in the tests, but it doesn't work...
Finally I've put my class to work I'll leave it here just in case.
The main issue was that DomCrawler needs an absolute URI, but if you pass that URI to CakePHP it won't work very well, so here is my class
<?php
namespace App\Test\TestCase\Controller;
use Cake\TestSuite\IntegrationTestCase;
use Symfony\Component\DomCrawler\Crawler;
class BaseIntegrationTestCase extends IntegrationTestCase
{
public function get( $url )
{
$result = parent::get($url);
$url = (stripos($url, 'http://') !== false) ? $url : 'http://localhost' . $url ;
$crawler = new Crawler( null, $url );
$crawler->addContent($this->_response);
return $crawler;
}
public function post($url, $data = [])
{
$parsed = parse_url($url);
$result = parent::post($parsed['path'], $data);
$url = (stripos($url, 'http://') !== false) ? $url : 'http://localhost' . $url ;
$crawler = new Crawler( null, $url );
$crawler->addContent($this->_response);
return $crawler;
}
public function submit( \Symfony\Component\DomCrawler\Form $form )
{
return $this->post( $form->getUri(), $form->getPhpValues() );
}
}
Usage example:
<?php
use App\Test\TestCase\Controller\BaseIntegrationTestCase;
class AdminsControllerTest extends BaseIntegrationTestCase
{
function testSomething()
{
$data = ['hours' => array (
[
'day' => 'Lunes',
'from' =>'10',
'to' => '22'
],
[
'day' => 'Martes',
'from' =>'10',
'to' => '22'
],
[
'day' => 'Miercoles',
'from' =>'10',
'to' => '22'
],
[
'day' => 'Jueves',
'from' =>'10',
'to' => '22'
],
[
'day' => 'Viernes',
'from' =>'10',
'to' => '23'
],
)
];
$crawler = $this->get('/miweb/add-page/TimeTable/2');
$this->assertResponseOk();
$this->assertResponseContains( 'horario' );
$form = $crawler->selectButton('Guardar')->form();
$form->setValues( $data );
$crawler = $this->submit($form);
// $this->post('/miweb/add-page/TimeTable/2', $data);
$this->assertResponseContains( 'Jueves' );
$this->assertResponseContains( 'Viernes' );
$this->assertResponseContains( '23' );
$this->assertCount( 7, $crawler->filter('.row.time') );
$post = TableRegistry::get('Posts')->get(3, ['contain' => ['Elements', 'Parent']]);
$this->assertContains( 'de 10 a 22', $post->content );
$this->assertContains( 'Martes', $post->content );
$this->assertEquals( 'Horarios', $post->title );
$this->assertEquals( '1', $post->site_id );
$this->assertEquals( 'horarios', $post->slug );
$this->assertEquals( 'TimeTable', $post->class );
$this->assertRedirect('/miweb/edit-page/3');
}
}