From 6d74f5d696b1e24b256e173500394b208fe71859 Mon Sep 17 00:00:00 2001 From: Joshua Paine Date: Sun, 3 May 2009 05:15:35 +0800 Subject: [PATCH 1/5] Make Library append closing ?> if omitted from class file Signed-off-by: Kris Jordan Signed-off-by: Kris Jordan --- recess/recess/lang/Library.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/recess/recess/lang/Library.class.php b/recess/recess/lang/Library.class.php index 2f53631..73de5c5 100644 --- a/recess/recess/lang/Library.class.php +++ b/recess/recess/lang/Library.class.php @@ -93,7 +93,8 @@ static function persistNamedRuns() { $path = self::$paths[$classInfo[self::PATH]]; $fileName = str_replace(self::dotSeparator,self::pathSeparator, $fullName) . self::CLASS_FILE_EXTENSION; $classFile = $path . $fileName; - $code = file_get_contents($classFile); + $code = rtrim(file_get_contents($classFile)); + if(substr($code,-2)!='?'.'>') $code .= ('?'.'>'); fwrite($file, $code); } From abf690b90582191b4f7c771be2b50759828709f7 Mon Sep 17 00:00:00 2001 From: KrisJordan Date: Tue, 5 May 2009 01:08:41 +0800 Subject: [PATCH 2/5] Fix to named runs feature in Library that protects against concurrent appends to the cached named run file. Signed-off-by: Kris Jordan --- recess/recess/cache/Cache.class.php | 33 +++++++++++++------ recess/recess/framework/Application.class.php | 3 +- recess/recess/lang/ClassDescriptor.class.php | 1 + recess/recess/lang/Library.class.php | 32 ++++++++++++++---- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/recess/recess/cache/Cache.class.php b/recess/recess/cache/Cache.class.php index 8967f87..f32b964 100644 --- a/recess/recess/cache/Cache.class.php +++ b/recess/recess/cache/Cache.class.php @@ -233,17 +233,30 @@ class SqliteCacheProvider implements ICacheProvider { function __construct() { $this->pdo = new Pdo('sqlite:' . $_ENV['dir.temp'] . 'sqlite-cache.db'); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - try { - $this->setStatement = $this->pdo->prepare('INSERT OR REPLACE INTO cache (key,value,expire) values (:key,:value,:expire)'); - $this->getStatement = $this->pdo->prepare('SELECT value,expire FROM cache WHERE key = :key'); - $this->getManyStatement = $this->pdo->prepare('SELECT value,expire,key FROM cache WHERE key LIKE :key'); - } catch(PDOException $e) { - $this->pdo->exec('CREATE TABLE "cache" ("key" TEXT PRIMARY KEY NOT NULL , "value" TEXT NOT NULL , "expire" INTEGER NOT NULL)'); - $this->pdo->exec('CREATE INDEX "expiration" ON "cache" ("expire" ASC)'); - $this->setStatement = $this->pdo->prepare('INSERT OR REPLACE INTO cache (key,value,expire) values (:key,:value,:expire)'); - $this->getStatement = $this->pdo->prepare('SELECT value,expire FROM cache WHERE key = :key'); - $this->getManyStatement = $this->pdo->prepare('SELECT value,expire,key FROM cache WHERE key LIKE :key'); + + $tries = 0; + while($tries < 2) { + try { + $this->setStatement = $this->pdo->prepare('INSERT OR REPLACE INTO cache (key,value,expire) values (:key,:value,:expire)'); + $this->getStatement = $this->pdo->prepare('SELECT value,expire FROM cache WHERE key = :key'); + $this->getManyStatement = $this->pdo->prepare('SELECT value,expire,key FROM cache WHERE key LIKE :key'); + break; + } catch(PDOException $e) { + + try { + $this->pdo->exec('CREATE TABLE "cache" ("key" TEXT PRIMARY KEY NOT NULL , "value" TEXT NOT NULL , "expire" INTEGER NOT NULL)'); + $this->pdo->exec('CREATE INDEX "expiration" ON "cache" ("expire" ASC)'); + } catch(PDOException $e) { + if($tries == 1) { + die('Could not create cache table'); + } + sleep(1); + $tries++; + continue; + } + } } + $this->time = time(); } diff --git a/recess/recess/framework/Application.class.php b/recess/recess/framework/Application.class.php index 6fe5c88..8ce5f9b 100644 --- a/recess/recess/framework/Application.class.php +++ b/recess/recess/framework/Application.class.php @@ -1,7 +1,6 @@ $missedClasses) { @@ -78,15 +80,27 @@ static function persistNamedRuns() { $namedRunFile = $namedRunDir . $namedRun . self::PHP_EXTENSION; if(!empty($missedClasses)) { - if(file_exists($namedRunFile)) { // append to - $file = fopen($namedRunFile,'a'); + $tempNamedRunFile = ''; + $tries = 0; + do { + $tempNamedRunFile = $namedRunFile . '.' . rand(0,32768); + } while(file_exists($tempNamedRunFile)); + + if(file_exists($namedRunFile)) { + copy($namedRunFile, $tempNamedRunFile); + $file = fopen($tempNamedRunFile,'a'); + if(filesize($tempNamedRunFile) !== self::$namedRunFileSize) { + fclose($file); + unlink($tempNamedRunFile); + return; + } } else { if(!file_exists($namedRunDir)) { - mkdir($namedRunDir); + mkdir($tempNamedRunFile); } - $file = fopen($namedRunFile,'w'); + $file = fopen($tempNamedRunFile,'w'); } - + foreach($missedClasses as $class) { $classInfo = self::$classesByClass[$class]; $fullName = $classInfo[self::NAME]; @@ -99,6 +113,10 @@ static function persistNamedRuns() { } fclose($file); + + copy($tempNamedRunFile, $namedRunFile); + + unlink($tempNamedRunFile); } } } From a8b7f0c1a17f3018f4ed5b1f28488e37ff0cd770 Mon Sep 17 00:00:00 2001 From: zdk Date: Sat, 29 Aug 2009 14:57:00 +0700 Subject: [PATCH 3/5] Add SQL IN statement and its test. (But not test yet wait for Sandbox setting up on slow internet) --- .../recess/database/pdo/PdoDataSet.class.php | 6 + .../database/sql/ISqlConditions.class.php | 1 + .../recess/database/sql/SqlBuilder.class.php | 16 + .../test/recess/database/PdoDsnSettings.php | 2 +- recess/test/recess/database/orm/ModelTest.php | 820 +++++++++--------- .../recess/database/pdo/PdoDataSetTest.php | 7 + .../database/sql/SelectSqlBuilderTest.php | 6 + 7 files changed, 450 insertions(+), 408 deletions(-) diff --git a/recess/recess/database/pdo/PdoDataSet.class.php b/recess/recess/database/pdo/PdoDataSet.class.php index 28e8319..3d46d38 100644 --- a/recess/recess/database/pdo/PdoDataSet.class.php +++ b/recess/recess/database/pdo/PdoDataSet.class.php @@ -341,5 +341,11 @@ function range($start, $finish) { $copy = clone $this; $copy->sqlBuilder->range( * @return PdoDataSet */ function orderBy($clause) { $copy = clone $this; $copy->sqlBuilder->orderBy($clause); return $copy; } + + /** + * @see SqlBuilder::in + * @return PdoDataSet + */ + function in($lhs, $rhs) { $copy = clone $this; $copy->sqlBuilder->in($lhs,$rhs); return $copy; } } ?> \ No newline at end of file diff --git a/recess/recess/database/sql/ISqlConditions.class.php b/recess/recess/database/sql/ISqlConditions.class.php index b1f8ef0..007b780 100644 --- a/recess/recess/database/sql/ISqlConditions.class.php +++ b/recess/recess/database/sql/ISqlConditions.class.php @@ -19,6 +19,7 @@ function lessThan($column, $value); function lessThanOrEqualTo($column, $value); function like($column, $value); function notLike($column, $value); + function in($column, $value); } ?> \ No newline at end of file diff --git a/recess/recess/database/sql/SqlBuilder.class.php b/recess/recess/database/sql/SqlBuilder.class.php index 7ec26b7..cbf13c4 100644 --- a/recess/recess/database/sql/SqlBuilder.class.php +++ b/recess/recess/database/sql/SqlBuilder.class.php @@ -338,6 +338,16 @@ public function isNull($column) { return $this->addCondition($column, nul */ public function isNotNull($column) { return $this->addCondition($column, null, Criterion::IS_NOT_NULL); } + /** + * IN to expression for WHERE clause of update, delete, or select statements. + * + * @param string $column + * @param array $value + * @return SqlBuilder + */ + public function in($column, $value) { return $this->addCondition($column, $value, Criterion::IN); } + + /** * Add a condition to the SqlBuilder statement. Additional logic here to prepend * a table name and also keep track of which columns have already been assigned conditions @@ -703,6 +713,12 @@ public function getQueryParameter() { if(is_numeric($this->value)) { return $this->value; } + + if(is_array($this->value)) { + $hack_value = '('.join(',', $this->value).')'; + return $hack_value; + } + // End workaround if($this->operator == self::ASSIGNMENT) { diff --git a/recess/test/recess/database/PdoDsnSettings.php b/recess/test/recess/database/PdoDsnSettings.php index 92f7643..a25970f 100644 --- a/recess/test/recess/database/PdoDsnSettings.php +++ b/recess/test/recess/database/PdoDsnSettings.php @@ -1,5 +1,5 @@ \ No newline at end of file diff --git a/recess/test/recess/database/orm/ModelTest.php b/recess/test/recess/database/orm/ModelTest.php index 04cf5b8..16c639a 100644 --- a/recess/test/recess/database/orm/ModelTest.php +++ b/recess/test/recess/database/orm/ModelTest.php @@ -102,414 +102,420 @@ class Page extends Model {} abstract class ModelTest extends PHPUnit_Extensions_Database_TestCase { protected $source; - function getDataSet() { - Databases::setDefaultSource($this->source); - Object::clearDescriptors(); - return $this->createXMLDataSet('recess/database/orm/sample-data.xml'); - } - - function tearDown() { - unset($this->source); - } - - function testHierarchy() { - $page = new Page(); - $page = $page->equal('id',1)->first(); - $this->assertEquals($page->children()->count(), 1); - $this->assertEquals($page->parent(), false); - $this->assertEquals($page->children()->equal('title','Child 1')->count(), 1); - } - - function testAll() { - $person = new Person(); - $people = $person->all(); - $this->assertEquals(count($people),6); - - $genera = Make::a('Genera')->equal('title','Social Healing')->first(); - $this->assertEquals($genera->title, 'Social Healing'); - $this->assertEquals($genera->id, 3); - } - - function testFindCriteria() { - $person = new Person(); - $person->age = 23; - $people = $person->find()->orderBy('lastName DESC'); - $this->assertEquals(count($people),2); - $this->assertEquals($people[0]->lastName, 'Sutherland'); - $this->assertEquals(get_class($people[0]), 'Person'); - } - - function testBooleanType() { - $car = new Car(); - $car->isDriveable = true; - $car->insert(); - $carId = $car->pk; - - $car = new Car(); - $car->pk = $carId; - $driveable = $car->find()->first()->isDriveable; - - $this->assertEquals($driveable === true, true); - } - - function testFindTailCriteria() { - $person = new Person(); - $people = $person->find()->greaterThan('age',22); - $this->assertEquals(count($people),4); - } - - function testBetween() { - $people = Make::a('Person')->all()->between('age',21,25); - $this->assertEquals(count($people), 4); - } - - function testMultipleConditionsOnSingleProperty() { - $people = Make::a('Person')->greaterThan('age',21)->lessThan('age',25); - $this->assertEquals(count($people), 4); - } - - function testMultipleConditionsOnStrings() { - $people = Make::a('Person')->like('firstName','%K%')->like('firstName','%s%'); - $this->assertEquals(count($people), 1); - } - - function testHasManyRelationship() { - $person = new Person(); - $person->id = 4; - $books = $person->books()->orderBy('title'); - $this->assertEquals(count($books), 3); - } - - function testHasManyRelationshipCriteria() { - $person = new Person(); - $person->firstName = 'Barack'; - $person->lastName = 'Obama'; - $books = $person->books()->like('title','%Dream%')->orderBy('title ASC'); - $this->assertEquals(count($books), 2); - $this->assertEquals($books[0]->title, 'Dreams from My Father: A Story of Race and Inheritance'); - } - - function testAliasHasManyRelationship() { - $person = new Person(); - $person->id = 4; - $novels = $person->novels(); - $this->assertEquals(count($novels),3); - } - - function testChainMultipleRelationships() { - $person = new Person(); - $person->id = 3; - $generas = $person->books()->generas(); - $this->assertEquals(count($generas), 3); - - $person = new Person(); - $generas = $person->equal('age',22)->books()->generas(); - $this->assertEquals(count($generas), 3); - - $person = new Person(); - $generas = $person->equal('age',22)->books()->like('title','%Dream%')->generas(); - $this->assertEquals(count($generas), 1); - } - - function testMultipleJoinTables() { - $movies = Make::a('Person') - ->equal('age',22) - ->books() - ->like('title','%Dream%') - ->generas() - ->movies(); - - $this->assertEquals(count($movies),2); - } - - function testBelongsTo() { - $book = new Book(); - $book->id = 1; - $barack = $book->author(); - $this->assertEquals(get_class($barack), 'Person'); - $this->assertEquals($barack->firstName, 'Barack'); - $baracksBooks = $barack->books(); - $this->assertEquals(count($baracksBooks), 3); - } - - function testHasAndBelongsToMany() { - $book = new Book(); - $book->id = 2; - $generas = $book->generas(); - $this->assertEquals(count($generas), 2); - - $books = $generas[0]->books(); - $this->assertEquals(count($books),4); - } - - function testHasAndBelongsToManyWithCriteria() { - $book = new Book(); - $generas = - $book - ->generas() - ->like('books.title', '%Dream%'); - $this->assertEquals(count($generas),2); - } - - function testUpdate() { - $people = Make::a('Person')->all(); - $person = $people[0]; - $name = $person->firstName; - - $person->firstName = 'UPDATE!'; - $person->update(); - - $people = Make::a('Person')->all(); - - $this->assertEquals($person->firstName, $people[0]->firstName); - } - - function testUpdateOnCollection() { - $person = new Person(); - $person->age = 23; - $person->lessThan('age',23)->update(); - - $people = $person->find(); - $this->assertEquals(count($people), 4); - - $person = new Person(); - $person->age = 100; - $person->all()->update(); - - $people = $person->find(); - $this->assertEquals(count($people),6); - } - - function testInsert() { - $people = Make::a('Person')->all(); - $people_count = count($people); - - $person = new Person(); - $person->firstName = 'Joe'; - $person->lastName = 'Biden'; - $person->age = 65; - $person->insert(); - - $people = Make::a('Person')->all(); - $this->assertEquals($people_count + 1, count($people)); - } - - function testDelete() { - $people = Make::a('Person')->all(); - $people_count = count($people); - $this->assertTrue($people[0]->delete()); - - $people = Make::a('Person')->all(); - $this->assertEquals($people_count - 1, count($people)); - } - - function testDeleteOnCollection() { - Make::a('Person')->greaterThanOrEqualTo('age',23)->delete(); - $people = Make::a('Person')->all(); - $this->assertEquals(count($people), 2); - - Make::a('Person')->all()->delete(); - $people = Make::a('Person')->all(); - $this->assertEquals(count($people), 0); - } - - function testAddToHasManyRelationship() { - $person = new Person; - $person->firstName = 'John'; - $person->age = 22; - - $book = new Book(); - $book->title = 'Obama Wins!'; - - $person->addToBooks($book); - - $this->assertEquals($book->author()->id, $person->id); - } - - function testRemoveFromHasManyRelationship() { - $barack = Make::a('Person')->equal('firstName', 'Barack')->first(); - - $barackBooksCount = count($barack->books()); - - $book = Make::a('Book')->like('title', '%Audacity%')->first(); - $barack->removeFromBooks($book); - - $this->assertEquals(count($barack->books()), $barackBooksCount - 1); - } - - function testSetOnBelongsToRelationship() { - $person = new Person; - $person->firstName = 'John'; - $person->age = 22; - - $book = new Book(); - $book->title = 'Obama Wins!'; - $book->setAuthor($person); - - $this->assertEquals($book->author()->id, $person->id); - } - - function testUnsetOnBelongsToRelationship() { - $barack = Make::a('Person')->equal('firstName', 'Barack')->first(); - - $barackBooksCount = count($barack->books()); - - $book = Make::a('Book')->like('title', '%Audacity%')->first(); - $book->unsetAuthor(); - - $this->assertEquals(count($barack->books()), $barackBooksCount - 1); - } - - function testNonDefaultPrimaryKey() { - $person = new Person; - $person->firstName = 'Katie'; - $person->age = 22; - - $car = new Car(); - $car->make = 'Honda'; - $car->insert(); - - $person->addToCars($car); - - $this->assertEquals($car->pk, 3); - $this->assertEquals($car->owner()->id, $person->id); - } - - function testAddToHasAndBelongsToManyRelationship() { - $book = new Book(); - $book->title = 'Hello world.'; - - $genera = new Genera(); - $genera->title = 'Scary'; - - $book->addToGeneras($genera); - - $resultBook = $genera->books()->first(); - - $this->assertEquals($book->id, $resultBook->id); - } - - function testRemoveFromHasAndBelongsToManyRelationship() { - $book = Make::a('Book')->like('title','%Sketch%')->first(); - $generas = $book->generas(); - $generasCount = count($generas); - - $genera = $generas->first(); - $book->removeFromGeneras($genera); - - $this->assertEquals($generasCount - 1, count($book->generas())); - } - - function testHasManyThrough() { - $kris = Make::a('Person')->equal('firstName','Kris')->first(); - $groups = $kris->groups(); - $this->assertEquals(count($groups), 2); - } - - function testHasManyOnDeleteDelete() { - $audacity = Make::a('Book')->like('title', '%Audacity%')->first(); - - $allChaptersCount = count(Make::a('Chapter')->all()); - $audacityChaptersCount = count($audacity->chapters()); + function getDataSet() { + Databases::setDefaultSource($this->source); + Object::clearDescriptors(); + return $this->createXMLDataSet('recess/database/orm/sample-data.xml'); + } + + function tearDown() { + unset($this->source); + } - $audacity->delete(); - - $this->assertEquals(count(Make::a('Chapter')->all()), $allChaptersCount - $audacityChaptersCount); - } - - function testHasManyOnDeleteCascade() { - $barack = Make::a('Person')->like('firstName','%Barack%')->first(); - $barack->delete(); - $this->assertEquals(count(Make::a('Chapter')->all()), 0); - } - - function testHasManyOnDeleteNullify() { - $kris = Make::a('Person')->equal('firstName', 'Kris')->first(); - - $car = Make::a('Car'); - - $all = $car->all(); - - $allCarsCount = count($all); - - $kris->delete(); - - $this->assertEquals($allCarsCount, count(Make::a('Car')->all())); - } - - function testBelongsToDeleteNullify() { - $audacity = Make::a('Book')->like('title','%Audacity%')->first(); - $audacity->chapters()->first()->delete(); - - $audacity = Make::a('Book')->like('title','%Audacity%'); - $this->assertEquals(1, count($audacity)); - } - - function testBelongsToDeleteDelete() { - $audacity = Make::a('Book')->like('title','%Audacity%')->first(); - $audacity->delete(); - $barack = Make::a('Person')->equal('firstName','Barack'); - $this->assertEquals(0, count($barack)); - } - - function testBelongsToDeleteCascade() { - $car = Make::a('Car')->equal('make','VW')->first(); - $car->delete(); - - $book = Make::a('Book')->like('title','%Michael Scott%'); - $this->assertEquals(0, count($book)); - } - - function testHasManyThroughDeleteNullify() { - $groupshipsCount = count(Make::a('Groupship')->all()); - - $kris = Make::a('Person')->equal('firstName','Kris')->first(); - $krisGroupsCount = count($kris->groups()); - - $kris = Make::a('Person')->equal('firstName','Kris')->first(); - $kris->delete(); - - $this->assertEquals($groupshipsCount - $krisGroupsCount, count(Make::a('Groupship')->all())); - } - - function testHasManyThroughDeleteDelete() { - $groupshipsCount = count(Make::a('Groupship')->all()); - - $members = Make::a('Group')->equal('name','NRA')->persons(); - $nraPeopleCount = count($members); - - $nra = Make::a('Group')->equal('name','NRA')->first(); - $nra->delete(); - - $this->assertEquals($groupshipsCount - $nraPeopleCount, count(Make::a('Groupship')->all())); - } - - function testHasManyThroughOnFind() { - $nra = Make::a('Group')->equal('name','NRA'); - $nraMembers = $nra->persons(); - $nraMembers2 = $nra->first()->persons(); - $this->assertEquals(count($nraMembers), count($nraMembers2)); - } - - function testHasManyThroughDeleteCascade() { - $booksCount = count(Make::a('Book')->all()); - $sports = Make::a('Genera')->like('title','%Sports%'); - $sportsCount = count($sports->books()); - $sports->delete(); - $this->assertEquals($booksCount - $sportsCount, count(Make::a('Book')->all())); - } - - function testNoLongerByReference() { - $people = Make::a('Person')->all(); - $kris = $people->equal('firstName','Kris'); - $this->assertNotEquals(count($people),count($kris)); - } - - function testTicket68FirstAfterEqual() { - $person = Make::a('Person')->equal('phone', '321-456-7890')->first(); - $this->assertEquals($person->id, 1); - $this->assertEquals($person->phone, '321-456-7890'); - } + function testHierarchy() { + $page = new Page(); + $page = $page->equal('id',1)->first(); + $this->assertEquals($page->children()->count(), 1); + $this->assertEquals($page->parent(), false); + $this->assertEquals($page->children()->equal('title','Child 1')->count(), 1); + } + + function testAll() { + $person = new Person(); + $people = $person->all(); + $this->assertEquals(count($people),6); + + $genera = Make::a('Genera')->equal('title','Social Healing')->first(); + $this->assertEquals($genera->title, 'Social Healing'); + $this->assertEquals($genera->id, 3); + } + + function testFindCriteria() { + $person = new Person(); + $person->age = 23; + $people = $person->find()->orderBy('lastName DESC'); + $this->assertEquals(count($people),2); + $this->assertEquals($people[0]->lastName, 'Sutherland'); + $this->assertEquals(get_class($people[0]), 'Person'); + } + + function testBooleanType() { + $car = new Car(); + $car->isDriveable = true; + $car->insert(); + $carId = $car->pk; + + $car = new Car(); + $car->pk = $carId; + $driveable = $car->find()->first()->isDriveable; + + $this->assertEquals($driveable === true, true); + } + + function testFindTailCriteria() { + $person = new Person(); + $people = $person->find()->greaterThan('age',22); + $this->assertEquals(count($people),4); + } + + function testBetween() { + $people = Make::a('Person')->all()->between('age',21,25); + $this->assertEquals(count($people), 4); + } + + function testMultipleConditionsOnSingleProperty() { + $people = Make::a('Person')->greaterThan('age',21)->lessThan('age',25); + $this->assertEquals(count($people), 4); + } + + function testMultipleConditionsOnStrings() { + $people = Make::a('Person')->like('firstName','%K%')->like('firstName','%s%'); + $this->assertEquals(count($people), 1); + } + + function testHasManyRelationship() { + $person = new Person(); + $person->id = 4; + $books = $person->books()->orderBy('title'); + $this->assertEquals(count($books), 3); + } + + function testHasManyRelationshipCriteria() { + $person = new Person(); + $person->firstName = 'Barack'; + $person->lastName = 'Obama'; + $books = $person->books()->like('title','%Dream%')->orderBy('title ASC'); + $this->assertEquals(count($books), 2); + $this->assertEquals($books[0]->title, 'Dreams from My Father: A Story of Race and Inheritance'); + } + + function testAliasHasManyRelationship() { + $person = new Person(); + $person->id = 4; + $novels = $person->novels(); + $this->assertEquals(count($novels),3); + } + + function testChainMultipleRelationships() { + $person = new Person(); + $person->id = 3; + $generas = $person->books()->generas(); + $this->assertEquals(count($generas), 3); + + $person = new Person(); + $generas = $person->equal('age',22)->books()->generas(); + $this->assertEquals(count($generas), 3); + + $person = new Person(); + $generas = $person->equal('age',22)->books()->like('title','%Dream%')->generas(); + $this->assertEquals(count($generas), 1); + } + + function testMultipleJoinTables() { + $movies = Make::a('Person') + ->equal('age',22) + ->books() + ->like('title','%Dream%') + ->generas() + ->movies(); + + $this->assertEquals(count($movies),2); + } + + function testBelongsTo() { + $book = new Book(); + $book->id = 1; + $barack = $book->author(); + $this->assertEquals(get_class($barack), 'Person'); + $this->assertEquals($barack->firstName, 'Barack'); + $baracksBooks = $barack->books(); + $this->assertEquals(count($baracksBooks), 3); + } + + function testHasAndBelongsToMany() { + $book = new Book(); + $book->id = 2; + $generas = $book->generas(); + $this->assertEquals(count($generas), 2); + + $books = $generas[0]->books(); + $this->assertEquals(count($books),4); + } + + function testHasAndBelongsToManyWithCriteria() { + $book = new Book(); + $generas = + $book + ->generas() + ->like('books.title', '%Dream%'); + $this->assertEquals(count($generas),2); + } + + function testUpdate() { + $people = Make::a('Person')->all(); + $person = $people[0]; + $name = $person->firstName; + + $person->firstName = 'UPDATE!'; + $person->update(); + + $people = Make::a('Person')->all(); + + $this->assertEquals($person->firstName, $people[0]->firstName); + } + + function testUpdateOnCollection() { + $person = new Person(); + $person->age = 23; + $person->lessThan('age',23)->update(); + + $people = $person->find(); + $this->assertEquals(count($people), 4); + + $person = new Person(); + $person->age = 100; + $person->all()->update(); + + $people = $person->find(); + $this->assertEquals(count($people),6); + } + + function testInsert() { + $people = Make::a('Person')->all(); + $people_count = count($people); + + $person = new Person(); + $person->firstName = 'Joe'; + $person->lastName = 'Biden'; + $person->age = 65; + $person->insert(); + + $people = Make::a('Person')->all(); + $this->assertEquals($people_count + 1, count($people)); + } + + function testDelete() { + $people = Make::a('Person')->all(); + $people_count = count($people); + $this->assertTrue($people[0]->delete()); + + $people = Make::a('Person')->all(); + $this->assertEquals($people_count - 1, count($people)); + } + + function testDeleteOnCollection() { + Make::a('Person')->greaterThanOrEqualTo('age',23)->delete(); + $people = Make::a('Person')->all(); + $this->assertEquals(count($people), 2); + + Make::a('Person')->all()->delete(); + $people = Make::a('Person')->all(); + $this->assertEquals(count($people), 0); + } + + function testAddToHasManyRelationship() { + $person = new Person; + $person->firstName = 'John'; + $person->age = 22; + + $book = new Book(); + $book->title = 'Obama Wins!'; + + $person->addToBooks($book); + + $this->assertEquals($book->author()->id, $person->id); + } + + function testRemoveFromHasManyRelationship() { + $barack = Make::a('Person')->equal('firstName', 'Barack')->first(); + + $barackBooksCount = count($barack->books()); + + $book = Make::a('Book')->like('title', '%Audacity%')->first(); + $barack->removeFromBooks($book); + + $this->assertEquals(count($barack->books()), $barackBooksCount - 1); + } + + function testSetOnBelongsToRelationship() { + $person = new Person; + $person->firstName = 'John'; + $person->age = 22; + + $book = new Book(); + $book->title = 'Obama Wins!'; + $book->setAuthor($person); + + $this->assertEquals($book->author()->id, $person->id); + } + + function testUnsetOnBelongsToRelationship() { + $barack = Make::a('Person')->equal('firstName', 'Barack')->first(); + + $barackBooksCount = count($barack->books()); + + $book = Make::a('Book')->like('title', '%Audacity%')->first(); + $book->unsetAuthor(); + + $this->assertEquals(count($barack->books()), $barackBooksCount - 1); + } + + function testNonDefaultPrimaryKey() { + $person = new Person; + $person->firstName = 'Katie'; + $person->age = 22; + + $car = new Car(); + $car->make = 'Honda'; + $car->insert(); + + $person->addToCars($car); + + $this->assertEquals($car->pk, 3); + $this->assertEquals($car->owner()->id, $person->id); + } + + function testAddToHasAndBelongsToManyRelationship() { + $book = new Book(); + $book->title = 'Hello world.'; + + $genera = new Genera(); + $genera->title = 'Scary'; + + $book->addToGeneras($genera); + + $resultBook = $genera->books()->first(); + + $this->assertEquals($book->id, $resultBook->id); + } + + function testRemoveFromHasAndBelongsToManyRelationship() { + $book = Make::a('Book')->like('title','%Sketch%')->first(); + $generas = $book->generas(); + $generasCount = count($generas); + + $genera = $generas->first(); + $book->removeFromGeneras($genera); + + $this->assertEquals($generasCount - 1, count($book->generas())); + } + + function testHasManyThrough() { + $kris = Make::a('Person')->equal('firstName','Kris')->first(); + $groups = $kris->groups(); + $this->assertEquals(count($groups), 2); + } + + function testHasManyOnDeleteDelete() { + $audacity = Make::a('Book')->like('title', '%Audacity%')->first(); + + $allChaptersCount = count(Make::a('Chapter')->all()); + $audacityChaptersCount = count($audacity->chapters()); + + $audacity->delete(); + + $this->assertEquals(count(Make::a('Chapter')->all()), $allChaptersCount - $audacityChaptersCount); + } + + function testHasManyOnDeleteCascade() { + $barack = Make::a('Person')->like('firstName','%Barack%')->first(); + $barack->delete(); + $this->assertEquals(count(Make::a('Chapter')->all()), 0); + } + + function testHasManyOnDeleteNullify() { + $kris = Make::a('Person')->equal('firstName', 'Kris')->first(); + + $car = Make::a('Car'); + + $all = $car->all(); + + $allCarsCount = count($all); + + $kris->delete(); + + $this->assertEquals($allCarsCount, count(Make::a('Car')->all())); + } + + function testBelongsToDeleteNullify() { + $audacity = Make::a('Book')->like('title','%Audacity%')->first(); + $audacity->chapters()->first()->delete(); + + $audacity = Make::a('Book')->like('title','%Audacity%'); + $this->assertEquals(1, count($audacity)); + } + + function testBelongsToDeleteDelete() { + $audacity = Make::a('Book')->like('title','%Audacity%')->first(); + $audacity->delete(); + $barack = Make::a('Person')->equal('firstName','Barack'); + $this->assertEquals(0, count($barack)); + } + + function testBelongsToDeleteCascade() { + $car = Make::a('Car')->equal('make','VW')->first(); + $car->delete(); + + $book = Make::a('Book')->like('title','%Michael Scott%'); + $this->assertEquals(0, count($book)); + } + + function testHasManyThroughDeleteNullify() { + $groupshipsCount = count(Make::a('Groupship')->all()); + + $kris = Make::a('Person')->equal('firstName','Kris')->first(); + $krisGroupsCount = count($kris->groups()); + + $kris = Make::a('Person')->equal('firstName','Kris')->first(); + $kris->delete(); + + $this->assertEquals($groupshipsCount - $krisGroupsCount, count(Make::a('Groupship')->all())); + } + + function testHasManyThroughDeleteDelete() { + $groupshipsCount = count(Make::a('Groupship')->all()); + + $members = Make::a('Group')->equal('name','NRA')->persons(); + $nraPeopleCount = count($members); + + $nra = Make::a('Group')->equal('name','NRA')->first(); + $nra->delete(); + + $this->assertEquals($groupshipsCount - $nraPeopleCount, count(Make::a('Groupship')->all())); + } + + function testHasManyThroughOnFind() { + $nra = Make::a('Group')->equal('name','NRA'); + $nraMembers = $nra->persons(); + $nraMembers2 = $nra->first()->persons(); + $this->assertEquals(count($nraMembers), count($nraMembers2)); + } + + function testHasManyThroughDeleteCascade() { + $booksCount = count(Make::a('Book')->all()); + $sports = Make::a('Genera')->like('title','%Sports%'); + $sportsCount = count($sports->books()); + $sports->delete(); + $this->assertEquals($booksCount - $sportsCount, count(Make::a('Book')->all())); + } + + function testNoLongerByReference() { + $people = Make::a('Person')->all(); + $kris = $people->equal('firstName','Kris'); + $this->assertNotEquals(count($people),count($kris)); + } + + function testTicket68FirstAfterEqual() { + $person = Make::a('Person')->equal('phone', '321-456-7890')->first(); + $this->assertEquals($person->id, 1); + $this->assertEquals($person->phone, '321-456-7890'); + } + + function test() { + $people = Make::a('Person')->in('id', array(1,2,3)); + $this->assertEquals($people[0]->id, 1); + $this->assertEquals($people[0]->phone, '321-456-7890'); + } } diff --git a/recess/test/recess/database/pdo/PdoDataSetTest.php b/recess/test/recess/database/pdo/PdoDataSetTest.php index 0d533df..97a203e 100644 --- a/recess/test/recess/database/pdo/PdoDataSetTest.php +++ b/recess/test/recess/database/pdo/PdoDataSetTest.php @@ -164,6 +164,13 @@ function testClone() { $results2 = $results2->equal('first_name','Barack'); $this->assertNotEquals(count($results),count($results2)); } + + + function testSelectIn() { + $results = $this->source->select()->from('people')->in('id', array(1, 2, 3, 10)); + $this->assertEquals(3, count($results)); + } + } ?> \ No newline at end of file diff --git a/recess/test/recess/database/sql/SelectSqlBuilderTest.php b/recess/test/recess/database/sql/SelectSqlBuilderTest.php index 4c23dbf..bccf5fb 100644 --- a/recess/test/recess/database/sql/SelectSqlBuilderTest.php +++ b/recess/test/recess/database/sql/SelectSqlBuilderTest.php @@ -48,6 +48,12 @@ function testMultipleWheres() { $this->assertEquals($this->builder->select(), $expected); } + function testWhereIn() { + $this->builder->from('table')->in('id', array(1, 2, 3, 4)); + $expected = 'SELECT * FROM `table` WHERE `table`.`id` IN (1, 2, 3, 4)'; + $this->assertEquals($this->builder->select(), $expected); + } + function testOrderByFail() { $this->builder->orderBy('name ASC'); try { From 7019021565e360c59ca3e76f343ccdf0677c2791 Mon Sep 17 00:00:00 2001 From: zdk Date: Sun, 30 Aug 2009 15:51:24 +0200 Subject: [PATCH 4/5] Added missing constant phpunit Criterion::IN --- recess/recess/database/sql/ISqlConditions.class.php | 2 +- recess/recess/database/sql/SqlBuilder.class.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/recess/recess/database/sql/ISqlConditions.class.php b/recess/recess/database/sql/ISqlConditions.class.php index 007b780..ca50fa0 100644 --- a/recess/recess/database/sql/ISqlConditions.class.php +++ b/recess/recess/database/sql/ISqlConditions.class.php @@ -19,7 +19,7 @@ function lessThan($column, $value); function lessThanOrEqualTo($column, $value); function like($column, $value); function notLike($column, $value); - function in($column, $value); + function in($column, $value); } ?> \ No newline at end of file diff --git a/recess/recess/database/sql/SqlBuilder.class.php b/recess/recess/database/sql/SqlBuilder.class.php index cbf13c4..a21b39f 100644 --- a/recess/recess/database/sql/SqlBuilder.class.php +++ b/recess/recess/database/sql/SqlBuilder.class.php @@ -697,6 +697,8 @@ class Criterion { const UNDERSCORE = '_'; + const IN = ' IN '; + public function __construct($column, $value, $operator, $pdoLabel = null){ $this->column = $column; $this->value = $value; From 7ebeeb2fbc588ed88649615f78836cea31cc4f2d Mon Sep 17 00:00:00 2001 From: zdk Date: Sun, 30 Aug 2009 16:00:10 +0200 Subject: [PATCH 5/5] Remove spaces at IN statement in testWhereIn() --- recess/test/recess/database/sql/SelectSqlBuilderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recess/test/recess/database/sql/SelectSqlBuilderTest.php b/recess/test/recess/database/sql/SelectSqlBuilderTest.php index bccf5fb..7fa95cd 100644 --- a/recess/test/recess/database/sql/SelectSqlBuilderTest.php +++ b/recess/test/recess/database/sql/SelectSqlBuilderTest.php @@ -50,7 +50,7 @@ function testMultipleWheres() { function testWhereIn() { $this->builder->from('table')->in('id', array(1, 2, 3, 4)); - $expected = 'SELECT * FROM `table` WHERE `table`.`id` IN (1, 2, 3, 4)'; + $expected = 'SELECT * FROM `table` WHERE `table`.`id` IN (1,2,3,4)'; $this->assertEquals($this->builder->select(), $expected); }