Browse Source

Remove unset() and clean up tests.

Use mocks instead of writing to log files. Don't mock Connection, as it
is the class under test, and the mocked driver obviates the need to mock
connect().
Mark Story 8 years ago
parent
commit
6ea4e739a8
2 changed files with 13 additions and 35 deletions
  1. 1 2
      src/Database/Connection.php
  2. 12 33
      tests/TestCase/Database/ConnectionTest.php

+ 1 - 2
src/Database/Connection.php

@@ -129,9 +129,8 @@ class Connection implements ConnectionInterface
     public function __destruct()
     {
         if ($this->_transactionStarted && class_exists('Cake\Log\Log')) {
-            Log::warning('The connection is going to be closed but the transaction is still active.');
+            Log::warning('The connection is going to be closed but there is an active transaction.');
         }
-        unset($this->_driver);
     }
 
     /**

+ 12 - 33
tests/TestCase/Database/ConnectionTest.php

@@ -495,43 +495,22 @@ class ConnectionTest extends TestCase
      *
      * @return void
      */
-    public function testCloseConnectionWithUncommittedTransaction()
+    public function testDestructorWithUncommittedTransaction()
     {
         $driver = $this->getMockFormDriver();
-        $connection = $this->getMockBuilder('\Cake\Database\Connection')
-            ->setMethods(['connect'])
-            ->setConstructorArgs([['driver' => $driver]])
-            ->getMock();
+        $connection = new Connection(['driver' => $driver]);
+        $connection->begin();
+        $this->assertTrue($connection->inTransaction());
 
-        // The returned value of getMock() is referenced by TestCase,
-        // so it's impossible(or difficult) to call __destruct() of it during the test.
-        // Instead, use cloned object.
-        $clonedConnection = clone $connection;
-        $clonedConnection->begin();
-        $this->assertTrue($clonedConnection->inTransaction());
-
-        Log::setConfig('debug', [
-            'engine' => 'File',
-            'path' => LOGS,
-            'levels' => ['notice', 'info', 'debug'],
-            'file' => 'debug',
-        ]);
-        Log::setConfig('error', [
-            'engine' => 'File',
-            'path' => LOGS,
-            'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
-            'file' => 'error',
-        ]);
-        $errorLogFile = LOGS . 'error.log';
-        if (file_exists($errorLogFile)) {
-            unlink($errorLogFile);
-        }
+        $logger = $this->createMock('Psr\Log\AbstractLogger');
+        $logger->expects($this->once())
+            ->method('log')
+            ->with('warning', $this->stringContains('The connection is going to be closed'));
+
+        Log::setConfig('error', $logger);
 
-        // a warning log will be generated by __destruct of Connection.
-        unset($clonedConnection);
-        $this->assertFileExists($errorLogFile);
-        $logContent = file_get_contents($errorLogFile);
-        $this->assertStringMatchesFormat('%AWarning: The connection is going to be closed but the transaction is still active.%A', $logContent);
+        // Destroy the connection
+        unset($connection);
     }
 
     /**