如何在 symfony2 中测试服务? [英] How can I test a service in symfony2?

查看:33
本文介绍了如何在 symfony2 中测试服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since I'm working with services, this question may end up being an issue with dependency-injection in symfony. Currently I'm trying to test one simple feature in my service via phpunit test and I keep getting the following error:

PHP Catchable fatal error:  Argument 1 passed to CaremonkMainSiteBundleTestsServicesGeoTest::__construct() must be an instance of CaremonkMainSiteBundleTestsServicesGeo, 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 文件:

From the error, it is obvious that I am trying create an instance of my service and the correct argument is not being passed, so bellow is my services.yml file:

#src/Caremonk/MainSiteBundle/Resources/config/services.yml
parameters:
    caremonk_main_site.geo.class: CaremonkMainSiteBundleServicesGeo
    caremonk_main_site.geo_test.class: CaremonkMainSiteBundleTestsServicesGeoTest

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

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

Bellow 是我构建的服务:

Bellow is my service that I've built:

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

use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;

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));
    }
}

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

My test file is shown here:

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

namespace CaremonkMainSiteBundleTestsServices;

use CaremonkMainSiteBundleServices;
use SymfonyBundleFrameworkBundleTestWebTestCase;
use SymfonyComponentDependencyInjectionContainerBuilder;

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 文件中注册如下:

Lastly, my services are registered bellow in the app/config.yml file:

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

我不太了解依赖关系,我希望我在这篇文章中对它的解释与 symfony 的想法接近.请让我知道我做错了什么,以便我可以测试我的服务.

I don't get dependency that well and I'm hoping that my interpretation of it as shown in this post is close to what symfony had in mind. Please let me know what I'm doing wrong so I can test my service.

推荐答案

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

In your test case, you're not going to get anything in your constructor - you can set up the object you want to test in a setupBeforeClass or setup method, e.g.

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 服务)

(Note also you don't need to set up your test classes as services either, so you can remove your geo_test service from services.yml)

一旦你这样做了,你应该能够用类似的东西运行你的测试用例

Once you've done that, you should be able to run your test case with something like

cd /root/of/project
phpunit -c app src/Caremonk/MainSiteBundle/Tests/Services/GeoTest.php

这篇关于如何在 symfony2 中测试服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