Browse Source

Ensure getMockForModel sets proper table name if it's not predefined

Jeremy Harris 9 years ago
parent
commit
59028cc77a
2 changed files with 20 additions and 0 deletions
  1. 4 0
      src/TestSuite/TestCase.php
  2. 16 0
      tests/TestCase/TestSuite/TestCaseTest.php

+ 4 - 0
src/TestSuite/TestCase.php

@@ -664,6 +664,10 @@ abstract class TestCase extends PHPUnit_Framework_TestCase
             }
         }
 
+        if (stripos($mock->table(), 'mock') === 0) {
+            $mock->table(Inflector::tableize($baseClass));
+        }
+
         TableRegistry::set($baseClass, $mock);
 
         return $mock;

+ 16 - 0
tests/TestCase/TestSuite/TestCaseTest.php

@@ -539,4 +539,20 @@ class TestCaseTest extends TestCase
         $this->assertTrue($Mock->save($entity));
         $this->assertFalse($Mock->save($entity));
     }
+
+    /**
+     * Test getting a table mock that doesn't have a preset table name sets the proper name
+     *
+     * @return void
+     */
+    public function testGetMockForModelSetTable()
+    {
+        Configure::write('App.namespace', 'TestApp');
+
+        $Tags = $this->getMockForModel('I18n', ['doSomething']);
+        $this->assertEquals('custom_i18n_table', $Tags->table());
+
+        $Tags = $this->getMockForModel('Tags', ['doSomething']);
+        $this->assertEquals('tags', $Tags->table());
+    }
 }