gpt4 book ai didi

php - 使用 domcrawler 的 CakePHP 3 集成测试

转载 作者:行者123 更新时间:2023-11-28 20:41:06 24 4
gpt4 key购买 nike

我已经使用 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;
}
}

然后在测试中扩展我的类,但它不起作用...

最佳答案

我终于开始上课了,我把它留在这里以防万一。

主要问题是 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');
}
}

关于php - 使用 domcrawler 的 CakePHP 3 集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32517504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com