Skip to content

Commit

Permalink
Remove use of TestCase::getMock() from "Database" namepace tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jun 5, 2016
1 parent 3c8d034 commit a6f8945
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 157 deletions.
58 changes: 30 additions & 28 deletions tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -112,7 +112,10 @@ public function testMissingDriver()
*/
public function testDisabledDriver()
{
$mock = $this->getMock('\Cake\Database\Connection\Driver', ['enabled'], [], 'DriverMock');
$mock = $this->getMockBuilder('\Cake\Database\Connection\Driver')
->setMethods(['enabled'])
->setMockClassName('DriverMock')
->getMock();
$connection = new Connection(['driver' => $mock]);
}

Expand Down Expand Up @@ -686,7 +689,9 @@ public function testQuote()
*/
public function testQuoteIdentifier()
{
$driver = $this->getMock('Cake\Database\Driver\Sqlite', ['enabled']);
$driver = $this->getMockBuilder('Cake\Database\Driver\Sqlite')
->setMethods(['enabled'])
->getMock();
$driver->expects($this->once())
->method('enabled')
->will($this->returnValue(true));
Expand Down Expand Up @@ -869,11 +874,10 @@ public function testLogBeginRollbackTransaction()
public function testLogCommitTransaction()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect'],
[['driver' => $driver]]
);
$connection = $this->getMockBuilder('\Cake\Database\Connection')
->setMethods(['connect'])
->setConstructorArgs([['driver' => $driver]])
->getMock();

