-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow a prefix for model class names
- Loading branch information
1 parent
219e92a
commit 0ca5d55
Showing
9 changed files
with
250 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
class ModelPrefixingTest extends PHPUnit_Framework_TestCase { | ||
|
||
public function setUp() { | ||
// Set up the dummy database connection | ||
ORM::set_db(new MockPDO('sqlite::memory:')); | ||
|
||
// Enable logging | ||
ORM::configure('logging', true); | ||
|
||
Model::$auto_prefix_models = null; | ||
} | ||
|
||
public function tearDown() { | ||
ORM::configure('logging', false); | ||
ORM::set_db(null); | ||
|
||
Model::$auto_prefix_models = null; | ||
} | ||
|
||
public function testStaticPropertyExists() { | ||
$this->assertClassHasStaticAttribute('auto_prefix_models', 'Model'); | ||
$this->assertInternalType('null', Model::$auto_prefix_models); | ||
} | ||
|
||
public function testSettingAndUnsettingStaticPropertyValue() { | ||
$model_prefix = 'My_Model_Prefix_'; | ||
$this->assertInternalType('null', Model::$auto_prefix_models); | ||
Model::$auto_prefix_models = $model_prefix; | ||
$this->assertInternalType('string', Model::$auto_prefix_models); | ||
$this->assertEquals($model_prefix, Model::$auto_prefix_models); | ||
Model::$auto_prefix_models = null; | ||
$this->assertInternalType('null', Model::$auto_prefix_models); | ||
} | ||
|
||
public function testNoPrefixOnAutoTableName() { | ||
Model::$auto_prefix_models = null; | ||
Model::factory('Simple')->find_many(); | ||
$expected = 'SELECT * FROM `simple`'; | ||
$this->assertEquals($expected, ORM::get_last_query()); | ||
} | ||
|
||
public function testPrefixOnAutoTableName() { | ||
Model::$auto_prefix_models = 'MockPrefix_'; | ||
Model::factory('Simple')->find_many(); | ||
$expected = 'SELECT * FROM `mock_prefix_simple`'; | ||
$this->assertEquals($expected, ORM::get_last_query()); | ||
} | ||
|
||
public function testPrefixOnAutoTableNameWithTableSpecified() { | ||
Model::$auto_prefix_models = 'MockPrefix_'; | ||
Model::factory('TableSpecified')->find_many(); | ||
$expected = 'SELECT * FROM `simple`'; | ||
$this->assertEquals($expected, ORM::get_last_query()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Paris\Tests { | ||
use ORM, Model, MockPDO, PHPUnit_Framework_TestCase; | ||
|
||
class ModelPrefixingTest53 extends PHPUnit_Framework_TestCase { | ||
|
||
public function setUp() { | ||
// Set up the dummy database connection | ||
ORM::set_db(new MockPDO('sqlite::memory:')); | ||
|
||
// Enable logging | ||
ORM::configure('logging', true); | ||
|
||
Model::$auto_prefix_models = null; | ||
} | ||
|
||
public function tearDown() { | ||
ORM::configure('logging', false); | ||
ORM::set_db(null); | ||
|
||
Model::$auto_prefix_models = null; | ||
} | ||
|
||
public function testNoPrefixOnAutoTableName() { | ||
Model::$auto_prefix_models = null; | ||
Model::factory('\Tests\Simple')->find_many(); | ||
$expected = 'SELECT * FROM `tests_simple`'; | ||
$this->assertEquals($expected, ORM::get_last_query()); | ||
} | ||
|
||
public function testPrefixOnAutoTableName() { | ||
Model::$auto_prefix_models = '\\Tests\\'; | ||
Model::factory('Simple')->find_many(); | ||
$expected = 'SELECT * FROM `tests_simple`'; | ||
$this->assertEquals($expected, ORM::get_last_query()); | ||
} | ||
|
||
public function testPrefixOnAutoTableNameWithTableSpecified() { | ||
Model::$auto_prefix_models = '\\Tests\\'; | ||
Model::factory('TableSpecified')->find_many(); | ||
$expected = 'SELECT * FROM `simple`'; | ||
$this->assertEquals($expected, ORM::get_last_query()); | ||
} | ||
|
||
public function testNamespacePrefixSwitching() { | ||
Model::$auto_prefix_models = '\\Tests\\'; | ||
Model::factory('TableSpecified')->find_many(); | ||
$expected = 'SELECT * FROM `simple`'; | ||
$this->assertEquals($expected, ORM::get_last_query()); | ||
|
||
Model::$auto_prefix_models = '\\Tests2\\'; | ||
Model::factory('TableSpecified')->find_many(); | ||
$expected = 'SELECT * FROM `simple`'; | ||
$this->assertEquals($expected, ORM::get_last_query()); | ||
} | ||
} | ||
} | ||
|
||
namespace Tests { | ||
use ORM, Model, MockPDO; | ||
class Simple extends Model { } | ||
class TableSpecified extends Model { | ||
public static $_table = 'simple'; | ||
} | ||
} | ||
namespace Tests2 { | ||
use ORM, Model, MockPDO; | ||
class Simple extends Model { } | ||
class TableSpecified extends Model { | ||
public static $_table = 'simple'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
class MultipleConnectionsTest extends PHPUnit_Framework_TestCase { | ||
|
||
const ALTERNATE = 'alternate'; | ||
|
||
public function setUp() { | ||
|
||
// Set up the dummy database connection | ||
ORM::set_db(new MockPDO('sqlite::memory:')); | ||
ORM::set_db(new MockDifferentPDO('sqlite::memory:'), self::ALTERNATE); | ||
|
||
// Enable logging | ||
ORM::configure('logging', true); | ||
ORM::configure('logging', true, self::ALTERNATE); | ||
} | ||
|
||
public function tearDown() { | ||
ORM::configure('logging', false); | ||
ORM::configure('logging', false, self::ALTERNATE); | ||
|
||
ORM::set_db(null); | ||
ORM::set_db(null, self::ALTERNATE); | ||
} | ||
|
||
public function testMultipleConnections() { | ||
$simple = Model::factory('Simple')->find_one(1); | ||
$statement = ORM::get_last_statement(); | ||
$this->assertInstanceOf('MockPDOStatement', $statement); | ||
|
||
$simple = Model::factory('Simple', self::ALTERNATE); // Change the object's default connection | ||
$simple->find_one(1); | ||
$statement = ORM::get_last_statement(); | ||
$this->assertInstanceOf('MockDifferentPDOStatement', $statement); | ||
|
||
$temp = Model::factory('Simple', self::ALTERNATE)->find_one(1); | ||
$statement = ORM::get_last_statement(); | ||
$this->assertInstanceOf('MockDifferentPDOStatement', $statement); | ||
} | ||
|
||
public function testCustomConnectionName() { | ||
$person3 = Model::factory('ModelWithCustomConnection')->find_one(1); | ||
$statement = ORM::get_last_statement(); | ||
$this->assertInstanceOf('MockDifferentPDOStatement', $statement); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters