Browse Source

Merge pull request #15797 from cakephp/4.3-socket

Change visibility of public properties of `Socket` class.
Mark Story 4 years ago
parent
commit
aa83f8f9c1

+ 1 - 1
src/Mailer/Transport/SmtpTransport.php

@@ -115,7 +115,7 @@ class SmtpTransport extends AbstractTransport
      */
     public function connected(): bool
     {
-        return $this->_socket !== null && $this->_socket->connected;
+        return $this->_socket !== null && $this->_socket->isConnected();
     }
 
     /**

+ 79 - 15
src/Network/Socket.php

@@ -34,13 +34,6 @@ class Socket
     use InstanceConfigTrait;
 
     /**
-     * Object description
-     *
-     * @var string
-     */
-    public $description = 'Remote DataSource Network Socket Interface';
-
-    /**
      * Default configuration settings for the socket connection
      *
      * @var array
@@ -58,28 +51,28 @@ class Socket
      *
      * @var resource|null
      */
-    public $connection;
+    protected $connection;
 
     /**
      * This boolean contains the current state of the Socket class
      *
      * @var bool
      */
-    public $connected = false;
+    protected $connected = false;
 
     /**
      * This variable contains an array with the last error number (num) and string (str)
      *
      * @var array
      */
-    public $lastError = [];
+    protected $lastError = [];
 
     /**
      * True if the socket stream is encrypted after a Cake\Network\Socket::enableCrypto() call
      *
      * @var bool
      */
-    public $encrypted = false;
+    protected $encrypted = false;
 
     /**
      * Contains all the encryption methods available
@@ -87,7 +80,6 @@ class Socket
      * @var array
      */
     protected $_encryptMethods = [
-        // phpcs:disable
         'sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
         'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
         'tlsv10_client' => STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT,
@@ -97,8 +89,7 @@ class Socket
         'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER,
         'tlsv10_server' => STREAM_CRYPTO_METHOD_TLSv1_0_SERVER,
         'tlsv11_server' => STREAM_CRYPTO_METHOD_TLSv1_1_SERVER,
-        'tlsv12_server' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER
-        // phpcs:enable
+        'tlsv12_server' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER,
     ];
 
     /**
@@ -113,7 +104,7 @@ class Socket
      * Constructor.
      *
      * @param array $config Socket configuration, which will be merged with the base configuration
-     * @see \Cake\Network\Socket::$_baseConfig
+     * @see \Cake\Network\Socket::$_defaultConfig
      */
     public function __construct(array $config = [])
     {
@@ -193,6 +184,16 @@ class Socket
     }
 
     /**
+     * Check the connection status after calling `connect()`.
+     *
+     * @return bool
+     */
+    public function isConnected(): bool
+    {
+        return $this->connected;
+    }
+
+    /**
      * Create a stream socket client. Mock utility.
      *
      * @param string $remoteSocketTarget remote socket
@@ -501,4 +502,67 @@ class Socket
         $this->setLastError(null, $errorMessage);
         throw new SocketException($errorMessage);
     }
+
+    /**
+     * Check the encryption status after calling `enableCrypto()`.
+     *
+     * @return bool
+     */
+    public function isEncrypted(): bool
+    {
+        return $this->encrypted;
+    }
+
+    /**
+     * Temporary magic method to allow accessing protected properties.
+     *
+     * Will be removed in 5.0.
+     *
+     * @param string $name Property name.
+     * @return mixed
+     */
+    public function __get($name)
+    {
+        switch ($name) {
+            case 'connected':
+                deprecationWarning('The property `$connected` is deprecated, use `isConnected()` instead.');
+
+                return $this->connected;
+
+            case 'encrypted':
+                deprecationWarning('The property `$encrypted` is deprecated, use `isEncrypted()` instead.');
+
+                return $this->encrypted;
+
+            case 'lastError':
+                deprecationWarning('The property `$lastError` is deprecated, use `lastError()` instead.');
+
+                return $this->lastError;
+
+            case 'connection':
+                deprecationWarning('The property `$connection` is deprecated.');
+
+                return $this->connection;
+
+            case 'description':
+                deprecationWarning('The CakePHP team would love to know your use case for this property.');
+
+                return 'Remote DataSource Network Socket Interface';
+        }
+
+        $trace = debug_backtrace();
+        $parts = explode('\\', static::class);
+        trigger_error(
+            sprintf(
+                'Undefined property: %s::$%s in %s on line %s',
+                array_pop($parts),
+                $name,
+                $trace[0]['file'],
+                $trace[0]['line']
+            ),
+            E_USER_NOTICE
+        );
+
+        return null;
+    }
 }

+ 17 - 10
tests/TestCase/Mailer/Transport/SmtpTransportTest.php

@@ -18,6 +18,7 @@ namespace Cake\Test\TestCase\Mailer\Transport;
 
 use Cake\Mailer\Message;
 use Cake\Network\Exception\SocketException;
+use Cake\Network\Socket;
 use Cake\TestSuite\TestCase;
 use ReflectionProperty;
 use TestApp\Mailer\Transport\SmtpTestTransport;
