From 4c79a05a36a04d3033c0c60e740d02f2879ab8ec Mon Sep 17 00:00:00 2001 From: Marc Date: Mon, 9 Sep 2013 02:17:42 +0200 Subject: [PATCH 1/2] Tested WorkerModule and Edited some code * Refactored class for being more testable * Fixed some CS * Tested full class --- Module/JobClass.php | 10 +- Module/WorkerClass.php | 117 +++++++----- Tests/Module/JobClassTest.php | 36 ++-- Tests/Module/WorkerClassTest.php | 307 +++++++++++++++++++++++++++++++ 4 files changed, 405 insertions(+), 65 deletions(-) create mode 100644 Tests/Module/WorkerClassTest.php diff --git a/Module/JobClass.php b/Module/JobClass.php index 773a36a..9eaf85b 100644 --- a/Module/JobClass.php +++ b/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 @@ private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSett */ private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings) { - + return is_null($jobAnnotation->defaultMethod) ? $defaultSettings['method'] : $jobAnnotation->defaultMethod; diff --git a/Module/WorkerClass.php b/Module/WorkerClass.php index 406ecec..67d31d8 100644 --- a/Module/WorkerClass.php +++ b/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 @@ 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 @@ public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $ref */ $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 @@ public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $ref * 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 @@ private function createJobCollection(ReflectionClass $reflectionClass, Reader $r /** * 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; } diff --git a/Tests/Module/JobClassTest.php b/Tests/Module/JobClassTest.php index ae5fc5f..18f07d4 100644 --- a/Tests/Module/JobClassTest.php +++ b/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 @@ public function setUp() /** - * 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 @@ public function testCombination1() ), ); - $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 @@ public function testCombination1() /** - * 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 @@ public function testCombination2() /** - * 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 @@ public function testCombinationServers() '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, diff --git a/Tests/Module/WorkerClassTest.php b/Tests/Module/WorkerClassTest.php new file mode 100644 index 0000000..b363fbd --- /dev/null +++ b/Tests/Module/WorkerClassTest.php @@ -0,0 +1,307 @@ + '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(), + )); + } +} \ No newline at end of file From f84593329b91cfc504249b62ed36bf9283ae0df8 Mon Sep 17 00:00:00 2001 From: Marc Date: Mon, 9 Sep 2013 02:56:09 +0200 Subject: [PATCH 2/2] Composer versions up to date --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 0939bf0..f09e63e 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "keywords": ["gearman"], "homepage": "https://github.com/mmoreram/GearmanBundle", "type": "symfony-bundle", - "license": "Apache", + "license": "MIT", "authors": [ { "name": "Marc Morera", @@ -20,14 +20,14 @@ ], "require": { "php": ">=5.3.3", - "ext-gearman": "*", + "ext-gearman": ">=0.1", "symfony/framework-bundle": ">=2.1", - "doctrine/common": ">=2.2.0-dev", + "doctrine/common": ">=2.2", "symfony/console": ">=2.1", "symfony/config": ">=2.1", "symfony/http-kernel": ">=2.1", "symfony/dependency-injection": ">=2.1", - "liip/doctrine-cache-bundle": "dev-master" + "liip/doctrine-cache-bundle": "1.0.3" }, "target-dir": "Mmoreram/GearmanBundle", "autoload": {