Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无效的CSRF令牌:Symfony 2上注册表的行为测试_Php_Symfony_Behat - Fatal编程技术网

Php 无效的CSRF令牌:Symfony 2上注册表的行为测试

Php 无效的CSRF令牌:Symfony 2上注册表的行为测试,php,symfony,behat,Php,Symfony,Behat,我正在使用behat开发Symfony 2.7应用程序的测试套件 测试注册表是一段非常简单的代码,但是由于CSRF令牌的原因,它失败了 我的配置有什么问题 我怎样才能避免这个问题 这是我的行为。yml: default: suites: frontend_test_suite: type: symfony_bundle bundle: 'AppBundle' extensions: Behat\Symfony2Extension: ~

我正在使用behat开发Symfony 2.7应用程序的测试套件

测试注册表是一段非常简单的代码,但是由于CSRF令牌的原因,它失败了

我的配置有什么问题

我怎样才能避免这个问题

这是我的行为。yml:

default:
  suites:
    frontend_test_suite:
        type: symfony_bundle
        bundle: 'AppBundle'
  extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
      base_url: http://application.local/app_test.php
      browser_name: chrome
      sessions:
        default:
          symfony2: ~
      show_auto: true
      show_cmd: echo %s
这是我的FeatureContext课程:

    <?php

    namespace MobileLeaves\Bundles\AppBundle\Features\Context;

    use Behat\Behat\Context\SnippetAcceptingContext;
    use Behat\MinkExtension\Context\MinkContext;
    use Behat\Symfony2Extension\Context\KernelDictionary;
    use Symfony\Component\Console\Input\StringInput;
    use Symfony\Component\Console\Output\BufferedOutput;
    use Symfony\Component\HttpKernel\KernelInterface;

    /**
     * Defines application features from the specific context.
     */
    class FeatureContext extends MinkContext implements SnippetAcceptingContext
    {

        use KernelDictionary;

        const DEBUG = true;  // make true if your tests have any troubles
        const LOADFIXTURES = true;  // activate when the project has fixtures

        /**
         * @var Application
         */
        protected $application;

        /**
         * Initializes context.
         *
         * Every scenario gets its own context instance.
         * You can also pass arbitrary arguments to the
         * context constructor through behat.yml.
         */
        public function __construct()
        {

        }

        /**
         * Clean the database (and schema).
         *
         * @Given /^database is clean$/
         */
        public function cleanDatabase()
        {
            print "\t\tC L E A N N I N G  -  D A T A B A S E \n";
            $this->application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->kernel);
            $this->application->setAutoExit(false);
            /*
             * In order to work-around this error:
             *    PDO Error - Invalid catalog name: 1046 No database selected
             * the sequence has been changed to prevent this error
             */
            //$this->runCommand("doctrine:database:drop", array("--force" => null));
            $this->runCommand('doctrine:database:create');
            $this->runCommand('doctrine:schema:drop', array('--force' => null));
            $this->runCommand('doctrine:schema:create');
            if (self::LOADFIXTURES) {
                $this->fixturesAreLoaded();
            }
            print "\t\tD A T A B A S E    -    C L E A N E D \n";
        }

        /**
         * @Given /^database is clean with \"(.*?)\" context loaded$/
         */
        public function cleanDatabaseWithContext($context)
        {
            $this->cleanDatabase($context);
        }

        /**
         * First, force logout, then go to the login page, fill the informations and finally go to requested page.
         *
         * @Given /^I am connected with "([^"]*)" and "([^"]*)" on "([^"]*)"$/
         *
         * @param string $login
         * @param string $rawPassword
         * @param string $url
         */
        public function iAmConnectedWithOn($login, $rawPassword, $url)
        {
            $this->visit('admin/logout');
            $this->visit('admin/login');
            $this->fillField('_username', $login);
            $this->fillField('_password', $rawPassword);
            $this->pressButton('_submit');
            $this->visit($url);
        }

        /**
         * @param $command
         * @param array $options
         */
        protected function runCommand($command, Array $options = array())
        {
            if (!self::DEBUG) {
                $options['-q'] = null;
            }
            $options['--env'] = 'test';
            foreach ($options as $option => $value) {
                $command .= ' '.$option.($value ? '='.$value : '');
            }
            $output = new BufferedOutput();
            $exitCode = $this->application->run(new StringInput($command), $output);

            if (self::DEBUG || $exitCode) {
                print sprintf("%s\t[exitCode=%d]\n%s", $command, $exitCode, $output->fetch());
            }
        }

        /**
         * @Given /^fixtures are loaded$/
         */
        public function fixturesAreLoaded()
        {

            $this->runCommand('doctrine:fixtures:load -n');
        }

        /**
         * {@inheritdoc}
         */
        public static function getAcceptedSnippetType()
        {
            return 'regex';
        }

        /**
         * Sets Kernel instance.
         *
         * @param KernelInterface $kernel
         */
        public function setKernel(KernelInterface $kernel) {
            $this->kernel = $kernel;
        }



    }

最后我发现了问题

注册页面上有两张表格。移除第二个表单可以顺利通过测试

问候,

Feature: Free Registration

  Background:
    Given database is clean

  Scenario: Free user register
    Given I am on "/en/register"
    When I fill in the following:
      | fos_user_registration_form[email] | maurocasula@gmail.com |
      | fos_user_registration_form[profile][name] | NAME LASTNAME |
      | fos_user_registration_form[plainPassword][first] | pass |
      | fos_user_registration_form[plainPassword][second] | pass |
    And I press "_submit"
    And show last response
    Then I should see "app.registration.confirmed_free"
    And the response status code should be 200