38

由于我正在使用服务,因此这个问题可能最终成为 symfony 中依赖注入的问题。目前我正在尝试通过 phpunit test 在我的服务中测试一个简单的功能,但我不断收到以下错误:

PHP Catchable fatal error:  Argument 1 passed to Caremonk\MainSiteBundle\Tests\Services\GeoTest::__construct() must be an instance of Caremonk\MainSiteBundle\Tests\Services\Geo, none given, called in /usr/share/nginx/html/caremonk/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php on line 473 and defined in /usr/share/nginx/html/caremonk/src/Caremonk/MainSiteBundle/Tests/Services/GeoTest.php on line 14

从错误中,很明显我正在尝试创建我的服务实例并且没有传递正确的参数,所以下面是我的 services.yml 文件:

#src/Caremonk/MainSiteBundle/Resources/config/services.yml
parameters:
    caremonk_main_site.geo.class: Caremonk\MainSiteBundle\Services\Geo
    caremonk_main_site.geo_test.class: Caremonk\MainSiteBundle\Tests\Services\GeoTest

services:
    geo:
        class: %caremonk_main_site.geo.class%
        arguments: []

    geo_test:
        class: %caremonk_main_site.geo_test.class%
        arguments: ["@geo"]

贝娄是我建立的服务:

<?php
//src/Caremonk/MainSiteBundle/Services/Geo.php
namespace Caremonk\MainSiteBundle\Services;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class Geo extends Controller
{

    public $pi80;
    public $latRad;
    public $lngRad;

    public function __construct()
    {
        $this->pi80 = M_PI / 180;
    }

    // Takes longitude and latitude and converts them into their respective radians
    // We also set our class properties to these values
    public function setCoordinates($lat,$lng)
    {
        $this->latRad = $lat * $this->pi80;
        $this->lngRad = $lng * $this->pi80;
    }

    public function distance($lat2, $lng2, $miles = true)
    {
        $lat1 = $this->latRad;
        $lng1 = $this->lngRad;
        $lat2 *= $pi80;
        $lng2 *= $pi80;

        $r = 6372.797; // mean radius of Earth in km
        $dlat = ($lat2 - $lat1)/2;
        $dlng = ($lng2 - $lng1)/2;
        $a = sin($dlat) * sin($dlat) + cos($lat1) * cos($lat2) * sin($dlng) * sin($dlng);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
        $km = $r * $c;

        return ($miles ? ($km * 0.621371192) : $km);
    }

    // This function returns the minimum latitude in radians
    public function min_lat($lat,$lng,$dis)
    {
         $dis /= .62137119;
         $ratio = $dis/6372.797;
         return asin(sin($lat)*cos($ratio) + cos($lat)*sin($ratio)*cos(M_PI));
    }

    // This function returns the max latitude in radians
    public function max_lat($lat,$lng,$dis)
    {
         $dis /= .62137119;
         $ratio = $dis/6372.797;
         return asin(sin($lat)*cos($ratio) + cos($lat)*sin($ratio)*cos(0));
    }

    // This function returns max longitude in radians
    public function max_lon($lat,$lng,$dis)
    {
         $dis /= .62137119;
         $ratio = $dis/6372.797;
         return $lng + atan2(sin(M_PI/2)*sin($ratio)*cos($lat),cos($ratio)-sin($lat)*sin($lat));
    }

    // This function returns min longitude in radians
    public function min_lon($lat,$lng,$dis)
    {
         $dis /= .62137119;
         $ratio = $dis/6372.797;
         return $lng + atan2(sin(M_PI*1.5)*sin($ratio)*cos($lat),cos($ratio)-sin($lat)*sin($lat));
    }
}

我的测试文件显示在这里:

<?php
//src/Caremonk/MainSiteBundle/Tests/Services/GeoTest.php

namespace Caremonk\MainSiteBundle\Tests\Services;

use Caremonk\MainSiteBundle\Services;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class GeoTest extends WebTestCase
{
    public $geo; 

    public function __construct(Geo $geo)
    {
        $this->geo = $geo;
    }

    public function testSetCoordinates()
    {
        $this->geo->setCoordinates(4,5);
        //print $geoService->distance(6,5);
    }
}

最后,我的服务在 app/config.yml 文件中注册如下:

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: "@CaremonkMainSiteBundle/Resources/config/services.yml" }
# Other config.yml stuff

我没有那么好理解依赖,我希望我在这篇文章中对它的解释接近 symfony 的想法。请让我知道我做错了什么,以便我可以测试我的服务。

4

3 回答 3

52

在您的测试用例中,您不会在构造函数中得到任何东西 - 您可以在 setupBeforeClass 或 setup 方法中设置要测试的对象,例如

public static function setUpBeforeClass()
{
     //start the symfony kernel
     $kernel = static::createKernel();
     $kernel->boot();

     //get the DI container
     self::$container = $kernel->getContainer();

     //now we can instantiate our service (if you want a fresh one for
     //each test method, do this in setUp() instead
     self::$geo = self::$container->get('geo');
}

(还请注意,您也不需要将测试类设置为服务,因此您可以从 services.yml 中删除 geo_test 服务)

完成后,您应该能够使用类似的东西运行您的测试用例

cd /root/of/project
phpunit -c app src/Caremonk/MainSiteBundle/Tests/Services/GeoTest.php
于 2013-07-22T22:04:58.013 回答
30

如果您不打算测试控制器(使用客户端,因为这样调用的控制器将使用新容器)您的测试类应该只扩展 KernelTestCase (Symfony\Bundle\FrameworkBundle\Test)。要启动内核并获取服务,您可以在测试设置中执行以下操作:

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class MyTest extends KernelTestCase
{
  public function setUp()
  {
    $kernel = self::bootKernel();
    $this->geo = $kernel->getContainer()->get('geo');
  }
  ...
}

更多信息: http ://symfony.com/doc/current/testing/doctrine.html

笔记:

  1. 要为每个测试提供新的内核和服务,我建议使用该setUp方法而不是setUpBeforeClass(如在接受的答案中)。
  2. 内核始终可以通过static::$kernel(引导后)访问。
  3. 也适用于 symfony 3 和 4。
于 2014-10-11T14:27:10.870 回答
0

如果您不想在应用程序中而是在独立包中进行测试,我建议您看一下这个问题的非常详细的答案

于 2015-12-18T15:36:51.977 回答