Эх сурвалжийг харах

Cleaner notation for expected exceptions in fixtures.

Niels Keurentjes 9 жил өмнө
parent
commit
523362c7c5

+ 1 - 1
tests/Composer/Test/Fixtures/installer/install-self-from-root.test

@@ -9,7 +9,7 @@ Tries to require a package with the same name as the root package
 }
 --RUN--
 install
---EXPECT-EXIT-CODE--
+--EXPECT-EXCEPTION--
 InvalidArgumentException
 --EXPECT--
 Root package 'foo/bar' cannot require itself in its composer.json

+ 19 - 9
tests/Composer/Test/InstallerTest.php

@@ -137,7 +137,7 @@ class InstallerTest extends TestCase
     /**
      * @dataProvider getIntegrationTests
      */
-    public function testIntegration($file, $message, $condition, $composerConfig, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectExitCode)
+    public function testIntegration($file, $message, $condition, $composerConfig, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectResult)
     {
         if ($condition) {
             eval('$res = '.$condition.';');
@@ -159,11 +159,11 @@ class InstallerTest extends TestCase
             ->will($this->returnCallback($callback));
 
         // Prepare for exceptions
-        if (is_int($expectExitCode) || ctype_digit($expectExitCode)) {
-            $expectExitCode = (int) $expectExitCode;
+        if (is_int($expectResult) || ctype_digit($expectResult)) {
+            $expectResult = (int) $expectResult;
         } else {
             $normalizedOutput = rtrim(str_replace("\n", PHP_EOL, $expect));
-            $this->setExpectedException($expectExitCode, $normalizedOutput);
+            $this->setExpectedException($expectResult, $normalizedOutput);
         }
 
         // Create Composer mock object according to configuration
@@ -242,12 +242,12 @@ class InstallerTest extends TestCase
         $appOutput = fopen('php://memory', 'w+');
         $result = $application->run(new StringInput($run), new StreamOutput($appOutput));
         fseek($appOutput, 0);
-        if (!is_int($expectExitCode)) {
+        if (!is_int($expectResult)) {
             // Shouldn't check output and results if an exception was expected by this point
             return;
         }
 
-        $this->assertEquals($expectExitCode, $result, $output . stream_get_contents($appOutput));
+        $this->assertEquals($expectResult, $result, $output . stream_get_contents($appOutput));
         if ($expectLock) {
             unset($actualLock['hash']);
             unset($actualLock['content-hash']);
@@ -279,7 +279,7 @@ class InstallerTest extends TestCase
             $installedDev = array();
             $lock = array();
             $expectLock = array();
-            $expectExitCode = 0;
+            $expectResult = 0;
 
             try {
                 $message = $testData['TEST'];
@@ -316,12 +316,21 @@ class InstallerTest extends TestCase
                 }
                 $expectOutput = isset($testData['EXPECT-OUTPUT']) ? $testData['EXPECT-OUTPUT'] : null;
                 $expect = $testData['EXPECT'];
-                $expectExitCode = isset($testData['EXPECT-EXIT-CODE']) ? $testData['EXPECT-EXIT-CODE'] : 0;
+                if (!empty($testData['EXPECT-EXCEPTION'])) {
+                    $expectResult = $testData['EXPECT-EXCEPTION'];
+                    if (!empty($testData['EXPECT-EXIT-CODE'])) {
+                        throw new \LogicException('EXPECT-EXCEPTION and EXPECT-EXIT-CODE are mutually exclusive');
+                    }
+                } elseif (!empty($testData['EXPECT-EXIT-CODE'])) {
+                    $expectResult = (int) $testData['EXPECT-EXIT-CODE'];
+                } else {
+                    $expectResult = 0;
+                }
             } catch (\Exception $e) {
                 die(sprintf('Test "%s" is not valid: '.$e->getMessage(), str_replace($fixturesDir.'/', '', $file)));
             }
 
-            $tests[basename($file)] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $composer, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectExitCode);
+            $tests[basename($file)] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $composer, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectResult);
         }
 
         return $tests;
@@ -341,6 +350,7 @@ class InstallerTest extends TestCase
             'EXPECT-LOCK' => false,
             'EXPECT-OUTPUT' => false,
             'EXPECT-EXIT-CODE' => false,
+            'EXPECT-EXCEPTION' => false,
             'EXPECT' => true,
         );