Kaynağa Gözat

Improved tests using dataProvider

mmoreram 11 yıl önce
ebeveyn
işleme
25559e369d
2 değiştirilmiş dosya ile 193 ekleme ve 212 silme
  1. 79 64
      Tests/Module/JobStatusTest.php
  2. 114 148
      Tests/Service/GearmanParserTest.php

+ 79 - 64
Tests/Module/JobStatusTest.php

@@ -2,7 +2,7 @@
 
 /**
  * Gearman Bundle for Symfony2
- * 
+ *
  * @author Marc Morera <yuhu@mmoreram.com>
  * @since 2013
  */
@@ -18,85 +18,100 @@ class JobStatusTest extends \PHPUnit_Framework_TestCase
 {
 
     /**
-     * Testing when job does not exist
+     * Testing job status
+     *
+     * @dataProvider dataProvider
      */
-    public function testJobStatusNonExistant()
+    public function testJobStatusNonExistant($known, $running, $completed, $completionTotal, $isKnown, $isRunning, $getCompleted, $getCompletionTotal, $getCompletionPercent, $isFinished)
     {
         $jobStatus = new JobStatus(array(
-            false,
-            false,
-            null,
-            null
+            $known,
+            $running,
+            $completed,
+            $completionTotal
         ));
 
-        $this->assertFalse($jobStatus->isKnown());
-        $this->assertFalse($jobStatus->isRunning());
-        $this->assertEquals($jobStatus->getCompleted(), 0);
-        $this->assertEquals($jobStatus->getCompletionTotal(), 0);
-        $this->assertEquals($jobStatus->getCompletionPercent(), 0);
-        $this->assertFalse($jobStatus->isFinished());
+        $this->assertEquals($jobStatus->isKnown(), $isKnown);
+        $this->assertEquals($jobStatus->isRunning(), $isRunning);
+        $this->assertEquals($jobStatus->getCompleted(), $getCompleted);
+        $this->assertEquals($jobStatus->getCompletionTotal(), $getCompletionTotal);
+        $this->assertEquals($jobStatus->getCompletionPercent(), $getCompletionPercent);
+        $this->assertEquals($jobStatus->isFinished(), $isFinished);
     }
 
 
     /**
-     * Testing when job is started
+     * Data provider
      */
-    public function testJobStatusStarted()
+    public function dataProvider()
     {
-        $jobStatus = new JobStatus(array(
-            true,
-            true,
-            0,
-            10
-        ));
+        return array(
 
-        $this->assertTrue($jobStatus->isKnown());
-        $this->assertTrue($jobStatus->isRunning());
-        $this->assertEquals($jobStatus->getCompleted(), 0);
-        $this->assertEquals($jobStatus->getCompletionTotal(), 10);
-        $this->assertEquals($jobStatus->getCompletionPercent(), 0);
-        $this->assertFalse($jobStatus->isFinished());
-    }
+            /**
+             * Testing when job does not exist
+             */
+            array(
+                false,
+                false,
+                null,
+                null,
+                false,
+                false,
+                0,
+                0,
+                0,
+                false
+            ),
 
+            /**
+             * Testing when job is started
+             */
+            array(
+                true,
+                true,
+                0,
+                10,
+                true,
+                true,
+                0,
+                10,
+                0,
+                false
+            ),
 
-    /**
-     * Testing when job is still running
-     */
-    public function testJobStatusRunning()
-    {
-        $jobStatus = new JobStatus(array(
-            true,
-            true,
-            5,
-            10
-        ));
-
-        $this->assertTrue($jobStatus->isKnown());
-        $this->assertTrue($jobStatus->isRunning());
-        $this->assertEquals($jobStatus->getCompleted(), 5);
-        $this->assertEquals($jobStatus->getCompletionTotal(), 10);
-        $this->assertEquals($jobStatus->getCompletionPercent(), 0.5);
-        $this->assertFalse($jobStatus->isFinished());
-    }
+            /**
+             * Testing when job is still running
+             */
+            array(
+                true,
+                true,
+                5,
+                10,
+                true,
+                true,
+                5,
+                10,
+                0.5,
+                false
+            ),
 
+            /**
+             * Testing when job is already finished
+             */
+            array(
+                true,
+                false,
+                10,
+                10,
+                true,
+                false,
+                10,
+                10,
+                1,
+                true
+            ),
 
-    /**
-     * Testing when job is already finished
-     */
-    public function testJobStatusFinished()
-    {
-        $jobStatus = new JobStatus(array(
-            true,
-            false,
-            10,
-            10
-        ));
 
-        $this->assertTrue($jobStatus->isKnown());
-        $this->assertFalse($jobStatus->isRunning());
-        $this->assertEquals($jobStatus->getCompleted(), 10);
-        $this->assertEquals($jobStatus->getCompletionTotal(), 10);
-        $this->assertEquals($jobStatus->getCompletionPercent(), 1);
-        $this->assertTrue($jobStatus->isFinished());
+        );
     }
 }

