Sfoglia il codice sorgente

green test for GEARMAN_WORK_EXECUTED

David Moreau 10 anni fa
parent
commit
92d09bab5e
1 ha cambiato i file con 73 aggiunte e 0 eliminazioni
  1. 73 0
      Tests/Service/GearmanExecuteTest.php

+ 73 - 0
Tests/Service/GearmanExecuteTest.php

@@ -13,7 +13,9 @@
 
 namespace Mmoreram\GearmanBundle\Tests\Service;
 
+use Mmoreram\GearmanBundle\Service\GearmanExecute;
 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+use Mmoreram\GearmanBundle\GearmanEvents;
 
 /**
  * Tests GearmanExecute class
@@ -36,4 +38,75 @@ class GearmanExecuteTest extends WebTestCase
                 ->get('gearman.execute')
         );
     }
+
+    public function testDispatchingEventsOnJob()
+    {
+        // Worker mock
+        $worker = $this->getMockBuilder('\GearmanWorker')
+            ->disableOriginalConstructor()
+            ->getMock();
+        $worker->method('addServer')->willReturn(true);
+
+        // Wrapper mock
+        $workers = array(
+            0 => array(
+                'className'    => "Mmoreram\\GearmanBundle\\Tests\\Service\\Mocks\\SingleCleanFile",
+                'fileName'     => dirname(__FILE__) . '/Mocks/SingleCleanFile.php',
+                'callableName' => null,
+                'description'  => "test",
+                'service'      => false,
+                'servers'      => array(),
+                'iterations'   => 1,
+                'jobs' => array(
+                    0 => array(
+                        'callableName'             => "test",
+                        'methodName'               => "test",
+                        'realCallableName'         => "test",
+                        'jobPrefix'                => NULL,
+                        'realCallableNameNoPrefix' => "test",
+                        'description'              => "test",
+                        'iterations'               => 1,
+                        'servers'                  => array(),
+                        'defaultMethod'            => "doBackground"
+                    )
+                )
+            )
+        );
+        $wrapper = $this->getMockBuilder('Mmoreram\GearmanBundle\Service\GearmanCacheWrapper')
+            ->disableOriginalConstructor()
+            ->getMock();
+        $wrapper->method('getWorkers')
+            ->willReturn($workers);
+
+        // Prepare a dispatcher to listen to tested events
+        $executedFlag = false;
+
+        $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
+        $dispatcher->addListener(GearmanEvents::GEARMAN_WORK_EXECUTED, function() use (&$executedFlag){
+            $executedFlag = true;
+        });
+
+        // Create the service under test
+        $service = new GearmanExecute($wrapper, array());
+        $service->setEventDispatcher($dispatcher);
+
+        // We need a job object, this part could be improved
+        $object = new \Mmoreram\GearmanBundle\Tests\Service\Mocks\SingleCleanFile();
+
+        // Finalize worker mock by making it call our job object
+        // This is normally handled by Gearman, but for test purpose we must simulate it
+        $worker->method('work')->will($this->returnCallback(function() use ($service, $object){
+            $service->handleJob(new \GearmanJob(), array(
+                'job_object_instance' => $object,
+                'job_method' => 'myMethod'
+            ));
+            return true;
+        }));
+
+        // Execute a job :)
+        $service->executeJob('test', $worker);
+
+        // Do we have the events ?
+        $this->assertTrue($executedFlag);
+    }
 }