Stephan Meyer 9 years ago
parent
commit
ec83a0d5ec
1 changed files with 138 additions and 77 deletions
  1. 138 77
      tests/TestCase/Database/Driver/SqlserverTest.php

+ 138 - 77
tests/TestCase/Database/Driver/SqlserverTest.php

@@ -1,4 +1,5 @@
 <?php
+
 /**
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
@@ -12,6 +13,7 @@
  * @since         3.0.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
+
 namespace Cake\Test\TestCase\Database\Driver;
 
 use Cake\Database\Query;
@@ -36,6 +38,66 @@ class SqlserverTest extends TestCase
     }
 
     /**
+     * data provider for testDnsString
+     *
+     * @return array
+     */
+    public function dnsStringDataProvider()
+    {
+        return [
+            [
+                [
+                    'app' => 'CakePHP-Testapp',
+                    'connectionPooling' => true,
+                    'failoverPartner' => 'failover.local',
+                    'loginTimeout' => 10,
+                    'multiSubnetFailover' => 'failover.local',
+                ],
+                'sqlsrv:Server=localhost\SQLEXPRESS;Database=cake;MultipleActiveResultSets=false;APP=CakePHP-Testapp;ConnectionPooling=1;Failover_Partner=failover.local;LoginTimeout=10;MultiSubnetFailover=failover.local',
+            ],
+            [
+                [
+                    'app' => 'CakePHP-Testapp',
+                    'failoverPartner' => 'failover.local',
+                    'multiSubnetFailover' => 'failover.local',
+                ],
+                'sqlsrv:Server=localhost\SQLEXPRESS;Database=cake;MultipleActiveResultSets=false;APP=CakePHP-Testapp;Failover_Partner=failover.local;MultiSubnetFailover=failover.local',
+            ],
+            [
+                [
+                ],
+                'sqlsrv:Server=localhost\SQLEXPRESS;Database=cake;MultipleActiveResultSets=false',
+            ]
+        ];
+    }
+
+    /**
+     * Test if all options in dns string are set
+     *
+     * @dataProvider dnsStringDataProvider
+     * @param array $constructorArgs
+     * @param string $dnsString
+     *
+     * @return void
+     */
+    public function testDnsString($constructorArgs, $dnsString)
+    {
+        $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlserver')
+                ->setMethods(['_connect'])
+                ->setConstructorArgs([$constructorArgs])
+                ->getMock();
+
+        $driver->method('_connect')
+            ->with($this->callback(
+                function ($dns) use ($dnsString) {
+                    return $dns === $dnsString;
+                })
+            )
+            ->will($this->returnValue([]));
+        $driver->connect();
+    }
+
+    /**
      * Test connecting to Sqlserver with custom configuration
      *
      * @return void
@@ -55,9 +117,9 @@ class SqlserverTest extends TestCase
             'settings' => ['config1' => 'value1', 'config2' => 'value2'],
         ];
         $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlserver')
-            ->setMethods(['_connect', 'connection'])
-            ->setConstructorArgs([$config])
-            ->getMock();
+                ->setMethods(['_connect', 'connection'])
+                ->setConstructorArgs([$config])
+                ->getMock();
         $dsn = 'sqlsrv:Server=foo;Database=bar;MultipleActiveResultSets=false';
 
         $expected = $config;
@@ -75,15 +137,13 @@ class SqlserverTest extends TestCase
         $expected['multiSubnetFailover'] = null;
 
         $connection = $this->getMockBuilder('stdClass')
-            ->setMethods(['exec', 'quote'])
-            ->getMock();
+                ->setMethods(['exec', 'quote'])
+                ->getMock();
         $connection->expects($this->any())
-            ->method('quote')
-            ->will($this->onConsecutiveCalls(
-                $this->returnArgument(0),
-                $this->returnArgument(0),
-                $this->returnArgument(0)
-            ));
+                ->method('quote')
+                ->will($this->onConsecutiveCalls(
+                                $this->returnArgument(0), $this->returnArgument(0), $this->returnArgument(0)
+        ));
 
         $connection->expects($this->at(0))->method('exec')->with('Execute this');
         $connection->expects($this->at(1))->method('exec')->with('this too');
@@ -92,10 +152,10 @@ class SqlserverTest extends TestCase
 
         $driver->connection($connection);
         $driver->expects($this->once())->method('_connect')
-            ->with($dsn, $expected);
+                ->with($dsn, $expected);
 
         $driver->expects($this->any())->method('connection')
-            ->will($this->returnValue($connection));
+                ->will($this->returnValue($connection));
 
         $driver->connect();
     }
@@ -108,48 +168,48 @@ class SqlserverTest extends TestCase
     public function testSelectLimitVersion12()
     {
         $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlserver')
-            ->setMethods(['_connect', 'getConnection', '_version'])
-            ->setConstructorArgs([[]])
-            ->getMock();
+                ->setMethods(['_connect', 'getConnection', '_version'])
+                ->setConstructorArgs([[]])
+                ->getMock();
         $driver
-            ->expects($this->any())
-            ->method('_version')
-            ->will($this->returnValue(12));
+                ->expects($this->any())
+                ->method('_version')
+                ->will($this->returnValue(12));
 
         $connection = $this->getMockBuilder('\Cake\Database\Connection')
-            ->setMethods(['connect', 'getDriver', 'setDriver'])
-            ->setConstructorArgs([['log' => false]])
-            ->getMock();
+                ->setMethods(['connect', 'getDriver', 'setDriver'])
+                ->setConstructorArgs([['log' => false]])
+                ->getMock();
         $connection
-            ->expects($this->any())
-            ->method('getDriver')
-            ->will($this->returnValue($driver));
+                ->expects($this->any())
+                ->method('getDriver')
+                ->will($this->returnValue($driver));
 
         $query = new Query($connection);
         $query->select(['id', 'title'])
-            ->from('articles')
-            ->order(['id'])
-            ->offset(10);
+                ->from('articles')
+                ->order(['id'])
+                ->offset(10);
         $this->assertEquals('SELECT id, title FROM articles ORDER BY id OFFSET 10 ROWS', $query->sql());
 
         $query = new Query($connection);
         $query->select(['id', 'title'])
-            ->from('articles')
-            ->order(['id'])
-            ->limit(10)
-            ->offset(50);
+                ->from('articles')
+                ->order(['id'])
+                ->limit(10)
+                ->offset(50);
         $this->assertEquals('SELECT id, title FROM articles ORDER BY id OFFSET 50 ROWS FETCH FIRST 10 ROWS ONLY', $query->sql());
 
         $query = new Query($connection);
         $query->select(['id', 'title'])
-            ->from('articles')
-            ->offset(10);
+                ->from('articles')
+                ->offset(10);
         $this->assertEquals('SELECT id, title FROM articles ORDER BY (SELECT NULL) OFFSET 10 ROWS', $query->sql());
 
         $query = new Query($connection);
         $query->select(['id', 'title'])
-            ->from('articles')
-            ->limit(10);
+                ->from('articles')
+                ->limit(10);
         $this->assertEquals('SELECT TOP 10 id, title FROM articles', $query->sql());
     }
 
@@ -161,59 +221,59 @@ class SqlserverTest extends TestCase
     public function testSelectLimitOldServer()
     {
         $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlserver')
-            ->setMethods(['_connect', 'getConnection', '_version'])
-            ->setConstructorArgs([[]])
-            ->getMock();
+                ->setMethods(['_connect', 'getConnection', '_version'])
+                ->setConstructorArgs([[]])
+                ->getMock();
         $driver
-            ->expects($this->any())
-            ->method('_version')
-            ->will($this->returnValue(8));
+                ->expects($this->any())
+                ->method('_version')
+                ->will($this->returnValue(8));
 
         $connection = $this->getMockBuilder('\Cake\Database\Connection')
-            ->setMethods(['connect', 'getDriver', 'setDriver'])
-            ->setConstructorArgs([['log' => false]])
-            ->getMock();
+                ->setMethods(['connect', 'getDriver', 'setDriver'])
+                ->setConstructorArgs([['log' => false]])
+                ->getMock();
         $connection
-            ->expects($this->any())
-            ->method('getDriver')
-            ->will($this->returnValue($driver));
+                ->expects($this->any())
+                ->method('getDriver')
+                ->will($this->returnValue($driver));
 
         $query = new Query($connection);
         $query->select(['id', 'title'])
-            ->from('articles')
-            ->limit(10);
+                ->from('articles')
+                ->limit(10);
         $expected = 'SELECT TOP 10 id, title FROM articles';
         $this->assertEquals($expected, $query->sql());
 
         $query = new Query($connection);
         $query->select(['id', 'title'])
-            ->from('articles')
-            ->offset(10);
+                ->from('articles')
+                ->offset(10);
         $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) AS [_cake_page_rownum_] ' .
-            'FROM articles) _cake_paging_ ' .
-            'WHERE _cake_paging_._cake_page_rownum_ > 10';
+                'FROM articles) _cake_paging_ ' .
+                'WHERE _cake_paging_._cake_page_rownum_ > 10';
         $this->assertEquals($expected, $query->sql());
 
         $query = new Query($connection);
         $query->select(['id', 'title'])
-            ->from('articles')
-            ->order(['id'])
-            ->offset(10);
+                ->from('articles')
+                ->order(['id'])
+                ->offset(10);
         $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY id)) AS [_cake_page_rownum_] ' .
-            'FROM articles) _cake_paging_ ' .
-            'WHERE _cake_paging_._cake_page_rownum_ > 10';
+                'FROM articles) _cake_paging_ ' .
+                'WHERE _cake_paging_._cake_page_rownum_ > 10';
         $this->assertEquals($expected, $query->sql());
 
         $query = new Query($connection);
         $query->select(['id', 'title'])
-            ->from('articles')
-            ->order(['id'])
-            ->where(['title' => 'Something'])
-            ->limit(10)
-            ->offset(50);
+                ->from('articles')
+                ->order(['id'])
+                ->where(['title' => 'Something'])
+                ->limit(10)
+                ->offset(50);
         $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY id)) AS [_cake_page_rownum_] ' .
-            'FROM articles WHERE title = :c0) _cake_paging_ ' .
-            'WHERE (_cake_paging_._cake_page_rownum_ > 50 AND _cake_paging_._cake_page_rownum_ <= 60)';
+                'FROM articles WHERE title = :c0) _cake_paging_ ' .
+                'WHERE (_cake_paging_._cake_page_rownum_ > 50 AND _cake_paging_._cake_page_rownum_ <= 60)';
         $this->assertEquals($expected, $query->sql());
     }
 
@@ -225,22 +285,23 @@ class SqlserverTest extends TestCase
     public function testInsertUsesOutput()
     {
         $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlserver')
-            ->setMethods(['_connect', 'getConnection'])
-            ->setConstructorArgs([[]])
-            ->getMock();
+                ->setMethods(['_connect', 'getConnection'])
+                ->setConstructorArgs([[]])
+                ->getMock();
         $connection = $this->getMockBuilder('\Cake\Database\Connection')
-            ->setMethods(['connect', 'getDriver', 'setDriver'])
-            ->setConstructorArgs([['log' => false]])
-            ->getMock();
+                ->setMethods(['connect', 'getDriver', 'setDriver'])
+                ->setConstructorArgs([['log' => false]])
+                ->getMock();
         $connection
-            ->expects($this->any())
-            ->method('getDriver')
-            ->will($this->returnValue($driver));
+                ->expects($this->any())
+                ->method('getDriver')
+                ->will($this->returnValue($driver));
         $query = new Query($connection);
         $query->insert(['title'])
-            ->into('articles')
-            ->values(['title' => 'A new article']);
+                ->into('articles')
+                ->values(['title' => 'A new article']);
         $expected = 'INSERT INTO articles (title) OUTPUT INSERTED.* VALUES (:c0)';
         $this->assertEquals($expected, $query->sql());
     }
+
 }