+ 114 - 148
Tests/Service/GearmanParserTest.php

@@ -117,154 +117,6 @@ class GearmanParserTest extends WebTestCase
     }
 
 
-    /**
-     * Testing loadNamespaceMap without Include and Exclude values
-     */
-    public function testLoadNamespaceMapSimple()
-    {
-
-        $this->bundleMock
-            ->expects($this->once())
-            ->method('getPath')
-            ->will($this->returnValue($this->bundlePath));
-
-        $this->kernelBundles = array(
-
-            "FirstBundleName" => $this->bundleMock,
-        );
-
-        list($paths, $excludedPaths) = $this->gearmanParser->loadNamespaceMap($this->kernelBundles, array(
-            "FirstBundle" => array(
-                "name"      =>  "FirstBundleName",
-                "active"    =>  true,
-                "include"   =>  array(),
-                "ignore"    =>  array(),
-            ),
-        ));
-
-        $this->assertEquals($paths, array($this->bundlePath . '/'));
-        $this->assertEquals($excludedPaths, array());
-    }
-
-
-    /**
-     * Testing loadNamespaceMap with just Include values
-     */
-    public function testLoadNamespaceMapIncludes()
-    {
-
-        $this->bundleMock
-            ->expects($this->once())
-            ->method('getPath')
-            ->will($this->returnValue($this->bundlePath));
-
-        $this->kernelBundles = array(
-
-            "FirstBundleName" => $this->bundleMock,
-        );
-
-        list($paths, $excludedPaths) = $this->gearmanParser->loadNamespaceMap($this->kernelBundles, array(
-            "FirstBundle" => array(
-                "name"      =>  "FirstBundleName",
-                "active"    =>  true,
-                "include"   =>  array(
-                    'Services',
-                    'Workers',
-                ),
-                "ignore"    =>  array(),
-            ),
-        ));
-
-        $this->assertEquals($paths, array(
-            $this->bundlePath . '/Services/',
-            $this->bundlePath . '/Workers/',
-        ));
-        $this->assertEquals($excludedPaths, array());
-    }
-
-
-    /**
-     * Testing loadNamespaceMap with just exclude values
-     */
-    public function testLoadNamespaceMapExcludes()
-    {
-
-        $this->bundleMock
-            ->expects($this->once())
-            ->method('getPath')
-            ->will($this->returnValue($this->bundlePath));
-
-        $this->kernelBundles = array(
-
-            "FirstBundleName" => $this->bundleMock,
-        );
-
-        list($paths, $excludedPaths) = $this->gearmanParser->loadNamespaceMap($this->kernelBundles, array(
-            "FirstBundle" => array(
-                "name"      =>  "FirstBundleName",
-                "active"    =>  true,
-                "include"   =>  array(),
-                "ignore"    =>  array(
-                    'Services',
-                    'Workers',
-                ),
-            ),
-        ));
-
-        $this->assertEquals($paths, array($this->bundlePath . '/'));
-        $this->assertEquals($excludedPaths, array(
-            'Services',
-            'Workers',
-        ));
-    }
-
-
-    /**
-     * Testing loadNamespaceMap with includes and exclude values
-     */
-    public function testLoadNamespaceMapBoth()
-    {
-
-        $this
-            ->bundleMock
-            ->expects($this->once())
-            ->method('getPath')
-            ->will($this->returnValue($this->bundlePath));
-
-        $this->kernelBundles = array(
-
-            "FirstBundleName" => $this->bundleMock,
-        );
-
-        list($paths, $excludedPaths) = $this->gearmanParser->loadNamespaceMap($this->kernelBundles, array(
-            "FirstBundle" => array(
-                "name"      =>  "FirstBundleName",
-                "active"    =>  true,
-                "include"   =>  array(
-                    'Controllers',
-                    'libs'
-                ),
-                "ignore"    =>  array(
-                    'Services',
-                    'Workers',
-                    'libs',
-                ),
-            ),
-        ));
-
-        $this->assertEquals($paths, array(
-            $this->bundlePath . '/Controllers/',
-            $this->bundlePath . '/libs/',
-
-        ));
-        $this->assertEquals($excludedPaths, array(
-            'Services',
-            'Workers',
-            'libs',
-        ));
-    }
-
-
     /**
      * Testing parseNamespaceMap with empty paths
      */
