我在我的测试中模拟了两个依赖项,我将其传递给类AlertsMessageBag的类构造函数。但是php会抛出我传递错误依赖的错误(Mockery \ Mock)。当我运行phpunit时会发生这种情况。
错误:
1)AlertsMessageBagTest :: testAddByLevel 传递给Prologue \ Alerts \ AlertsMessageBag :: __ construct()的参数1必须是Illuminate \ Session \ Store的实例,给出Mockery \ Mock的实例,在/ Users / Gebruiker / Sites / tests / workbench / workbench / prologue / alerts中调用/tests/AlertsMessageBagTest.php在第18行并在/Users/Gebruiker/Sites/tests/workbench/workbench/prologue/alerts/src/Prologue/Alerts/AlertsMessageBag.php:31 / Users / Gebruiker / Sites / tests / workbench中定义/workbench/prologue/alerts/tests/AlertsMessageBagTest.php:18
我的测试:
<?php
use Mockery as m;
use Prologue\Alerts\AlertsMessageBag;
class AlertsMessageBagTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
m::close();
}
public function testAddByLevel()
{
$session = m::mock('Illuminate\Session\Store');
$config = m::mock('Illuminate\Config\Repository');
$container = new AlertsMessageBag($session, $config);
$container->error('foo');
$messages = $container->get('error');
$this->assertEquals(array('foo'), $messages);
}
}
AlertsMessageBag类:
<?php namespace Prologue\Alerts;
use BadMethodCallException;
use Illuminate\Session\Store;
use Illuminate\Config\Repository;
use Illuminate\Support\MessageBag;
class AlertsMessageBag extends MessageBag {
/**
* Illuminate's Session Store.
*
* @var \Illuminate\Session\Store
*/
protected $session;
/**
* Illuminate's Config Repository.
*
* @var \Illuminate\Config\Repository
*/
protected $config;
/**
* Initialize the AlertMessageBag class.
*
* @param \Illuminate\Session\Store $session
* @param array $messages
* @return void
*/
public function __construct(Store $session, Repository $config, array $messages = array())
{
$this->config = $config;
$this->session = $session;
// If there are already messages stored in the current
// session, merge them with the provided messages.
if ($session->has($this->getSessionKey()))
{
$messages = array_merge_recursive(
$session->get($this->getSessionKey()),
$messages
);
}
parent::__construct($messages);
}
/**
* Store the messages in the current session.
*
* @return \Prologue\Alert\AlertMessageBag
*/
public function flash()
{
$this->session->flash($this->getSessionKey(), $this->messages);
return $this;
}
/**
* Returns the alert levels from the config.
*
* @return array
*/
protected function getLevels()
{
return $this->config->get('alerts::levels');
}
/**
* Returns the session key from the config.
*
* @return string
*/
protected function getSessionKey()
{
return $this->config->get('alerts::session_key');
}
/**
* Returns the Illuminate Session Store.
*
* @return \Illuminate\Session\Store
*/
public function getSession()
{
return $this->session;
}
/**
* Returns the Illuminate Config Repository.
*
* @return \Illuminate\Config\Repository
*/
public function getConfig()
{
return $this->config;
}
/**
* Dynamically handle alert additions.
*
* @param string $method
* @param array $parameters
* @return mixed
* @throws BadMethodCallException
*/
public function __call($method, $parameters)
{
// Check if the method is in the allowed alert levels array.
if (in_array($method, $this->getLevels()))
{
return $this->add($method, $parameters[0]);
}
throw new BadMethodCallException("Method [$method] does not exist.");
}
}
我无法理解为什么会这样。我做错了什么?
答案 0 :(得分:1)
原来我在illuminate/session
文件中忘记了composer.json
。相当尴尬。