Browse Source

Pass the connection to mocked models.

This fixes issues with partial mocks where functions needing the
connection would fail.

Refs #4113
mark_story 11 years ago
parent
commit
c12c03e6c4
2 changed files with 13 additions and 32 deletions
  1. 7 2
      src/TestSuite/TestCase.php
  2. 6 30
      tests/TestCase/TestSuite/TestCaseTest.php

+ 7 - 2
src/TestSuite/TestCase.php

@@ -16,6 +16,7 @@ namespace Cake\TestSuite;
 
 use Cake\Core\App;
 use Cake\Core\Configure;
+use Cake\Datasource\ConnectionManager;
 use Cake\ORM\TableRegistry;
 use Cake\Routing\Router;
 use Cake\Utility\Inflector;
@@ -543,10 +544,14 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
 			$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;
 	}

+ 6 - 30
tests/TestCase/TestSuite/TestCaseTest.php

@@ -30,27 +30,6 @@ use Cake\Test\Fixture\FixturizedTestCase;
 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
  *
  * @return void
@@ -332,17 +311,14 @@ class TestCaseTest extends TestCase {
 		$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());
 	}
 
 /**