diff --git a/tests/granada/EagerTest.php b/tests/granada/EagerTest.php index 4e4ae46..471f939 100644 --- a/tests/granada/EagerTest.php +++ b/tests/granada/EagerTest.php @@ -236,6 +236,32 @@ public function testFindManyWithHasManyThrough() { } + public function testChainedRelationships() { + $owner = Owner::with(array('car'=>array('with'=>'manufactor')))->find_one(1); + $fullQueryLog = ORM::get_query_log(); + // Return last three queries + $actualSql = array_slice($fullQueryLog, count($fullQueryLog) - 3); + + $expectedSql = array(); + $expectedSql[] = "SELECT * FROM `owner` WHERE `id` = '1' LIMIT 1"; + $expectedSql[] = "SELECT * FROM `car` WHERE `owner_id` IN ('1')"; + $expectedSql[] = "SELECT * FROM `manufactor` WHERE `id` IN ('1')"; + + $this->assertEquals($expectedSql, $actualSql); + } + + public function testChainedAdterHas_Many_Through() { + $car = Car::with(array('parts'=>array('with'=>'cars')))->find_one(1); + $test_exists = $car->as_array(); + $test_exists = $car->parts->as_array(); + foreach($car->parts as $part){ + foreach($part->cars as $car){ + $test_exists = $car->as_array(); + }; + } + // NO FATAL ERRORS OR EXCEPTIONS THROW + } + public function testLazyLoading() { $owner = Model::factory('Owner')->find_one(1); $this->assertEquals($owner->car->manufactor_id, 1); diff --git a/tests/granada/GranadaNewTest.php b/tests/granada/GranadaNewTest.php new file mode 100644 index 0000000..dc7e026 --- /dev/null +++ b/tests/granada/GranadaNewTest.php @@ -0,0 +1,113 @@ + + * + * Modified by Tom van Oorschot + * Additions: + * - Test will also check for double records on a has_many relation + */ +class GranadaNewTest extends PHPUnit_Framework_TestCase { + + public function setUp() { + + // The tests for eager loading requires a real database. + // Set up SQLite in memory + ORM::set_db(new PDO('sqlite::memory:')); + + // Create schemas and populate with data + ORM::get_db()->exec(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '..'.DIRECTORY_SEPARATOR.'models.sql')); + + // Enable logging + ORM::configure('logging', true); + } + + public function tearDown() { + ORM::configure('logging', false); + ORM::set_db(null); + } + + public function testGetter(){ + $car = Model::factory('Car')->find_one(1); + $expected = 'test'; + $this->assertEquals($expected, $car->nonExistentProperty); + + $car = Model::factory('Car')->find_one(1); + $expected = 'Car1'; + $this->assertEquals($expected, $car->get('name')); + } + + public function testSetterForProperty(){ + $car = Model::factory('Car')->find_one(1); + $car->name = 'Car1'; + $car->save(); + $expected = 'test'; + $this->assertEquals($expected, $car->name); + } + + public function testSetterForRelationship(){ + $car = Model::factory('Car')->with('manufactor')->find_one(1); + $expected = 'Manufactor1'; + $this->assertEquals($expected, $car->manufactor->name, 'Relationship loaded'); + + $expected = 'test'; + $car->manufactor = 'test'; + + $this->assertEquals($expected, $car->relationships['manufactor'], 'Relationship overloaded'); + } + + public function testCallStaticForModel(){ + $expected = Model::factory('Car')->with('manufactor')->find_one(1); + $car = Car::with('manufactor')->find_one(1); + $this->assertEquals($expected, $car, 'Call from static and from factory are the same'); + } + + public function testPluck(){ + $id = Car::where_id_is(1)->pluck('id'); + $this->assertEquals(1, $id, 'PLuck a column'); + } + + public function testfindPairs(){ + $pairs = Car::find_pairs('id', 'name'); + $expected = array( + '1' => 'Car1', + '2' => 'Car2', + '3' => 'Car3', + '4' => 'Car4' + ); + $this->assertEquals($expected, $pairs); + } + + public function testfilters(){ + $car = Car::byName('Car1')->find_one(); + $this->assertEquals($car->name, 'Car1'); + } + + /** + * @expectedException Exception + */ + public function testnonExistentFilter(){ + $car = Car::test('Car1')->find_one(); + } + + public function testInsert(){ + Car::insert(array( + array( + 'id'=> '20', + 'name' =>'Car20', + 'manufactor_id'=> 1, + 'owner_id'=> 1 + ) + )); + $count = Car::count(); + $this->assertEquals(5, $count, 'Car must be Inserted'); + } + + + +} \ No newline at end of file diff --git a/tests/granada/GranadaTest.php b/tests/granada/GranadaTest.php index 6ca30df..df59900 100644 --- a/tests/granada/GranadaTest.php +++ b/tests/granada/GranadaTest.php @@ -148,5 +148,4 @@ public function testHasManyThroughRelationWithCustomIntermediateModelAndKeyNames $expected = "SELECT `author`.* FROM `author` JOIN `author_book` ON `author`.`id` = `author_book`.`custom_author_id` WHERE `author_book`.`custom_book_id` = '1'"; $this->assertEquals($expected, ORM::get_last_query()); } - } \ No newline at end of file diff --git a/tests/models.php b/tests/models.php index 267cdad..3853fef 100644 --- a/tests/models.php +++ b/tests/models.php @@ -38,6 +38,18 @@ public function owner() { public function parts() { return $this->has_many_through('Part'); } + + public function get_nonExistentProperty(){ + return "test"; + } + + public function set_name($value){ + return 'test'; + } + + public static function filter_byName($query, $name){ + return $query->where('name', $name); + } } class CarPart extends Model { }