嗨,我正在测试一些php代码,并且我从PHPunit得到了这个错误。
TypeError:传递给Checkout \ Offer \ ThreeForTwo :: applicable()的参数1必须是Chechout \ Cart \ Line的实例,是Checkout \ Cart \ Line的实例
ThreeForTwo类的来源:
<?php
namespace Checkout\Offer;
use Checkout\Offer;
use Chechout\Cart\Line;
class ThreeForTwo implements Offer
{
/**
* @return ThreeForTwo
*/
public static function createOffer()
{
return new self();
}
/**
* @return boolean
*/
public function applicable(Line $line)
{
$db = Database::getIsntance();
if($line->quantity >= 3 && $db->hasThreeForTwo($line->item))
{
return true;
}
return false;
}
/**
* @return float
*/
public function calculate(Line $line)
{
return $line->item->getPrice() * $line->quantity - $line->item->getPrice() * (intVal($line->quantity/3));
}
}
以及Line类的Source调用方法:
<?php
namespace Checkout\Cart;
use Checkout\Item;
use Checkout\Offer\ThreeForTwo;
use Checkout\Offer\Discount;
class Line
{
/** @var int */
private $quantity;
/** @var Item */
private $item;
/** @var array */
private $offers;
/**
* Line constructor
* @param Item
* @param int
*/
public function __construct($item, $quantity)
{
$this->item = $item;
$this->quantity = $quantity;
$this->offers = array(
ThreeForTwo::createOffer(),
Discount::createOffer()
);
}
/**
* @return int
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* @return Item
*/
public function getItem()
{
return $this->item;
}
/**
* @return float
*/
public function calculate()
{
foreach($this->offers as $offer)
{
if($offer->applicable($this))
{
return $offer->calculate($this);
}
else
{
return $this->quantity * $this->item->getPrice();
}
}
}
}
我认为我可能在我的班级课程中遗漏了一些东西...
感谢您的帮助