Bläddra i källkod

Added test coverage to JobClass

* Also changed some methods logic for better UnitTesting
Marc 11 år sedan
förälder
incheckning
02d667bee8
4 ändrade filer med 238 tillägg och 27 borttagningar
  1. 1 1
      Driver/Gearman/Job.php
  2. 1 1
      Driver/Gearman/Work.php
  3. 48 25
      Module/JobClass.php
  4. 188 0
      Tests/Module/JobClassTest.php

+ 1 - 1
Driver/Gearman/Job.php

@@ -17,7 +17,7 @@ use Doctrine\Common\Annotations\Annotation;
  * 
  * @Annotation
  */
-final class Job extends Annotation
+class Job extends Annotation
 {
     /**
      * Method name to assign into job

+ 1 - 1
Driver/Gearman/Work.php

@@ -16,7 +16,7 @@ use Doctrine\Common\Annotations\Annotation;
  * 
  * @Annotation 
  */
-final class Work extends Annotation
+class Work extends Annotation
 {
     /**
      * Name of worker

+ 48 - 25
Module/JobClass.php

@@ -10,7 +10,6 @@
 namespace Mmoreram\GearmanBundle\Module;
 
 use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
-use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
 use Symfony\Component\DependencyInjection\ContainerAware;
 use ReflectionMethod;
 
@@ -22,6 +21,14 @@ use ReflectionMethod;
 class JobClass extends ContainerAware
 {
 
+    /**
+     * @var string
+     * 
+     * Default description when is not defined
+     */
+    const DEFAULT_DESCRIPTION = 'No description is defined';
+
+
     /**
      * @var string
      * 
@@ -100,64 +107,80 @@ class JobClass extends ContainerAware
 
         $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
         $this->description  = is_null($jobAnnotation->description)
-                            ? 'No description is defined'
+                            ? self::DEFAULT_DESCRIPTION
                             : $jobAnnotation->description;
 
-        $this
-            ->loadSettings($jobAnnotation, $defaultSettings)
-            ->loadServers($jobAnnotation, $servers);
+        $this->servers = $this->loadServers($jobAnnotation, $servers);
+        $this->iterations = $this->loadIterations($jobAnnotation, $defaultSettings);
+        $this->defaultMethod = $this->loadDefaultMethod($jobAnnotation, $defaultSettings);
     }
 
 
     /**
-     * Load settings
+     * Load servers
+     * 
+     * If any server is defined in JobAnnotation, this one is used.
+     * Otherwise is used servers set in Class
      * 
      * @param JobAnnotation $jobAnnotation JobAnnotation class
      * @param array         $servers       Array of servers defined for Worker
      * 
-     * @return JobClass self Object
+     * @return array Servers
      */
     private function loadServers(JobAnnotation $jobAnnotation, array $servers)
     {
-        /**
-         * By default, this job takes default servers defined in its worker
-         */
-        $this->servers = $servers;
 
         /**
          * If is configured some servers definition in the worker, overwrites
          */
         if ($jobAnnotation->servers) {
 
-            $this->servers  = ( is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']) )
-                            ? $jobAnnotation->servers
-                            : array($jobAnnotation->servers);
+            $servers    = ( is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']) )
+                        ? $jobAnnotation->servers
+                        : array($jobAnnotation->servers);
         }
 
-        return $this;
+        return $servers;
     }
 
 
     /**
-     * Load settings
+     * Load iterations
+     * 
+     * If iterations is defined in JobAnnotation, this one is used.
+     * Otherwise is used set in Class
      * 
      * @param JobAnnotation $jobAnnotation   JobAnnotation class
      * @param array         $defaultSettings Default settings for Worker
      * 
-     * @return JobClass self Object
+     * @return integer Iteration
      */
-    private function loadSettings(JobAnnotation $jobAnnotation, array $defaultSettings)
+    private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSettings)
     {
-        $this->iterations   = is_null($jobAnnotation->iterations)
-                            ? (int) $defaultSettings['iterations']
-                            : $jobAnnotation->iterations;
 
+        return  is_null($jobAnnotation->iterations)
+                ? (int) $defaultSettings['iterations']
+                : (int) $jobAnnotation->iterations;
+    }
 
-        $this->defaultMethod    = is_null($jobAnnotation->defaultMethod)
-                                ? $defaultSettings['method']
-                                : $jobAnnotation->defaultMethod;
 
-        return $this;
+    /**
+     * Load defaultMethod
+     * 
+     * If defaultMethod is defined in JobAnnotation, this one is used.
+     * Otherwise is used set in Class
+     * 
+     * @param JobAnnotation $jobAnnotation   JobAnnotation class
+     * @param array         $defaultSettings Default settings for Worker
+     * 
+     * @return string Default method
+     */
+    private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings)
+    {
+        
+        return  is_null($jobAnnotation->defaultMethod)
+                ? $defaultSettings['method']
+                : $jobAnnotation->defaultMethod;
     }
 
 

+ 188 - 0
Tests/Module/JobClassTest.php

@@ -0,0 +1,188 @@
+<?php
+
+/**
+ * RSQueueBundle for Symfony2
+ *
+ * Marc Morera 2013
+ */
+
+namespace Mmoreram\GearmanBundle\Tests\Module;
+
+use Mmoreram\GearmanBundle\Module\JobClass;
+use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
+use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
+
+/**
+ * Tests JobClassTest class
+ */
+class JobClassTest extends \PHPUnit_Framework_TestCase
+{
+
+    /**
+     * @var Job
+     * 
+     * Job annotation driver
+     */
+    private $jobAnnotation;
+
+
+    /**
+     * @var \ReflectionClass
+     * 
+     * Method reflection class
+     */
+    private $methodReflectionClass;
+
+
+    /**
+     * @var string
+     * 
+     * Callable name
+     */
+    private $callableNameClass = 'MyClassCallablaName';
+
+
+    /**
+     * @var string
+     * 
+     * Class name
+     */
+    private $methodName = 'myMethod';
+
+
+    /**
+     * @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->methodReflectionClass = $this->getMockBuilder('\ReflectionMethod')
+                                            ->disableOriginalConstructor()
+                                            ->setMethods(array(
+                                                'getName',
+                                            ))
+                                            ->getMock();
+
+        $this->jobAnnotation = $this->getMockBuilder('\Mmoreram\GearmanBundle\Driver\Gearman\Job')
+                                    ->disableOriginalConstructor()
+                                    ->getMock();
+    }
+
+
+    /**
+     * Testing first combination
+     */
+    public function testCombination1()
+    {
+
+        $this
+            ->methodReflectionClass
+            ->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue($this->methodName));
+
+        $this->jobAnnotation->name = 'myOtherMethodName';
+        $this->jobAnnotation->description = 'This is my own description';
+        $this->jobAnnotation->iterations = 200;
+        $this->jobAnnotation->defaultMethod = 'doHighBackground';
+        $this->jobAnnotation->servers = array(
+            array(
+                'host'  =>  '10.0.0.2',
+                'port'  =>  '80',
+            ),
+        );
+
+        $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
+        $this->assertEquals($jobClass->toArray(), array(
+
+            'callableName'          =>  $this->jobAnnotation->name,
+            'methodName'            =>  $this->methodName,
+            'realCallableName'      =>  str_replace('\\', '', $this->callableNameClass . '~' . $this->jobAnnotation->name),
+            'description'           =>  $this->jobAnnotation->description,
+            'iterations'            =>  $this->jobAnnotation->iterations,
+            'servers'               =>  $this->jobAnnotation->servers,
+            'defaultMethod'         =>  $this->jobAnnotation->defaultMethod,
+        ));
+    }
+
+
+    /**
+     * Testing second combination
+     */
+    public function testCombination2()
+    {
+
+        $this
+            ->methodReflectionClass
+            ->expects($this->exactly(2))
+            ->method('getName')
+            ->will($this->returnValue($this->methodName));
+
+        $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
+        $this->assertEquals($jobClass->toArray(), array(
+
+            'callableName'          =>  $this->methodName,
+            'methodName'            =>  $this->methodName,
+            'realCallableName'      =>  str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
+            'description'           =>  $jobClass::DEFAULT_DESCRIPTION,
+            'iterations'            =>  $this->defaultSettings['iterations'],
+            'servers'               =>  $this->servers,
+            'defaultMethod'         =>  $this->defaultSettings['method'],
+        ));
+    }
+
+
+    /**
+     * Testing specific server scenario
+     */
+    public function testCombinationServers()
+    {
+
+        $this
+            ->methodReflectionClass
+            ->expects($this->exactly(2))
+            ->method('getName')
+            ->will($this->returnValue($this->methodName));
+
+        $this->jobAnnotation->servers = array(
+            'host'  =>  '10.0.0.2',
+            'port'  =>  '80',
+        );
+
+        $jobClass = new JobClass($this->jobAnnotation, $this->methodReflectionClass, $this->callableNameClass, $this->servers, $this->defaultSettings);
+        $this->assertEquals($jobClass->toArray(), array(
+
+            'callableName'          =>  $this->methodName,
+            'methodName'            =>  $this->methodName,
+            'realCallableName'      =>  str_replace('\\', '', $this->callableNameClass . '~' . $this->methodName),
+            'description'           =>  $jobClass::DEFAULT_DESCRIPTION,
+            'iterations'            =>  $this->defaultSettings['iterations'],
+            'servers'               =>  array($this->jobAnnotation->servers),
+            'defaultMethod'         =>  $this->defaultSettings['method'],
+        ));
+    }
+}