vendredi 24 avril 2015

Symfony2 Unit Testing a service

I'm still very new to symfony and really enjoying it.

I'm at the stage where I've managed to create and setup a service, the service itself uses 2 dependencies:

  • 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).
  • The Doctrine Entity Manager.

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.

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.

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?

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.

Here's the Service i want to test:

<?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->em = $em;
        $this->client = $client;
    }

    /**
     * @return array
     */
    public Function parseData(){

        //var_dump($this);
        $stadiumData = $this->client->Stadiums();
        //var_dump($stadiumData);
        //get the Repo
        $repo = $this->em->getRepository('DataBundle:Stadium');

        $log = array();

        foreach ($stadiumData as $stadium) {
            // Get the current stadium in the list
            $criteria = array( 'stadiumID' => $stadium['StadiumID'] );
            $currentStadium = $repo->FindOneBy( $criteria );

            if ( ! $currentStadium) {
                $currentStadium = new Stadium(); //no stadium with the StadiumID exists so create a new stadium

                $logData = [
                    'action'   => 'Added Stadium',
                    'itemID'   => $stadium['StadiumID'],
                    'itemName' => $stadium['Name']
                ];
                $log[] = $logData;
            } else {
                $logData = [
                    'action'   => 'Updated Stadium',
                    'itemID'   => $stadium['StadiumID'],
                    'itemName' => $stadium['Name']
                ];
                $log[] = $logData;
            }
            $currentStadium->setStadiumID( $stadium['StadiumID'] );
            $currentStadium->setName( $stadium['Name'] );
            $currentStadium->setCity( $stadium['City'] );
            $currentStadium->setState( $stadium['State'] );
            $currentStadium->setCountry( $stadium['Country'] );
            $currentStadium->setCapacity( $stadium['Capacity'] );
            $currentStadium->setPlayingSurface( $stadium['PlayingSurface'] );
            $this->em->persist( $currentStadium );
        }
        $this->em->flush();
        return $log;
    }
}

Aucun commentaire:

Enregistrer un commentaire