Skip to content

Commit

Permalink
Pass the connection to mocked models.
Browse files Browse the repository at this point in the history
This fixes issues with partial mocks where functions needing the
connection would fail.

Refs #4113
  • Loading branch information
markstory committed Aug 2, 2014
1 parent 0208da7 commit c12c03e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
9 changes: 7 additions & 2 deletions src/TestSuite/TestCase.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
Expand Down Expand Up @@ -543,10 +544,14 @@ public function getMockForModel($alias, array $methods = array(), array $options
$options['className'] = $className;
}

$connectionName = $options['className']::defaultConnectionName();
$connection = ConnectionManager::get($connectionName);

list($plugin, $baseClass) = pluginSplit($alias);
$options += ['alias' => $baseClass] + TableRegistry::config($alias);
$options += ['alias' => $baseClass, 'connection' => $connection];
$options += TableRegistry::config($alias);

$mock = $this->getMock($options['className'], $methods, array($options));
$mock = $this->getMock($options['className'], $methods, [$options]);
TableRegistry::set($alias, $mock);
return $mock;
}
Expand Down
36 changes: 6 additions & 30 deletions tests/TestCase/TestSuite/TestCaseTest.php
Expand Up @@ -29,27 +29,6 @@
*/
class TestCaseTest extends TestCase {

/**
* setUp
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Reporter = $this->getMock('Cake\TestSuite\Reporter\HtmlReporter');
}

/**
* tearDown
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->Result);
unset($this->Reporter);
}

/**
* testAssertTags
*
Expand Down Expand Up @@ -332,17 +311,14 @@ public function testGetMockForModel() {
$this->assertNull($Posts->table());

$Posts = $this->getMockForModel('Posts', array('save'));

$this->assertNull($Posts->save($entity));

$Posts->expects($this->at(0))
->method('save')
->will($this->returnValue(true));
$Posts->expects($this->at(1))
->method('save')
->will($this->returnValue(false));
$this->assertTrue($Posts->save($entity));
$this->assertFalse($Posts->save($entity));
->will($this->returnValue('mocked'));
$this->assertEquals('mocked', $Posts->save($entity));

$Posts = $this->getMockForModel('Posts', ['doSomething']);
$this->assertInstanceOf('Cake\Database\Connection', $Posts->connection());
$this->assertEquals('test', $Posts->connection()->configName());
}

/**
Expand Down

0 comments on commit c12c03e

Please sign in to comment.