且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法以编程方式向behat功能提供变量?

更新时间:2022-12-12 17:56:35

这对您来说几乎是一份复制粘贴工作.我为您编写了一个自定义步骤定义Given the page contents are correct.我还添加了Scenario Outline作为经典示例,因此它们两者都相同,但是您感兴趣的是自定义步骤定义.

This will be pretty much a copy+paste job for you. I wrote a one custom step definition Given the page contents are correct for you. I also added a Scenario Outline as a classic example as well so both of them do the same but the one you're interested in is custom step definition one.

GHERKHIN

Feature: Just testing

  Scenario Outline: Multiple generic visits
    Given I am on "<page>"
    Then I should see "<content>"
    Examples:
    | page              | content   |
    | /                 | HOME PAGE |
    | /login            | Username  |
    | /profile/step_one | Name      |

  Scenario: Multiple dynamic visits
    Given the page contents are correct

结果

FEATURECONTEXT

您所需要做的就是使用getPageAndContent()查询数据库以返回您想要的内容.现在,我只是在$pageAndContent中对一些示例进行了硬编码.

All you need is to use getPageAndContent() to query the database to return what you want. For now I just hard-coded a few examples in $pageAndContent.

namespace Application\FrontendBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpKernel\KernelInterface;
use Exception;
use LogicException;

class FeatureContext extends MinkContext implements KernelAwareContext
{
    /* @var KernelInterface */
    private $kernel;

    public function setKernel(KernelInterface $kernelInterface)
    {
        $this->kernel = $kernelInterface;
    }

    /**
     * @Given /^the page contents are correct$/
     */
    public function thePageContentsAreCorrect()
    {
        $pageAndContent = $this->getPageAndContent();

        foreach ($pageAndContent as $page => $content) {
            try {
                $this->visitPath($page);
            } catch (Exception $e) {
                throw new LogicException(sprintf('The page "%s" does not exist.', $page));
            }

            try {
                $this->assertSession()->pageTextContains($this->fixStepArgument($content));
            } catch (Exception $e) {
                throw new LogicException(sprintf('The page "%s" does not contain "%s".', $page, $content));
            }
        }
    }

    /**
     * @return array
     */
    private function getPageAndContent()
    {
        /*
        $em = $this->getEntityManager();
        $repo = $this->getRepository($em, 'ApplicationFrontendBundle:Pages');

        $pageAndContent = [];
        $pages = $repo->findAll();
        foreach ($pages as $page) {
            // Build you array here
            $pageAndContent[$page->getUrl] = $page->getContent();
        }

        return $pageAndContent;
        */

        return [
            '/' => 'HOME PAGE',
            '/login' > 'Username',
            '/profile/step_one' => 'Name'
        ];
    }

    /**
     * @return EntityManager
     */
    private function getEntityManager()
    {
        return $this->kernel->getContainer()->get('doctrine')->getManager();
    }

    /**
     * @param EntityManager $entityManager
     * @param string        $serviceName
     *
     * @return EntityRepository
     */
    private function getRepository(EntityManager $entityManager, $serviceName)
    {
        return $entityManager->getRepository($serviceName);
    }
}