1

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

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

4

1 回答 1

1

最后,我已经开始上课了,以防万一。

主要问题是 DomCrawler 需要一个绝对 URI,但是如果您将该 URI 传递给 CakePHP,它将无法很好地工作,所以这是我的课程

<?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');
     }
}
于 2015-09-11T09:47:16.480 回答