使用 domcrawler 的 CakePHP 3 集成测试
CakePHP 3 Integration tests with domcrawler
我使用 Laravel 和 Symfony 有一段时间了,我对使用 DomCrawler 进行的测试感到非常满意。现在上班用的是CakePHP 3,对集成测试系统不是很满意,是这样的:
$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€' );
我一直在寻找一种方法将DomCrawler集成到测试系统中,以便我可以使用$crawler->filter('body > p');
、$form->submit()
和所有功能,但我一无所获。有没有人这样做过?可能吗?
到目前为止我所做的是:
<?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;
}
}
然后在测试中扩展我的 Class,但它不起作用...
我终于让 class 开始工作了,我把它留在这里以防万一。
主要问题是 DomCrawler 需要一个绝对 URI,但如果你将该 URI 传递给 CakePHP,它就不会很好地工作,所以这是我的 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() );
}
}
用法示例:
<?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');
}
}
我使用 Laravel 和 Symfony 有一段时间了,我对使用 DomCrawler 进行的测试感到非常满意。现在上班用的是CakePHP 3,对集成测试系统不是很满意,是这样的:
$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€' );
我一直在寻找一种方法将DomCrawler集成到测试系统中,以便我可以使用$crawler->filter('body > p');
、$form->submit()
和所有功能,但我一无所获。有没有人这样做过?可能吗?
到目前为止我所做的是:
<?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;
}
}
然后在测试中扩展我的 Class,但它不起作用...
我终于让 class 开始工作了,我把它留在这里以防万一。
主要问题是 DomCrawler 需要一个绝对 URI,但如果你将该 URI 传递给 CakePHP,它就不会很好地工作,所以这是我的 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() );
}
}
用法示例:
<?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');
}
}