Browse Source

Merge branch 'master' into 3.next

ADmad 7 years ago
parent
commit
b12ac33c22

+ 2 - 2
src/Console/CommandScanner.php

@@ -64,13 +64,13 @@ class CommandScanner
             App::path('Shell')[0],
             $appNamespace . '\Shell\\',
             '',
-            ['app']
+            []
         );
         $appCommands = $this->scanDir(
             App::path('Command')[0],
             $appNamespace . '\Command\\',
             '',
-            ['app']
+            []
         );
 
         return array_merge($appShells, $appCommands);

+ 5 - 2
src/Http/Client/Adapter/Stream.php

@@ -306,8 +306,11 @@ class Stream
         set_error_handler(function ($code, $message) {
             $this->_connectionErrors[] = $message;
         });
-        $this->_stream = fopen($url, 'rb', false, $this->_context);
-        restore_error_handler();
+        try {
+            $this->_stream = fopen($url, 'rb', false, $this->_context);
+        } finally {
+            restore_error_handler();
+        }
 
         if (!$this->_stream || !empty($this->_connectionErrors)) {
             throw new Exception(implode("\n", $this->_connectionErrors));

+ 2 - 0
tests/TestCase/Console/CommandCollectionTest.php

@@ -204,11 +204,13 @@ class CommandCollectionTest extends TestCase
         $collection = new CommandCollection();
         $collection->addMany($collection->autoDiscover());
 
+        $this->assertTrue($collection->has('app'));
         $this->assertTrue($collection->has('demo'));
         $this->assertTrue($collection->has('i18m'));
         $this->assertTrue($collection->has('sample'));
         $this->assertTrue($collection->has('testing_dispatch'));
 
+        $this->assertSame('TestApp\Shell\AppShell', $collection->get('app'));
         $this->assertSame('TestApp\Command\DemoCommand', $collection->get('demo'));
         $this->assertSame('TestApp\Shell\I18mShell', $collection->get('i18m'));
         $this->assertSame('TestApp\Shell\SampleShell', $collection->get('sample'));

+ 30 - 0
tests/TestCase/Http/Client/Adapter/StreamTest.php

@@ -35,6 +35,10 @@ class CakeStreamWrapper implements \ArrayAccess
 
     public function stream_open($path, $mode, $options, &$openedPath)
     {
+        if ($path == 'http://throw_exception/') {
+            throw new \Exception;
+        }
+
         $query = parse_url($path, PHP_URL_QUERY);
         if ($query) {
             parse_str($query, $this->_query);
@@ -152,6 +156,32 @@ class StreamTest extends TestCase
     }
 
     /**
+     * Test stream error_handler cleanup when wrapper throws exception
+     *
+     * @return void
+     */
+    public function testSendWrapperException()
+    {
+        $stream = new Stream();
+        $request = new Request('http://throw_exception/');
+
+        $currentHandler = set_error_handler(function () {
+        });
+        restore_error_handler();
+
+        try {
+            $stream->send($request, []);
+        } catch (\Exception $e) {
+        }
+
+        $newHandler = set_error_handler(function () {
+        });
+        restore_error_handler();
+
+        $this->assertEquals($currentHandler, $newHandler);
+    }
+
+    /**
      * Test building the context headers
      *
      * @return void

+ 24 - 0
tests/test_app/TestApp/Shell/AppShell.php

@@ -0,0 +1,24 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.6.5
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Shell;
+
+use Cake\Console\Shell;
+
+/**
+ * Stub class to ensure inclusion in command list.
+ */
+class AppShell extends Shell
+{
+}