Browse Source

Tested WorkerModule and Edited some code

* Refactored class for being more testable
* Fixed some CS
* Tested full class
Marc 12 years ago
parent
commit
4c79a05a36
4 changed files with 405 additions and 65 deletions
  1. 5 5
      Module/JobClass.php
  2. 71 46
      Module/WorkerClass.php
  3. 22 14
      Tests/Module/JobClassTest.php
  4. 307 0
      Tests/Module/WorkerClassTest.php

+ 5 - 5
Module/JobClass.php

@@ -92,18 +92,18 @@ class JobClass extends ContainerAware
      * Construct method
      *
      * @param JobAnnotation    $jobAnnotation     JobAnnotation class
-     * @param ReflectionMethod $method            ReflextionMethod class
+     * @param ReflectionMethod $reflectionMethod  ReflextionMethod class
      * @param string           $callableNameClass Callable name class
      * @param array            $servers           Array of servers defined for Worker
      * @param array            $defaultSettings   Default settings for Worker
      */
-    public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
+    public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $reflectionMethod, $callableNameClass, array $servers, array $defaultSettings)
     {
         $this->callableName = is_null($jobAnnotation->name)
-                            ? $method->getName()
+                            ? $reflectionMethod->getName()
                             : $jobAnnotation->name;
 
-        $this->methodName = $method->getName();
+        $this->methodName = $reflectionMethod->getName();
 
         $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
         $this->description  = is_null($jobAnnotation->description)
@@ -177,7 +177,7 @@ class JobClass extends ContainerAware
      */
     private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings)
     {
-        
+
         return  is_null($jobAnnotation->defaultMethod)
                 ? $defaultSettings['method']
                 : $jobAnnotation->defaultMethod;

+ 71 - 46
Module/WorkerClass.php

@@ -9,7 +9,7 @@
 
 namespace Mmoreram\GearmanBundle\Module;
 
-use Doctrine\Common\Annotations\Reader;
+use Doctrine\Common\Annotations\SimpleAnnotationReader;
 use Mmoreram\GearmanBundle\Module\JobCollection;
 use Mmoreram\GearmanBundle\Module\JobClass as Job;
 use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
@@ -25,6 +25,14 @@ use ReflectionMethod;
 class WorkerClass
 {
 
+    /**
+     * @var string
+     * 
+     * Default description when is not defined
+     */
+    const DEFAULT_DESCRIPTION = 'No description is defined';
+
+
     /**
      * @var string
      * 
@@ -118,14 +126,15 @@ class WorkerClass
     /**
      * Retrieves all jobs available from worker
      *
-     * @param WorkAnnotation  $workAnnotation  workAnnotation class
-     * @param ReflectionClass $reflectionClass Reflexion class
-     * @param Reader          $reader          ReaderAnnotation class
-     * @param array           $servers         Array of servers defined for Worker
-     * @param array           $defaultSettings Default settings for Worker
+     * @param WorkAnnotation         $workAnnotation  workAnnotation class
+     * @param ReflectionClass        $reflectionClass Reflexion class
+     * @param SimpleAnnotationReader $reader          ReaderAnnotation class
+     * @param array                  $servers         Array of servers defined for Worker
+     * @param array                  $defaultSettings Default settings for Worker
      */
-    public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $reflectionClass, Reader $reader, array $servers, array $defaultSettings)
+    public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $reflectionClass, SimpleAnnotationReader $reader, array $servers, array $defaultSettings)
     {
+
         $this->namespace = $reflectionClass->getNamespaceName();
 
         /**
@@ -133,7 +142,7 @@ class WorkerClass
          */
         $this->callableName = is_null($workAnnotation->name)
                             ? $reflectionClass->getName()
-                            : $this->namespace .'\\' .$workAnnotation->name;
+                            : $this->namespace . $workAnnotation->name;
 
         $this->callableName = str_replace('\\', '', $this->callableName);
 
@@ -141,95 +150,106 @@ class WorkerClass
          * Setting worker description
          */
         $this->description  = is_null($workAnnotation->description)
-                            ? 'No description is defined'
+                            ? self::DEFAULT_DESCRIPTION
                             : $workAnnotation->description;
 
         $this->fileName = $reflectionClass->getFileName();
         $this->className = $reflectionClass->getName();
         $this->service = $workAnnotation->service;
 
-        $this
-            ->loadSettings($workAnnotation, $defaultSettings)
-            ->loadServers($workAnnotation, $servers)
-            ->createJobCollection($reflectionClass, $reader);
+        $this->servers = $this->loadServers($workAnnotation, $servers);
+        $this->iterations = $this->loadIterations($workAnnotation, $defaultSettings);
+        $this->defaultMethod = $this->loadDefaultMethod($workAnnotation, $defaultSettings);
+        $this->jobCollection = $this->createJobCollection($reflectionClass, $reader);
     }
 
 
     /**
-     * Load settings
+     * Load servers
+     * 
+     * If any server is defined in JobAnnotation, this one is used.
+     * Otherwise is used servers set in Class
      * 
      * @param WorkAnnotation $workAnnotation WorkAnnotation class
      * @param array          $servers        Array of servers defined for Worker
      * 
-     * @return WorkerClass self Object
+     * @return array Servers
      */
     private function loadServers(WorkAnnotation $workAnnotation, array $servers)
     {
-        /**
-         * By default, this worker takes default servers definition
-         */
-        $this->servers = $servers;
 
         /**
          * If is configured some servers definition in the worker, overwrites
          */
         if ($workAnnotation->servers) {
 
-            $this->servers  = ( is_array($workAnnotation->servers) && !isset($workAnnotation->servers['host']) )
-                            ? $workAnnotation->servers
-                            : array($workAnnotation->servers);
+            $servers    = ( is_array($workAnnotation->servers) && !isset($workAnnotation->servers['host']) )
+                        ? $workAnnotation->servers
+                        : array($workAnnotation->servers);
         }
 
-        return $this;
+        return $servers;
     }
 
 
