title: How can I test a service in symfony2?
body: <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>#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"]
  </code></pre>
  
  <p>Bellow is my service that I've built:</p>
  
  <pre><code>&lt;?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-&gt;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-&gt;latRad = $lat * $this-&gt;pi80;
          $this-&gt;lngRad = $lng * $this-&gt;pi80;
      }
  
      public function distance($lat2, $lng2, $miles = true)
      {
          $lat1 = $this-&gt;latRad;
          $lng1 = $this-&gt;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));
      }
  }
  </code></pre>
  
  <p>My test file is shown here:</p>
  
  <pre><code>&lt;?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-&gt;geo = $geo;
      }
  
      public function testSetCoordinates()
      {
          $this-&gt;geo-&gt;setCoordinates(4,5);
          //print $geoService-&gt;distance(6,5);
      }
  }
  </code></pre>
  
  <p>Lastly, my services are registered bellow in the app/config.yml file:</p>
  
  <pre><code>imports:
      - { resource: parameters.yml }
      - { resource: security.yml }
      - { resource: "@CaremonkMainSiteBundle/Resources/config/services.yml" }
  # Other config.yml stuff
  </code></pre>
  
  <p>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.</p>