@@ -56,8 +57,8 @@ class SmtpTransportTest extends TestCase
     public function setUp(): void
     {
         parent::setUp();
-        $this->socket = $this->getMockBuilder('Cake\Network\Socket')
-            ->onlyMethods(['read', 'write', 'connect', 'disconnect', 'enableCrypto'])
+        $this->socket = $this->getMockBuilder(Socket::class)
+            ->onlyMethods(['read', 'write', 'connect', 'disconnect', 'enableCrypto', 'isConnected'])
             ->getMock();
 
         $this->SmtpTransport = new SmtpTestTransport();
@@ -512,7 +513,8 @@ class SmtpTransportTest extends TestCase
     public function testQuit(): void
     {
         $this->socket->expects($this->once())->method('write')->with("QUIT\r\n");
-        $this->socket->connected = true;
+        $this->socket->expects($this->once())->method('isConnected')->willReturn(true);
+
         $this->SmtpTransport->disconnect();
     }
 
@@ -636,7 +638,8 @@ class SmtpTransportTest extends TestCase
     public function testExplicitConnectAlreadyConnected(): void
     {
         $this->socket->expects($this->never())->method('connect');
-        $this->socket->connected = true;
+        $this->socket->expects($this->once())->method('isConnected')->willReturn(true);
+
         $this->SmtpTransport->connect();
     }
 
@@ -645,10 +648,14 @@ class SmtpTransportTest extends TestCase
      */
     public function testConnected(): void
     {
-        $this->socket->connected = true;
-        $this->assertTrue($this->SmtpTransport->connected());
+        $this->socket->expects($this->exactly(2))
+            ->method('isConnected')
+            ->will($this->onConsecutiveCalls(
+                true,
+                false
+            ));
 
-        $this->socket->connected = false;
+        $this->assertTrue($this->SmtpTransport->connected());
         $this->assertFalse($this->SmtpTransport->connected());
     }
 
@@ -659,7 +666,7 @@ class SmtpTransportTest extends TestCase
     {
         $this->socket->expects($this->once())->method('write')->with("QUIT\r\n");
         $this->socket->expects($this->once())->method('disconnect');
-        $this->socket->connected = true;
+        $this->socket->expects($this->once())->method('isConnected')->willReturn(true);
         unset($this->SmtpTransport);
     }
 
@@ -670,7 +677,7 @@ class SmtpTransportTest extends TestCase
     {
         $this->socket->expects($this->once())->method('write')->with("QUIT\r\n");
         $this->socket->expects($this->once())->method('disconnect');
-        $this->socket->connected = true;
+        $this->socket->expects($this->once())->method('isConnected')->willReturn(true);
         $this->SmtpTransport->disconnect();
     }
 
@@ -745,7 +752,7 @@ class SmtpTransportTest extends TestCase
         $this->socket->expects($this->once())->method('connect')->will($this->returnValue(true));
 
         $this->SmtpTransport->send($message);
-        $this->socket->connected = true;
+        $this->socket->expects($this->once())->method('isConnected')->willReturn(true);
         $this->SmtpTransport->send($message);
     }
 

+ 21 - 9
tests/TestCase/Network/SocketTest.php

@@ -105,20 +105,20 @@ class SocketTest extends TestCase
      */
     public function testSocketConnection(): void
     {
-        $this->assertFalse($this->Socket->connected);
+        $this->assertFalse($this->Socket->isConnected());
         $this->Socket->disconnect();
-        $this->assertFalse($this->Socket->connected);
+        $this->assertFalse($this->Socket->isConnected());
         try {
             $this->Socket->connect();
-            $this->assertTrue($this->Socket->connected);
+            $this->assertTrue($this->Socket->isConnected());
             $this->Socket->connect();
-            $this->assertTrue($this->Socket->connected);
+            $this->assertTrue($this->Socket->isConnected());
 
             $this->Socket->disconnect();
             $config = ['persistent' => true];
             $this->Socket = new Socket($config);
             $this->Socket->connect();
-            $this->assertTrue($this->Socket->connected);
+            $this->assertTrue($this->Socket->isConnected());
         } catch (SocketException $e) {
             $this->markTestSkipped('Cannot test network, skipping.');
         }
@@ -400,9 +400,9 @@ class SocketTest extends TestCase
     public function testEnableCryptoEnableTls12(): void
     {
         $this->_connectSocketToSslTls();
-        $this->assertFalse($this->Socket->encrypted);
+        $this->assertFalse($this->Socket->isEncrypted());
         $this->Socket->enableCrypto('tlsv12', 'client', true);
-        $this->assertTrue($this->Socket->encrypted);
+        $this->assertTrue($this->Socket->isEncrypted());
     }
 
     /**
@@ -411,9 +411,9 @@ class SocketTest extends TestCase
     public function testEnableCryptoEnableStatus(): void
     {
         $this->_connectSocketToSslTls();
-        $this->assertFalse($this->Socket->encrypted);
+        $this->assertFalse($this->Socket->isEncrypted());
         $this->Socket->enableCrypto('tls', 'client', true);
-        $this->assertTrue($this->Socket->encrypted);
+        $this->assertTrue($this->Socket->isEncrypted());
     }
 
     /**
@@ -492,4 +492,16 @@ class SocketTest extends TestCase
         ]);
         $socket->connect();
     }
+
+    /**
+     * @return void
+     * @deprecated
+     */
+    public function testDeprecatedProps()
+    {
+        $this->deprecated(function () {
+            $this->assertFalse($this->Socket->connected);
+            $this->assertFalse($this->Socket->encrypted);
+        });
+    }
 }