我知道在symfony 2中,现在我有测试简单的场景 我有这个实体:
<?php
namespace test\MedBundle\Entity;
use test\MedBundle\Entity\Product;
/**
* Basket
*/
class Basket
{
/**
* @var int
*/
private $id;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @ORM\OneToMany(targetEntity="test\MedBundle\Entity\Product",mappedBy="basket")
*/
private $products;
public function __construct()
{
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add products
*
* @param \test\MedBundle\Entity\Product $products
*
* @return Basket
*/
public function addProducts(\test\MedBundle\Entity\Product $products)
{
$this->products[] = $products;
return $this;
}
/**
* Remove products
*
* @param \test\MedBundle\Entity\Product $products
*/
public function removeProducts(\test\MedBundle\Entity\Product $products)
{
$this->products->removeElement($products);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
public function price(){
$price =0;
foreach($this->products as $product){
$price = $product->getPrice();
}
}
}
和此实体产品:
<?php
namespace test\MedBundle\Entity;
/**
* Product
*/
class Product
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $price;
/**
* @ORM\ManyToOne(targetEntity="basket",inversedBy="products")
* @ORM\JoinColumn(name="id_basket", referencedColumnName="id",nullable=true)
*/
private $basket;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set price
*
* @param string $price
*
* @return Product
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return string
*/
public function getPrice()
{
return $this->price;
}
}
现在我有这种情况:
Feature: Basket
Permet de vérifier que le panier fonctionne
Scenario: Multiple products are added to the basket
Given An empty basket
When A product costing 5 $ is added to the basket
And A product costing 15 $ is added to the basket
Then the basket price is 20 €
这是我的FeatureContext.php:
<?php
namespace test\MedBundle\Behat\Context;
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Behat\Context\BehatContext;
use Behat\Behat\Exception\PendingException;
use test\MedBundle\Entity\Basket;
use test\MedBundle\Entity\Product;
/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context
{
/**
* 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()
{
}
/**
* @Given an empty basket
*/
public function anEmptyBasket()
{
$this->basket = new Basket();
}
/**
* @Given A product costing :price $ is added to basket
*/
public function aProductCostingIsAddedToBasket($price)
{
$product = new Product($price);
$this->basket->addProducts($product);
}
/**
* @Then the basket price is :price €
*/
public function theBasketPriceIsEu($price)
{
if ($this->basket->price() != $price) {
throw new Exception('le prix ne correspend pas');
}
}
}
但是当我测试我的场景时,我有这个问题
请帮助我,谢谢你