@@ -345,13 +197,127 @@ class GearmanParserTest extends WebTestCase
     }
 
 
+    /**
+     * Testing parseNamespaceMap with some paths
+     *
+     * @dataProvider loadNamespaceMapDataProvider
+     */
+    public function testLoadNamespaceMap($active, $include, $ignore, $expectedPaths, $expectedExcludedPaths)
+    {
+        $this
+            ->bundleMock
+            ->expects($this->any())
+            ->method('getPath')
+            ->will($this->returnValue($this->bundlePath));
 
+        $this->kernelBundles = array(
 
+            "FirstBundleName" => $this->bundleMock,
+        );
 
+        list($paths, $excludedPaths) = $this->gearmanParser->loadNamespaceMap($this->kernelBundles, array(
+            "FirstBundle" => array(
+                "name"      =>  "FirstBundleName",
+                "active"    =>  $active,
+                "include"   =>  $include,
+                "ignore"    =>  $ignore,
+            ),
+        ));
 
+        $this->assertEquals($paths, $expectedPaths);
+        $this->assertEquals($excludedPaths, $expectedExcludedPaths);
+    }
 
 
+    /**
+     * Load namespace map Data Provider
+     */
+    public function loadNamespaceMapDataProvider()
+    {
+        return array(
 
+            // Testing loadNamespaceMap with includes and exclude values
+            array(
+                true,
+                array(
+                    'Controllers',
+                    'libs'
+                ),
+                array(
+                    'Services',
+                    'Workers',
+                    'libs',
+                ),
+                array(
+                    $this->bundlePath . '/Controllers/',
+                    $this->bundlePath . '/libs/',
+                ),
+                array(
+                    'Services',
+                    'Workers',
+                    'libs',
+                )
+            ),
+
+            // Testing loadNamespaceMap without Include and Exclude values
+            array(
+                true,
+                array(),
+                array(),
+                array(
+                    $this->bundlePath . '/',
+                ),
+                array(),
+            ),
+
+            // Testing loadNamespaceMap with just exclude values
+            array(
+                true,
+                array(),
+                array(
+                    'Services',
+                    'Workers',
+                ),
+                array(
+                    $this->bundlePath . '/',
+                ),
+                array(
+                    'Services',
+                    'Workers',
+                ),
+            ),
+
+            // Testing loadNamespaceMap with just Include values
+            array(
+                true,
+                array(
+                    'Services',
+                    'Workers',
+                ),
+                array(),
+                array(
+                    $this->bundlePath . '/Services/',
+                    $this->bundlePath . '/Workers/',
+                ),
+                array(),
+            ),
+
+            // Testing loadNamespaceMap with invalid bundle
+            array(
+                false,
+                array(
+                    'Services',
+                    'Workers',
+                ),
+                array(
+                    'Ignore',
+                    'Tests',
+                ),
+                array(),
+                array(),
+            )
+        );
+    }