+
     /**
-     * Load settings
+     * Load iterations
+     * 
+     * If iterations is defined in WorkAnnotation, this one is used.
+     * Otherwise is used set in Class
      * 
      * @param WorkAnnotation $workAnnotation  WorkAnnotation class
      * @param array          $defaultSettings Default settings for Worker
      * 
-     * @return WorkerClass self Object
+     * @return integer Iteration
      */
-    private function loadSettings(WorkAnnotation $workAnnotation, array $defaultSettings)
+    private function loadIterations(WorkAnnotation $workAnnotation, array $defaultSettings)
     {
-        $this->iterations   = is_null($workAnnotation->iterations)
-                            ? (int) $defaultSettings['iterations']
-                            : $workAnnotation->iterations;
-
-        $defaultSettings['iterations'] = $this->iterations;
 
-        $this->defaultMethod    = is_null($workAnnotation->defaultMethod)
-                                ? $defaultSettings['method']
-                                : $workAnnotation->defaultMethod;
+        return  is_null($workAnnotation->iterations)
+                ? (int) $defaultSettings['iterations']
+                : (int) $workAnnotation->iterations;
+    }
 
-        $defaultSettings['method'] = $this->defaultMethod;
 
-        $this->settings = $defaultSettings;
+    /**
+     * Load defaultMethod
+     * 
+     * If defaultMethod is defined in WorkAnnotation, this one is used.
+     * Otherwise is used set in Class
+     * 
+     * @param WorkAnnotation $workAnnotation  WorkAnnotation class
+     * @param array          $defaultSettings Default settings for Worker
+     * 
+     * @return string Default method
+     */
+    private function loadDefaultMethod(WorkAnnotation $workAnnotation, array $defaultSettings)
+    {
 
-        return $this;
+        return  is_null($workAnnotation->defaultMethod)
+                ? $defaultSettings['method']
+                : $workAnnotation->defaultMethod;
     }
 
 
     /**
      * Creates job collection of worker
      * 
-     * @param ReflectionClass $reflectionClass Reflexion class
-     * @param Reader          $reader          ReaderAnnotation class
+     * @param ReflectionClass        $reflectionClass Reflexion class
+     * @param SimpleAnnotationReader $reader          ReaderAnnotation class
      * 
      * @return WorkerClass self Object
      */
