petefox/behat-resources-extension

此包已被放弃且不再维护。未建议替代包。

一个Behat扩展,用于使用配置作为可加载资源

1.0.1 2015-12-16 16:04 UTC

This package is not auto-updated.

Last update: 2023-01-21 09:53:28 UTC


README

这是一个简单的Behat扩展,它可以将yaml文件转换为模型/实体/对象,以使您的步骤更完善。

安装

通过composer安装

composer require peterfox/behat-resources-extension

然后,将扩展添加到您的behat.yml配置中

# behat.yml

default:
  ...
  extensions:
    BehatResources\ResourceExtension:
        path:
          resource: Resources # default value
          base: # Not required but useful, it will otherwise be the folder for the context loaded
        resource_map: # This is a map of Directories and the class it is an alias for
          User: Namespace\Of\Entity

为Laravel/Eloquent等实现一个类似于以下示例的ResourceFactory

<?php

use BehatResources\ResourceFactory;

class EloquentResourceFactory implements ResourceFactory
{
    /**
     * @param string $class
     * @param string $type
     * @param array $arguments
     * @return Object
     */
    public function buildObject($class, $type, $arguments = [])
    {
        return factory($class)->make($arguments);
    }

    /**
     * @param string $class
     * @param string $type
     * @param array $arguments
     * @return Object
     */
    public function buildPersistedObject($class, $type, $arguments = [])
    {
        return factory($class)->create($arguments);
    }
}

使上下文实现ResourceContext

<?php

use BehatResources\Context\ResourceContext;

class FeatureContext extends MinkContext implements ResourceContext
{
    private $builder;

    /**
     * @param ResourceBuilder $builder
     */
    public function setResourceBuilder(ResourceBuilder $builder)
    {
        $this->builder = $builder;
    }

    /**
     * @param $type
     * @param $identifier
     * @return array
     */
    public function getResource($type, $identifier)
    {
        return $this->resourceBuilder->getLoader()->load($type, $identifier);
    }

    /**
     * @param $type
     * @param $identifier
     * @return object
     */
    public function getResourceObject($type, $identifier)
    {
        return $this->resourceBuilder->build($type, $identifier);
    }
    
    /**
     * @param $type
     * @param $identifier
     * @return object
     */
    public function getPersistedResourceObject($type, $identifier)
    {
        return $this->resourceBuilder->build($type, $identifier);
    }

    /**
     * Returns your implementation of the ResourceFactory for changing resources into Objects/Models/Entities etc.
     *
     * @return ResourceFactory|null
     */
    public function getResourceFactory()
    {
        return new EloquentResourceFactory();
    }
}

然后,您可以在Resources目录下创建类型文件夹,例如User。在这些类型中,您可以添加以下基于yaml的配置

#features/bootstrap/Resources/User/Peter.yml
name: Peter
email: peter.fox@peterfox.me
password: howdyho1!

在您的上下文中,您可以创建一个像这样的behat步骤

    /**
     * @Given /^there is a ([^"]*) called "([^"]*)"$/
     */
    public function thereIsACalled($resource, $name)
    {
        $this->getPersistedResourceObject($resource, $name);
    }  

因此,您可以在功能中如此使用以下步骤

 Given there is a User called "Peter"