$logger = $this->getMockBuilder('\Cake\Database\Log\QueryLogger')->getMock();
$connection->logger($logger);
Expand All @@ -897,11 +901,10 @@ public function testLogCommitTransaction()
public function testTransactionalSuccess()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect', 'commit', 'begin'],
[['driver' => $driver]]
);
$connection = $this->getMockBuilder('\Cake\Database\Connection')
->setMethods(['connect', 'commit', 'begin'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
$connection->expects($this->at(0))->method('begin');
$connection->expects($this->at(1))->method('commit');
$result = $connection->transactional(function ($conn) use ($connection) {
Expand All @@ -920,11 +923,10 @@ public function testTransactionalSuccess()
public function testTransactionalFail()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect', 'commit', 'begin', 'rollback'],
[['driver' => $driver]]
);
$connection = $this->getMockBuilder('\Cake\Database\Connection')
->setMethods(['connect', 'commit', 'begin', 'rollback'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
$connection->expects($this->at(0))->method('begin');
$connection->expects($this->at(1))->method('rollback');
$connection->expects($this->never())->method('commit');
Expand All @@ -946,11 +948,10 @@ public function testTransactionalFail()
public function testTransactionalWithException()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect', 'commit', 'begin', 'rollback'],
[['driver' => $driver]]
);
$connection = $this->getMockBuilder('\Cake\Database\Connection')
->setMethods(['connect', 'commit', 'begin', 'rollback'])
->setConstructorArgs([['driver' => $driver]])
->getMock();
$connection->expects($this->at(0))->method('begin');
$connection->expects($this->at(1))->method('rollback');
$connection->expects($this->never())->method('commit');
Expand All @@ -968,16 +969,17 @@ public function testTransactionalWithException()
public function testSchemaCollection()
{
$driver = $this->getMockFormDriver();
$connection = $this->getMock(
'\Cake\Database\Connection',
['connect'],
[['driver' => $driver]]
);
$connection = $this->getMockBuilder('\Cake\Database\Connection')
->setMethods(['connect'])
->setConstructorArgs([['driver' => $driver]])
->getMock();

$schema = $connection->schemaCollection();
$this->assertInstanceOf('Cake\Database\Schema\Collection', $schema);

$schema = $this->getMock('Cake\Database\Schema\Collection', [], [$connection]);
$schema = $this->getMockBuilder('Cake\Database\Schema\Collection')
->setConstructorArgs([$connection])
->getMock();
$connection->schemaCollection($schema);
$this->assertSame($schema, $connection->schemaCollection());
}
Expand Down
22 changes: 13 additions & 9 deletions tests/TestCase/Database/Driver/MysqlTest.php
Expand Up @@ -14,7 +14,6 @@
*/
namespace Cake\Test\TestCase\Database\Driver;

use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use \PDO;
Expand Down Expand Up @@ -45,7 +44,9 @@ public function setup()
*/
public function testConnectionConfigDefault()
{
$driver = $this->getMock('Cake\Database\Driver\Mysql', ['_connect', 'connection']);
$driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')
->setMethods(['_connect', 'connection'])
->getMock();
$dsn = 'mysql:host=localhost;port=3306;dbname=cake;charset=utf8';
$expected = [
'persistent' => true,
Expand All @@ -65,7 +66,9 @@ public function testConnectionConfigDefault()
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];
$connection = $this->getMock('StdClass', ['exec']);
$connection = $this->getMockBuilder('StdClass')
->setMethods(['exec'])
->getMock();

$driver->expects($this->once())->method('_connect')
->with($dsn, $expected);
Expand Down Expand Up @@ -98,11 +101,10 @@ public function testConnectionConfigCustom()
'this too',
]
];
$driver = $this->getMock(
'Cake\Database\Driver\Mysql',
['_connect', 'connection'],
[$config]
);
$driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')
->setMethods(['_connect', 'connection'])
->setConstructorArgs([$config])
->getMock();
$dsn = 'mysql:host=foo;port=3440;dbname=bar;charset=a-language';
$expected = $config;
$expected['init'][] = "SET time_zone = 'Antartica'";
Expand All @@ -113,7 +115,9 @@ public function testConnectionConfigCustom()
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];

$connection = $this->getMock('StdClass', ['exec']);
$connection = $this->getMockBuilder('StdClass')
->setMethods(['exec'])
->getMock();
$connection->expects($this->at(0))->method('exec')->with('Execute this');
$connection->expects($this->at(1))->method('exec')->with('this too');
$connection->expects($this->at(2))->method('exec')->with("SET time_zone = 'Antartica'");
Expand Down
31 changes: 17 additions & 14 deletions tests/TestCase/Database/Driver/PostgresTest.php
Expand Up @@ -14,7 +14,6 @@
*/
namespace Cake\Test\TestCase\Database\Driver;

use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use \PDO;

Expand All @@ -31,7 +30,9 @@ class PostgresTest extends TestCase
*/
public function testConnectionConfigDefault()
{
$driver = $this->getMock('Cake\Database\Driver\Postgres', ['_connect', 'connection']);
$driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
->setMethods(['_connect', 'connection'])
->getMock();
$dsn = 'pgsql:host=localhost;port=5432;dbname=cake';
$expected = [
'persistent' => true,
Expand All @@ -53,7 +54,9 @@ public function testConnectionConfigDefault()
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

$connection = $this->getMock('stdClass', ['exec', 'quote']);
$connection = $this->getMockBuilder('stdClass')
->setMethods(['exec', 'quote'])
->getMock();
$connection->expects($this->any())
->method('quote')
->will($this->onConsecutiveCalls(
Expand Down Expand Up @@ -94,11 +97,10 @@ public function testConnectionConfigCustom()
'schema' => 'fooblic',
'init' => ['Execute this', 'this too']
];
$driver = $this->getMock(
'Cake\Database\Driver\Postgres',
['_connect', 'connection'],
[$config]
);
$driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
->setMethods(['_connect', 'connection'])
->setConstructorArgs([$config])
->getMock();
$dsn = 'pgsql:host=foo;port=3440;dbname=bar';

$expected = $config;
Expand All @@ -108,7 +110,9 @@ public function testConnectionConfigCustom()
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

$connection = $this->getMock('stdClass', ['exec', 'quote']);
$connection = $this->getMockBuilder('stdClass')
->setMethods(['exec', 'quote'])
->getMock();
$connection->expects($this->any())
->method('quote')
->will($this->onConsecutiveCalls(
Expand Down Expand Up @@ -141,11 +145,10 @@ public function testConnectionConfigCustom()
*/
public function testInsertReturning()
{
$driver = $this->getMock(
'Cake\Database\Driver\Postgres',
['_connect', 'connection'],
[[]]
);
$driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
->setMethods(['_connect', 'connection'])
->setConstructorArgs([[]])
->getMock();
$connection = $this
->getMockBuilder('\Cake\Database\Connection')
->setMethods(['connect'])
Expand Down
24 changes: 14 additions & 10 deletions tests/TestCase/Database/Driver/SqliteTest.php
Expand Up @@ -14,9 +14,8 @@
*/
namespace Cake\Test\TestCase\Database\Driver;

use Cake\Core\Configure;
use Cake\Database\Driver\Sqlite;
use Cake\Testsuite\TestCase;
use Cake\TestSuite\TestCase;
use \PDO;

/**
Expand All @@ -32,7 +31,9 @@ class SqliteTest extends TestCase
*/
public function testConnectionConfigDefault()
{
$driver = $this->getMock('Cake\Database\Driver\Sqlite', ['_connect']);
$driver = $this->getMockBuilder('Cake\Database\Driver\Sqlite')
->setMethods(['_connect'])
->getMock();
$dsn = 'sqlite::memory:';
$expected = [
'persistent' => false,
Expand Down Expand Up @@ -69,11 +70,10 @@ public function testConnectionConfigCustom()
'encoding' => 'a-language',
'init' => ['Execute this', 'this too']
];
$driver = $this->getMock(
'Cake\Database\driver\Sqlite',
['_connect', 'connection'],
[$config]
);
$driver = $this->getMockBuilder('Cake\Database\driver\Sqlite')
->setMethods(['_connect', 'connection'])
->setConstructorArgs([$config])
->getMock();
$dsn = 'sqlite:bar.db';

$expected = $config;
Expand All @@ -84,7 +84,9 @@ public function testConnectionConfigCustom()
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

$connection = $this->getMock('StdClass', ['exec']);
$connection = $this->getMockBuilder('StdClass')
->setMethods(['exec'])
->getMock();
$connection->expects($this->at(0))->method('exec')->with('Execute this');
$connection->expects($this->at(1))->method('exec')->with('this too');
$connection->expects($this->exactly(2))->method('exec');
Expand Down Expand Up @@ -125,7 +127,9 @@ public static function schemaValueProvider()
public function testSchemaValue($input, $expected)
{
$driver = new Sqlite();
$mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
$mock = $this->getMockBuilder('FakePdo')
->setMethods(['quote', 'quoteIdentifier'])
->getMock();
$mock->expects($this->any())
->method('quote')
->will($this->returnCallback(function ($value) {
Expand Down

0 comments on commit a6f8945

Please sign in to comment.