-    private function createJobCollection(ReflectionClass $reflectionClass, Reader $reader)
+    private function createJobCollection(ReflectionClass $reflectionClass, SimpleAnnotationReader $reader)
     {
-        $this->jobCollection = new JobCollection;
+        $jobCollection = new JobCollection;
 
         /**
          * For each defined method, we parse it
          */
-        foreach ($reflectionClass->getMethods() as $method) {
+        foreach ($reflectionClass->getMethods() as $reflectionMethod) {
 
-            $reflectionMethod = new ReflectionMethod($method->class, $method->name);
             $methodAnnotations = $reader->getMethodAnnotations($reflectionMethod);
 
             /**
@@ -245,13 +265,18 @@ class WorkerClass
                     /**
                      * Creates new Job
                      */
-                    $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, $this->settings);
-                    $this->jobCollection->add($job);
+                    $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, array(
+
+                        'iterations'    =>  $this->iterations,
+                        'method'        =>  $this->defaultMethod,
+                    ));
+
+                    $jobCollection->add($job);
                 }
             }
         }
 
-        return $this;
+        return $jobCollection;
     }
 
 

+ 22 - 14
Tests/Module/JobClassTest.php

@@ -29,9 +29,9 @@ class JobClassTest extends \PHPUnit_Framework_TestCase
     /**
      * @var \ReflectionClass
      * 
-     * Method reflection class
+     * Reflection Method
      */
-    private $methodReflectionClass;
+    private $reflectionMethod;
 
 
     /**
@@ -80,7 +80,7 @@ class JobClassTest extends \PHPUnit_Framework_TestCase
     public function setUp()
     {
 
-        $this->methodReflectionClass = $this->getMockBuilder('\ReflectionMethod')
+        $this->reflectionMethod = $this->getMockBuilder('\ReflectionMethod')
                                             ->disableOriginalConstructor()
                                             ->setMethods(array(
                                                 'getName',
@@ -94,13 +94,17 @@ class JobClassTest extends \PHPUnit_Framework_TestCase
 
 
     /**
-     * Testing first combination
+     * Testing scenario with all Job annotations filled
+     * 
+     * All settings given in annotations should be considered to configure Job
+     * 
+     * Also testing server definition in JobAnnotation as an array of arrays ( multi server )
      */
-    public function testCombination1()
+    public function testJobAnnotationsDefined()
     {
 
         $this
-            ->methodReflectionClass
+            ->reflectionMethod
             ->expects($this->once())
             ->method('getName')
             ->will($this->returnValue($this->methodName));
@@ -116,7 +120,7 @@ class JobClassTest extends \PHPUnit_Framework_TestCase
             ),
         );
 
