Skip to content

Commit

Permalink
Some new tests and assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Surt committed Nov 8, 2013
1 parent d84f9f1 commit dd5ca03
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
26 changes: 26 additions & 0 deletions tests/granada/EagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
113 changes: 113 additions & 0 deletions tests/granada/GranadaNewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

use Granada\Orm;
use Granada\Model;

/**
* Testing eager loading
*
* @author Peter Schumacher <peter@schumacher.dk>
*
* Modified by Tom van Oorschot <tomvanoorschot@gmail.com>
* 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');
}



}
1 change: 0 additions & 1 deletion tests/granada/GranadaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
12 changes: 12 additions & 0 deletions tests/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
Expand Down

0 comments on commit dd5ca03

Please # to comment.