尝试在Symfony 2.8

时间:2016-04-28 16:05:14

标签: php symfony phpunit web-crawler

我试图在Symfony 2.8应用程序上使用PHPUnit执行简单测试。 这是测试:

public function testCreateContact(){
        $client= static::createClient();
        $client->followRedirects();
        $crawler = $client->request('GET', '/contact_create');
        $link = $crawler->filter('a:contains("Cancel")')->link();
        $crawlerLink = $client->click($link);

    }

和TWIG文件:

{% extends '::layout.html.twig' %}

{% block title %}MaestroBundle:Contacto:alta{% endblock %}

{% block body %}

<h1>New Contacto</h1>
<div class="form_error"></div>
<div id="form_body">
    <form id="createForm" method="POST" {{ form_enctype(form) }}>
            {{ form_widget(formulario) }}
            <input type="submit" value="create"/>
        </form>
            <a id="cancel" name="cancel" href="{{ path('contact_show') }}">Cancel</a>

</div>
{% endblock %}

当我执行此测试时,出现以下错误:

  

有1个错误:

     

1)   SisEvo \ MaestroBundle \测试\控制器\ ContactoControllerTest :: testCrearContacto   InvalidArgumentException:当前节点列表为空。

     

/var/www/html/evoIsaac/vendor/symfony/symfony/src/Symfony/Component/DomCrawler/Crawler.php:706   /var/www/html/evoIsaac/src/SisEvo/MaestroBundle/Tests/Controller/ContactoControllerTest.php:24

     

FAILURES!测试:2,断言:1,错误:1。

这是第24行: $ link = $ crawler-&gt;过滤器(&#39; a:包含(&#34;取消&#34;)&#39;) - &gt; link();

此外,我尝试使用var_dump检查$ crawler内容,结果如下:

  

文件&#34; /var/www/html/evoIsaac/app/config/routing.yml"才不是   在/var/www/html/evoIsaac/app/config/routing.yml中包含有效的YAML   (正在从中导入   &#34; /var/www/html/evoIsaac/app/config/routing_dev.yml")。 (500内部   服务器错误)

PHPUnit版本:5.3.2 PHP版本:5.6.18 Symfony版本:2.8.4 操作系统:Fedora 23

有人可以帮助我吗?

修改

我试图检查我的YAML文件,但是我收到了这个错误:

  

ERROR:

     

在扫描下一个令牌时找到了字符&#39; @&#39;那不可能   在&#34;&#34;,第22行,第15栏中启动任何令牌:           资源:@ MaestroBundle / Controller /                     ^

这是我的YAML文件:

cys:
    resource: "@CysBundle/Controller/"
    type:     annotation
    prefix:   /

homepage:
    path: /
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    'default/login.html.twig'


contrato_evolutia:
    resource: "@ContratoEvolutiaBundle/Controller/"
    type: annotation

api_maestro:
    resource: "@ApiBundle/Controller/Maestro/"
    type:     annotation

maestro:
    resource: @MaestroBundle/Controller/
    type:     annotation

default:
    resource: @DefaultBundle/Controller/
    type:     annotation


fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

我还尝试用以下方法检查客户端响应:

$this->assertTrue($client->getResponse()->isSuccessful());

但是PHPUnit显示了这个错误:

  

声明false为真的失败

希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

最后我解决了这个问题!! 感谢@ A.L和@Matteo的意见;问题是我没有&#34;&#34;在@Bundle之前的YAML,现在它正在工作!

我的YAML文件的最终版本是:

 cys:
    resource: "@CysBundle/Controller/"
    type:     annotation
    prefix:   /

homepage:
    path: /
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    'default/login.html.twig'


contrato_evolutia:
    resource: "@ContratoEvolutiaBundle/Controller/"
    type: annotation

api_maestro:
    resource: "@ApiBundle/Controller/Maestro/"
    type:     annotation

maestro:
    resource: "@MaestroBundle/Controller/"
    type:     annotation

default:
    resource: "@DefaultBundle/Controller/"
    type:     annotation


fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

非常感谢! 艾萨克。