-        $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
+        $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
         $this->assertEquals($jobClass->toArray(), array(
 
             'callableName'          =>  $this->jobAnnotation->name,
@@ -131,18 +135,22 @@ class JobClassTest extends \PHPUnit_Framework_TestCase
 
 
     /**
-     * Testing second combination
+     * Testing scenario with any Job annotation filled
+     * 
+     * All settings set as default should be considered to configure Job
+     * 
+     * Also testing empty server definition in JobAnnotation
      */
-    public function testCombination2()
+    public function testJonAnnotationsEmpty()
     {
 
         $this
-            ->methodReflectionClass
+            ->reflectionMethod
             ->expects($this->exactly(2))
             ->method('getName')
             ->will($this->returnValue($this->methodName));
 
-        $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
+        $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
         $this->assertEquals($jobClass->toArray(), array(
 
             'callableName'          =>  $this->methodName,
@@ -157,13 +165,13 @@ class JobClassTest extends \PHPUnit_Framework_TestCase
 
 
     /**
-     * Testing specific server scenario
+     * Testing specific server scenario configured in Job annotations as a simple server
      */
     public function testCombinationServers()
     {
 
         $this
-            ->methodReflectionClass
+            ->reflectionMethod
             ->expects($this->exactly(2))
             ->method('getName')
             ->will($this->returnValue($this->methodName));
@@ -173,7 +181,7 @@ class JobClassTest extends \PHPUnit_Framework_TestCase
             'port'  =>  '80',
         );
 
-        $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
+        $jobClass = new JobClass($this->jobAnnotation, $this->reflectionMethod, $this->callableNameClass, $this->servers, $this->defaultSettings);
         $this->assertEquals($jobClass->toArray(), array(
 
             'callableName'          =>  $this->methodName,

+ 307 - 0
Tests/Module/WorkerClassTest.php

@@ -0,0 +1,307 @@
+<?php
+
+/**
+ * RSQueueBundle for Symfony2
+ *
+ * Marc Morera 2013
+ */
+
+namespace Mmoreram\GearmanBundle\Tests\Module;
+
+use Mmoreram\GearmanBundle\Module\JobClass;
+use Mmoreram\GearmanBundle\Module\WorkerClass;
+use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
+use Mmoreram\GearmanBundle\Driver\Gearman\Job as JonAnnotation;
+
+/**
+ * Tests JobClassTest class
+ */
+class WorkerClassTest extends \PHPUnit_Framework_TestCase
+{
+
+    /**
+     * @var WorkAnnotation
+     * 
+     * Worker annotation driver
+     */
+    private $workerAnnotation;
+
+
+    /**
+     * @var \ReflectionClass
+     * 
+     * Reflection Class
+     */
+    private $reflectionClass;
+
+
+    /**
+     * @var Reader
+     * 
+     * Reader
+     */
+    private $reader;
+
+
+    /**
+     * @var string
+     * 
+     * Class namespace
+     */
+    private $classNamespace = 'MyClassNamespace';
+
+
+    /**
+     * @var string
+     * 
+     * Callable name
+     */
+    private $callableNameClass = 'MyClassCallablaName';
+
+
+    /**
+     * @var string
+     * 
+     * Class name
+     */
+    private $className = 'myClass';
+
+
+    /**
+     * @var string
+     * 
+     * Filename
+     */
+    private $fileName = 'myClass.php';
+
+
+    /**
+     * @var array
+     * 
+     * Servers list
+     */
+    private $servers = array(
+        array(
+            'host'  =>  '192.168.1.1',
+            'port'  =>  '8080',
+        ),
+    );
+
+
+    /**
+     * @var array
+     * 
+     * Default settings
+     */
+    private $defaultSettings = array(
+        'method'        =>  'doHigh',
+        'iterations'    =>  100,
+    );
+
+
+    /**
+     * Setup
+     */
+    public function setUp()
+    {
+
+        $this->reflectionClass = $this  ->getMockBuilder('\ReflectionClass')
+                                        ->disableOriginalConstructor()
+                                        ->setMethods(array(
+                                            'getName',
+                                            'getNamespaceName',
+                                            'getFileName',
+                                            'getMethods',
+                                        ))
+                                        ->getMock();
+
+        $this->workAnnotation = $this   ->getMockBuilder('\Mmoreram\GearmanBundle\Driver\Gearman\Work')
+                                        ->disableOriginalConstructor()
+                                        ->getMock();
+
+        $this->reader = $this   ->getMockBuilder('Doctrine\Common\Annotations\SimpleAnnotationReader')
+                                ->disableOriginalConstructor()
+                                ->setMethods(array(
+                                    'getMethodAnnotations'
+                                ))
+                                ->getMock();
+    }
+
+
+    /**
+     * Testing scenario with all Job annotations filled
+     * 
+     * All settings given in annotations should be considered to configure Job
+     * 
+     * Also testing server definition in JobAnnotation as an array of arrays ( multi server )
+     */
+    public function testWorkerAnnotationsDefined()
+    {
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getNamespaceName')
+            ->will($this->returnValue($this->classNamespace));
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue($this->className));
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getFileName')
+            ->will($this->returnValue($this->fileName));
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getMethods')
+            ->will($this->returnValue(array()));
+
+        $this
+            ->reader
+            ->expects($this->any())
+            ->method('getMethodAnnotations');
+
+        $this->workAnnotation->name = 'myOtherWorkerName';
+        $this->workAnnotation->description = 'This is my own description';
+        $this->workAnnotation->iterations = 200;
+        $this->workAnnotation->defaultMethod = 'doHighBackground';
+        $this->workAnnotation->service = 'my.service';
+        $this->workAnnotation->servers = array(
+            array(
+                'host'  =>  '10.0.0.2',
+                'port'  =>  '80',
+            ),
+        );
+
+        $workerClass = new WorkerClass($this->workAnnotation, $this->reflectionClass, $this->reader, $this->servers, $this->defaultSettings);
+        $this->assertEquals($workerClass->toArray(), array(
+
+            'namespace'             =>  $this->classNamespace,
+            'className'             =>  $this->className,
+            'fileName'              =>  $this->fileName,
+            'callableName'          =>  $this->classNamespace . $this->workAnnotation->name,
+            'description'           =>  $this->workAnnotation->description,
+            'service'               =>  $this->workAnnotation->service,
+            'servers'               =>  $this->workAnnotation->servers,
+            'iterations'            =>  $this->workAnnotation->iterations,
+            'jobs'                  =>  array(),
+        ));
+    }
+
+
+    /**
+     * Testing scenario with any Job annotation filled
+     * 
+     * All settings set as default should be considered to configure Job
+     * 
+     * Also testing empty server definition in JobAnnotation
+     */
+    public function testWorkerAnnotationsEmpty()
+    {
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getNamespaceName')
+            ->will($this->returnValue($this->classNamespace));
+
+        $this
+            ->reflectionClass
+            ->expects($this->exactly(2))
+            ->method('getName')
+            ->will($this->returnValue($this->className));
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getFileName')
+            ->will($this->returnValue($this->fileName));
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getMethods')
+            ->will($this->returnValue(array()));
+
+        $this
+            ->reader
+            ->expects($this->any())
+            ->method('getMethodAnnotations');
+
+        $workerClass = new WorkerClass($this->workAnnotation, $this->reflectionClass, $this->reader, $this->servers, $this->defaultSettings);
+        $this->assertEquals($workerClass->toArray(), array(
+
+            'namespace'             =>  $this->classNamespace,
+            'className'             =>  $this->className,
+            'fileName'              =>  $this->fileName,
+            'callableName'          =>  $this->className,
+            'description'           =>  $workerClass::DEFAULT_DESCRIPTION,
+            'service'               =>  null,
+            'servers'               =>  $this->servers,
+            'iterations'            =>  $this->defaultSettings['iterations'],
+            'jobs'                  =>  array(),
+        ));
+    }
+
+
+    /**
+     * Testing specific server scenario configured in Job annotations as a simple server
+     */
+    public function testCombinationServers()
+    {
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getNamespaceName')
+            ->will($this->returnValue($this->classNamespace));
+
+        $this
+            ->reflectionClass
+            ->expects($this->exactly(2))
+            ->method('getName')
+            ->will($this->returnValue($this->className));
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getFileName')
+            ->will($this->returnValue($this->fileName));
+
+        $this
+            ->reflectionClass
+            ->expects($this->once())
+            ->method('getMethods')
+            ->will($this->returnValue(array()));
+
+        $this
+            ->reader
+            ->expects($this->any())
+            ->method('getMethodAnnotations');
+
+        $this->workAnnotation->servers = array(
+            'host'  =>  '10.0.0.2',
+            'port'  =>  '80',
+        );
+
+        $workerClass = new WorkerClass($this->workAnnotation, $this->reflectionClass, $this->reader, $this->servers, $this->defaultSettings);
+        $this->assertEquals($workerClass->toArray(), array(
+
+            'namespace'             =>  $this->classNamespace,
+            'className'             =>  $this->className,
+            'fileName'              =>  $this->fileName,
+            'callableName'          =>  $this->className,
+            'description'           =>  $workerClass::DEFAULT_DESCRIPTION,
+            'service'               =>  null,
+            'servers'               =>  array($this->workAnnotation->servers),
+            'iterations'            =>  $this->defaultSettings['iterations'],
+            'jobs'                  =>  array(),
+        ));
+    }
+}