2

我在我的测试中模拟了两个依赖项,我将它们传递给类 AlertsMessageBag 的类构造函数。但是 php 会抛出错误,我传递了错误的依赖(Mockery\Mock)。当我运行 phpunit 时会发生这种情况。

错误:

1) 传递给 Prologue\Alerts\AlertsMessageBag::__construct() 的 AlertsMessageBagTest::testAddByLevel 参数 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.");
    }

}

我只是不明白为什么会这样。我究竟做错了什么?

4

1 回答 1

1

原来我忘了illuminate/session在我的composer.json文件中要求。挺尴尬的。

于 2013-08-03T13:53:35.367 回答