Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / psy / psysh / test / ShellTest.php
similarity index 66%
rename from vendor/psy/psysh/test/Psy/Test/ShellTest.php
rename to vendor/psy/psysh/test/ShellTest.php
index 01dddc8ee92cddc50f8430fb1a8183e78ec716c5..1fb78b230908407147b141e43d78f807b1764945 100644 (file)
@@ -3,7 +3,7 @@
 /*
  * This file is part of Psy Shell.
  *
- * (c) 2012-2017 Justin Hileman
+ * (c) 2012-2018 Justin Hileman
  *
  * For the full copyright and license information, please view the LICENSE
  * file that was distributed with this source code.
@@ -20,7 +20,7 @@ use Symfony\Component\Console\Output\StreamOutput;
 
 class ShellTest extends \PHPUnit\Framework\TestCase
 {
-    private $streams = array();
+    private $streams = [];
 
     public function tearDown()
     {
@@ -42,20 +42,20 @@ class ShellTest extends \PHPUnit\Framework\TestCase
         $shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
 
         $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
-        $this->assertEquals(array('one', 'two', 'three', '_'), $shell->getScopeVariableNames());
-        $this->assertEquals('banana', $shell->getScopeVariable('one'));
-        $this->assertEquals(123, $shell->getScopeVariable('two'));
+        $this->assertSame(['one', 'two', 'three', '_'], $shell->getScopeVariableNames());
+        $this->assertSame('banana', $shell->getScopeVariable('one'));
+        $this->assertSame(123, $shell->getScopeVariable('two'));
         $this->assertSame($three, $shell->getScopeVariable('three'));
         $this->assertNull($shell->getScopeVariable('_'));
 
-        $shell->setScopeVariables(array());
-        $this->assertEquals(array('_'), $shell->getScopeVariableNames());
+        $shell->setScopeVariables([]);
+        $this->assertSame(['_'], $shell->getScopeVariableNames());
 
         $shell->setBoundObject($this);
-        $this->assertEquals(array('_', 'this'), $shell->getScopeVariableNames());
+        $this->assertSame(['_', 'this'], $shell->getScopeVariableNames());
         $this->assertSame($this, $shell->getScopeVariable('this'));
-        $this->assertEquals(array('_' => null), $shell->getScopeVariables(false));
-        $this->assertEquals(array('_' => null, 'this' => $this), $shell->getScopeVariables());
+        $this->assertSame(['_' => null], $shell->getScopeVariables(false));
+        $this->assertSame(['_' => null, 'this' => $this], $shell->getScopeVariables());
     }
 
     /**
@@ -64,44 +64,82 @@ class ShellTest extends \PHPUnit\Framework\TestCase
     public function testUnknownScopeVariablesThrowExceptions()
     {
         $shell = new Shell($this->getConfig());
-        $shell->setScopeVariables(array('foo' => 'FOO', 'bar' => 1));
+        $shell->setScopeVariables(['foo' => 'FOO', 'bar' => 1]);
         $shell->getScopeVariable('baz');
     }
 
+    public function testIncludesWithScopeVariables()
+    {
+        $one       = 'banana';
+        $two       = 123;
+        $three     = new \StdClass();
+        $__psysh__ = 'ignore this';
+        $_         = 'ignore this';
+        $_e        = 'ignore this';
+
+        $config = $this->getConfig(['usePcntl' => false]);
+
+        $shell = new Shell($config);
+        $shell->setScopeVariables(compact('one', 'two', 'three', '__psysh__', '_', '_e', 'this'));
+        $shell->addInput('exit', true);
+
+        // This is super slow and we shouldn't do this :(
+        $shell->run(null, $this->getOutput());
+
+        $this->assertNotContains('__psysh__', $shell->getScopeVariableNames());
+        $this->assertSame(['one', 'two', 'three', '_', '_e'], $shell->getScopeVariableNames());
+        $this->assertSame('banana', $shell->getScopeVariable('one'));
+        $this->assertSame(123, $shell->getScopeVariable('two'));
+        $this->assertSame($three, $shell->getScopeVariable('three'));
+        $this->assertNull($shell->getScopeVariable('_'));
+    }
+
     public function testIncludes()
     {
-        $config = $this->getConfig(array('configFile' => __DIR__ . '/../../fixtures/empty.php'));
+        $config = $this->getConfig(['configFile' => __DIR__ . '/fixtures/empty.php']);
 
         $shell = new Shell($config);
         $this->assertEmpty($shell->getIncludes());
-        $shell->setIncludes(array('foo', 'bar', 'baz'));
-        $this->assertEquals(array('foo', 'bar', 'baz'), $shell->getIncludes());
+        $shell->setIncludes(['foo', 'bar', 'baz']);
+        $this->assertSame(['foo', 'bar', 'baz'], $shell->getIncludes());
     }
 
     public function testIncludesConfig()
     {
-        $config = $this->getConfig(array(
-            'defaultIncludes' => array('/file.php'),
-            'configFile'      => __DIR__ . '/../../fixtures/empty.php',
-        ));
+        $config = $this->getConfig([
+            'defaultIncludes' => ['/file.php'],
+            'configFile'      => __DIR__ . '/fixtures/empty.php',
+        ]);
 
         $shell = new Shell($config);
 
         $includes = $shell->getIncludes();
-        $this->assertEquals('/file.php', $includes[0]);
+        $this->assertSame('/file.php', $includes[0]);
     }
 
     public function testAddMatchersViaConfig()
     {
-        $config = $this->getConfig(array(
-            'tabCompletionMatchers' => array(
-                new ClassMethodsMatcher(),
-            ),
-        ));
+        $shell = new FakeShell();
+        $matcher = new ClassMethodsMatcher();
 
-        $matchers = $config->getTabCompletionMatchers();
+        $config = $this->getConfig([
+            'matchers' => [$matcher],
+        ]);
+        $config->setShell($shell);
 
-        $this->assertTrue(array_pop($matchers) instanceof ClassMethodsMatcher);
+        $this->assertSame([$matcher], $shell->matchers);
+    }
+
+    public function testAddMatchersViaConfigAfterShell()
+    {
+        $shell = new FakeShell();
+        $matcher = new ClassMethodsMatcher();
+
+        $config = $this->getConfig([]);
+        $config->setShell($shell);
+        $config->addMatchers([$matcher]);
+
+        $this->assertSame([$matcher], $shell->matchers);
     }
 
     public function testRenderingExceptions()
@@ -200,7 +238,7 @@ class ShellTest extends \PHPUnit\Framework\TestCase
         $this->assertFalse($shell->hasCode());
         $code = preg_replace('/\s+/', ' ', $code);
         $this->assertNotNull($code);
-        $this->assertEquals('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
+        $this->assertSame('class a { } return new \\Psy\\CodeCleaner\\NoReturnValue();', $code);
     }
 
     public function testKeepCodeBufferOpen()
@@ -220,7 +258,7 @@ class ShellTest extends \PHPUnit\Framework\TestCase
         $this->assertFalse($shell->hasCode());
         $code = preg_replace('/\s+/', ' ', $code);
         $this->assertNotNull($code);
-        $this->assertEquals('return 1 + 1 + 1;', $code);
+        $this->assertSame('return 1 + 1 + 1;', $code);
     }
 
     /**
@@ -241,7 +279,7 @@ class ShellTest extends \PHPUnit\Framework\TestCase
         $shell->flushCode();
         $code = '$test()';
         $shell->addCode($code);
-        $this->assertEquals($shell->flushCode(), 'return $test();');
+        $this->assertSame($shell->flushCode(), 'return $test();');
     }
 
     public function testWriteStdout()
@@ -256,7 +294,7 @@ class ShellTest extends \PHPUnit\Framework\TestCase
         rewind($stream);
         $streamContents = stream_get_contents($stream);
 
-        $this->assertEquals('{{stdout}}' . PHP_EOL, $streamContents);
+        $this->assertSame('{{stdout}}' . PHP_EOL, $streamContents);
     }
 
     public function testWriteStdoutWithoutNewline()
@@ -271,7 +309,7 @@ class ShellTest extends \PHPUnit\Framework\TestCase
         rewind($stream);
         $streamContents = stream_get_contents($stream);
 
-        $this->assertEquals('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
+        $this->assertSame('{{stdout}}<aside>⏎</aside>' . PHP_EOL, $streamContents);
     }
 
     /**
@@ -291,10 +329,10 @@ class ShellTest extends \PHPUnit\Framework\TestCase
 
     public function getReturnValues()
     {
-        return array(
-            array('{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL),
-            array(1, "=> \033[35m1\033[39m" . PHP_EOL),
-        );
+        return [
+            ['{{return value}}', "=> \"\033[32m{{return value}}\033[39m\"" . PHP_EOL],
+            [1, "=> \033[35m1\033[39m" . PHP_EOL],
+        ];
     }
 
     /**
@@ -309,14 +347,37 @@ class ShellTest extends \PHPUnit\Framework\TestCase
 
         $shell->writeException($exception);
         rewind($stream);
-        $this->assertEquals($expected, stream_get_contents($stream));
+        $this->assertSame($expected, stream_get_contents($stream));
     }
 
     public function getRenderedExceptions()
     {
-        return array(
-            array(new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL),
-        );
+        return [
+            [new \Exception('{{message}}'), "Exception with message '{{message}}'" . PHP_EOL],
+        ];
+    }
+
+    /**
+     * @dataProvider getExecuteValues
+     */
+    public function testShellExecute($input, $expected)
+    {
+        $output = $this->getOutput();
+        $stream = $output->getStream();
+        $shell  = new Shell($this->getConfig());
+        $shell->setOutput($output);
+        $this->assertEquals($expected, $shell->execute($input));
+        rewind($stream);
+        $this->assertSame('', stream_get_contents($stream));
+    }
+
+    public function getExecuteValues()
+    {
+        return [
+            ['return 12', 12],
+            ['"{{return value}}"', '{{return value}}'],
+            ['1', '1'],
+        ];
     }
 
     private function getOutput()
@@ -329,17 +390,17 @@ class ShellTest extends \PHPUnit\Framework\TestCase
         return $output;
     }
 
-    private function getConfig(array $config = array())
+    private function getConfig(array $config = [])
     {
         // Mebbe there's a better way than this?
         $dir = tempnam(sys_get_temp_dir(), 'psysh_shell_test_');
         unlink($dir);
 
-        $defaults = array(
+        $defaults = [
             'configDir'  => $dir,
             'dataDir'    => $dir,
             'runtimeDir' => $dir,
-        );
+        ];
 
         return new Configuration(array_merge($defaults, $config));
     }