Skip to content
This repository was archived by the owner on Sep 15, 2022. It is now read-only.

Latest commit

 

History

History
executable file
·
201 lines (159 loc) · 3.9 KB

File metadata and controls

executable file
·
201 lines (159 loc) · 3.9 KB

Entity Context

Configuration

Edit behat.yml:

default:
    # ...
    suites:
        default:
            # ...
        contexts:
            - # ...
            - Knp\FriendlyContexts\Context\EntityContext
    extensions:
        # ...
        Knp\FriendlyContexts\Extension: ~

Define namespaces

It's possible to define the namespace of the entities you wish to use with the following optional configuration:

default:
    # ...
    extensions:
        # ...
        Knp\FriendlyContexts\Extension:
            entities:
                namespaces:
                    - Acme

Examples

You have the following models (with getters and setters):

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class User
{

    /**
     * @ORM\Column(type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @ORM\Column
     */
    private $login;

    /**
     * @ORM\Column
     */
    private $firstname;

    /**
     * @ORM\Column
     */
    private $lastname;

    /**
     * @ORM\Column
     */
    private $email;

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="user")
     */
    private $products;
}
<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Product
{

    /**
     * @ORM\Column(type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @ORM\Column
     */
    private $name;

    /**
     * @ORM\Column(type="decimal")
     */
    private $price;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="products")
     */
    private $user;
}

Simply create 2 users instances

  Given the following users:
    | login | firstname | lastname |
    | admin | John      | Doe      |
    | user  | George    | Abitbol  |

Here the context will automaticly resolve user to App\Entity\User, will create 2 instances of this entity following given data and will fake the required email.

Simply create 3 products attached to users

  And the following products:
    | name    | user  |
    | Ball    | John  |
    | T-Shirt | user  |
    | Truck   | admin |

Here the context will automaticly resolve products to App\Entity\Product, will create 3 instances of this entity following given data and will fake the required price.

For attaching user to product, you can call a user by any information given in the previous step (login, firstname or lastname) or by the result of the _toString method of the user entity.

I don't care about data, I just want 100 users

No problem

  And there is 100 users

And now you've got 100 totaly faked users. You can't call there users like in the previous example.

Okay, I care about data, I want to create 200 products for John Doe

Here we go !!!

  And there is 200 products like:
    | user |
    | John |

I want to know if a user is created/deleted

Just ask

  When I open the form
  And I fill in the form
  And I press "Submit"
  Then I should see "Your user is created"
  And 1 user should have been created # <= this is the step

Same thing for deletion

  When press "Delete"
  Then I should see "Your user is deleted"
  And 1 user should have been deleted # <= this is the step

Reset Schema

You just have to use the tag @reset-schema

@reset-schema
Feature: My feature
...

Information

The context can resolve a same entity class by many name. For example, if you have a class names User, you can use user, User, users or Users. And for a class named ProjectGroup, you can use project group, project groups, projectgroup, projectgroups, ...