title: Symfony2 Unit Testing a service
body: <p>I'm still very new to symfony and really enjoying it.</p>
  
  <p>I'm at the stage where I've managed to create and setup a service, the service itself uses 2 dependencies: </p>
  
  <ul>
  <li>A Data API that returns json data  (this is a separate library which
  i have implemented as a service and came with its own unit tests).</li>
  <li>The Doctrine Entity Manager.</li>
  </ul>
  
  <p>The service pulls the data required using the api and then loops through the data and checks to see if the data already exists, if it does it updates the existing entity and persists it, Otherwise it creates a new entity assigns the data and persists it.</p>
  
  <p>I now need to write a unit test for this, i've not used PHPUnit only from symfony2 tutorials which were testing responses from a controller.</p>
  
  <p>How do i go about writing a unit test for this service?
  In particular mocking the data that i would normally pull from the api.
  and then checks to see if the entry needs to be updated or created?</p>
  
  <p>A code example would be really helpful so i can then use this as a template to create tests for other similar services that i create.</p>
  
  <p>Here's the Service i want to test:</p>
  
  <pre><code>&lt;?php
  
  namespace FantasyPro\DataBundle\DataManager;
  
  use Doctrine\ORM\EntityManager;
  use FantasyDataAPI\Client;
  use FantasyPro\DataBundle\Entity\Stadium;
  
  class StadiumParser {
      /**
       * @var EntityManager $em
       */
      private  $em;
      /**
       * @var Client $client
       */
      private $client;
  
      public function __construct( EntityManager $em, Client $client) {
          $this-&gt;em = $em;
          $this-&gt;client = $client;
      }
  
      /**
       * @return array
       */
      public Function parseData(){
  
          //var_dump($this);
          $stadiumData = $this-&gt;client-&gt;Stadiums();
          //var_dump($stadiumData);
          //get the Repo
          $repo = $this-&gt;em-&gt;getRepository('DataBundle:Stadium');
  
          $log = array();
  
          foreach ($stadiumData as $stadium) {
              // Get the current stadium in the list from the database
              $criteria = array( 'stadiumID' =&gt; $stadium['StadiumID'] );
              $currentStadium = $repo-&gt;FindOneBy( $criteria );
  
              if ( ! $currentStadium) {
                  $currentStadium = new Stadium(); //no stadium with the StadiumID exists so create a new stadium
  
                  $logData = [
                      'action'   =&gt; 'Added Stadium',
                      'itemID'   =&gt; $stadium['StadiumID'],
                      'itemName' =&gt; $stadium['Name']
                  ];
                  $log[] = $logData;
              } else {
                  $logData = [
                      'action'   =&gt; 'Updated Stadium',
                      'itemID'   =&gt; $stadium['StadiumID'],
                      'itemName' =&gt; $stadium['Name']
                  ];
                  $log[] = $logData;
              }
              $currentStadium-&gt;setStadiumID( $stadium['StadiumID'] );
              $currentStadium-&gt;setName( $stadium['Name'] );
              $currentStadium-&gt;setCity( $stadium['City'] );
              $currentStadium-&gt;setState( $stadium['State'] );
              $currentStadium-&gt;setCountry( $stadium['Country'] );
              $currentStadium-&gt;setCapacity( $stadium['Capacity'] );
              $currentStadium-&gt;setPlayingSurface( $stadium['PlayingSurface'] );
              $this-&gt;em-&gt;persist( $currentStadium );
          }
          $this-&gt;em-&gt;flush();
          return $log;
      }
  }
  </code></pre>
  
  <p>****** UPDATE *******
  after reading ilpaijin's answer.</p>
  
  <p>I've simplified the service so it no longer returns a log, i initially had this in so i could check what had been added by sending the log to a twig template in my controller, I eventually plan to have this running as a command so i can run a it via a cron job so the log bit is unnecessary.</p>
  
  <p><strike>I'm now setting the entity within my construct as i couldn't work out how to pass an entity as a injected dependency.</strike>
  Now grabbing a new entity by using <code>createNewStadium()</code> method.</p>
  
  <p>The Updated Service:
      
  
  <pre><code>namespace FantasyPro\DataBundle\DataManager;
  
  use Doctrine\ORM\EntityManager;
  use FantasyDataAPI\Client;
  use FantasyPro\DataBundle\Entity\Stadium;
  
  class StadiumParser {
      /**
       * @var EntityManager $em
       */
      private  $em;
      /**
       * @var Client $client
       */
      private $client;
      /**
       * @var Stadium Stadium
       */
      private $stadium;
  
      public function __construct( EntityManager $em, Client $client) {
          $this-&gt;em = $em;
          $this-&gt;client = $client;
      }
  
      /**
       * Gets a list of stadiums using $this-&gt;client-&gt;Stadiums.
       * loops through returned stadiums and persists them
       * when loop has finished flush them to the db
       */
      public Function parseData(){
          $data = $this-&gt;client-&gt;Stadiums();
          //get the Repo
          $repo = $this-&gt;em-&gt;getRepository('DataBundle:Stadium');
  
          foreach ($data as $item) {
              // Get the current stadium in the list
              $criteria = array( 'stadiumID' =&gt; $item['StadiumID'] );
              $currentStadium = $repo-&gt;FindOneBy( $criteria );
  
              if ( ! $currentStadium) {
                  $currentStadium = $this-&gt;createNewStadium; //no stadium with the StadiumID use the new stadium entity
              }
              $currentStadium-&gt;setStadiumID( $item['StadiumID'] );
              $currentStadium-&gt;setName( $item['Name'] );
              $currentStadium-&gt;setCity( $item['City'] );
              $currentStadium-&gt;setState( $item['State'] );
              $currentStadium-&gt;setCountry( $item['Country'] );
              $currentStadium-&gt;setCapacity( $item['Capacity'] );
              $currentStadium-&gt;setPlayingSurface( $item['PlayingSurface'] );
              $this-&gt;em-&gt;persist( $currentStadium );
          }
          $this-&gt;em-&gt;flush();
      }
  
      // Adding this new method gives you the ability to mock this dependency  when testing  
      private function createNewStadium()
      {
          return new Stadium();
      }
  }
  </code></pre>