在PHP单元中调用服务


Symfony, Call service in PHP Unit

我有一个单元测试,我需要调用一个服务。

我是这样做的:

<?php
namespace FM'PriceBundle'Tests'Service;
use Symfony'Bundle'FrameworkBundle'Test'KernelTestCase;
class PriceServiceTest extends KernelTestCase
{
    private $container;
    public function testFiulPrice()
    {
        self::bootKernel();
        $this->container = self::$kernel->getContainer();
        $productId = 1;
        $id = 1;
        $what = ['postal', 'departement', 'region', 'country', 'insee'];
        $date = new 'DateTime('2016-06-23');
        $price = $this->container->get('fm.price.get')->fiulPrice($productId, $id, $what[0], $date);
        $this->assertNotEmpty($price);
    }
}

但是我有这个错误:

有1个错误:

1)调频服务' PriceBundle '测试' ' PriceServiceTest:: testFiulPricePredis'Connection'ConnectionException: Connection refused [tcp://localhost:6379]

我猜你在应用程序中使用了redis。

但是我不明白你为什么要实现这样的测试。

该测试从字面上取固定数据,并尝试从使用依赖(redis)的外部服务获取结果。

在我看来,您需要对PriceService进行单元测试,对注册为'fm.price '的服务进行另一个单元测试。Get ',和(可能)第三个使用redis,但在这种情况下,你必须填充缓存与一些样本数据