Ejemplo n.º 1
0
 public function testOneToMany()
 {
     SolutionSchema::registerSchema("MySchema", "Rhubarb\\Stem\\Tests\\Fixtures\\UnitTestingSolutionSchema");
     $company = new Company();
     $company->getRepository()->clearObjectCache();
     $company->CompanyName = "Test Company";
     $company->save();
     $user = new User();
     $user->getRepository()->clearObjectCache();
     $user->Username = "******";
     $user->Password = "******";
     $user->Active = 1;
     $user->CompanyID = $company->CompanyID;
     $user->save();
     $user = new User();
     $user->Username = "******";
     $user->Password = "";
     $user->Active = 1;
     $user->CompanyID = $company->CompanyID;
     $user->save();
     $user = new User();
     $user->Username = "******";
     $user->Password = "";
     $user->Active = 0;
     $user->CompanyID = $company->CompanyID;
     $user->save();
     $oneToMany = new OneToMany("Unused", "Company", "CompanyID", "UnitTestUser");
     $list = $oneToMany->fetchFor($company);
     $this->assertCount(2, $list);
     $this->assertEquals("msmith", $list[1]->Username);
 }
 public function testSchemaCanBeCreatedFromMySqlSchema()
 {
     $user = new User();
     $schema = $user->getSchema();
     $comparisonSchema = MySqlComparisonSchema::fromMySqlSchema($schema);
     $this->assertEquals(["UserID" => "`UserID` int(11) unsigned NOT NULL AUTO_INCREMENT", "CompanyID" => "`CompanyID` int(11) unsigned NOT NULL DEFAULT '0'", "UserType" => "`UserType` enum('Staff','Administrator') NOT NULL DEFAULT 'Staff'", "Username" => "`Username` varchar(40) NOT NULL DEFAULT ''", "Forename" => "`Forename` varchar(40) NOT NULL DEFAULT ''", "Surname" => "`Surname` varchar(40) NOT NULL DEFAULT ''", "Password" => "`Password` varchar(120) NOT NULL DEFAULT ''", "Active" => "`Active` tinyint(3) NOT NULL DEFAULT '0'", "Wage" => "`Wage` decimal(8,2) NOT NULL DEFAULT '0.00'"], $comparisonSchema->columns);
     $this->assertEquals(["PRIMARY KEY (`UserID`)", "KEY `CompanyID` (`CompanyID`)"], $comparisonSchema->indexes);
 }
Ejemplo n.º 3
0
 public function testOneToOne()
 {
     SolutionSchema::registerSchema("MySchema", "Rhubarb\\Stem\\Tests\\Fixtures\\UnitTestingSolutionSchema");
     $company = new Company();
     $company->CompanyName = "Test Company";
     $company->save();
     $user = new User();
     $user->Username = "******";
     $user->Password = "******";
     $user->Active = 1;
     $user->CompanyID = $company->CompanyID;
     $user->save();
     $oneToOne = new OneToOne("Unused", "User", "CompanyID", "Company", "CompanyID");
     $result = $oneToOne->fetchFor($user);
     $this->assertEquals("Test Company", $result->CompanyName);
 }
Ejemplo n.º 4
0
 public function testAverage()
 {
     $user = new User();
     $user->Wage = 100;
     $user->Active = true;
     $user->save();
     $user = new User();
     $user->Wage = 200;
     $user->Active = true;
     $user->save();
     $user = new User();
     $user->Wage = 600;
     $user->Active = true;
     $user->save();
     $collection = User::find();
     list($average) = $collection->calculateAggregates([new Average("Wage")]);
     $this->assertEquals(300, $average);
 }
 public function testRelationships()
 {
     SolutionSchema::clearSchemas();
     SolutionSchema::registerSchema("MySchema", "Rhubarb\\Stem\\Tests\\Fixtures\\UnitTestingSolutionSchema");
     error_reporting(E_ALL);
     ini_set("display_errors", "on");
     $schema = new UnitTestingSolutionSchema();
     $schema->defineRelationships();
     $relationship = $schema->getRelationship("UnitTestUser", "Company");
     $this->assertInstanceOf("Rhubarb\\Stem\\Schema\\Relationships\\OneToOne", $relationship);
     $this->assertInstanceOf("Rhubarb\\Stem\\Schema\\Relationships\\OneToMany", $relationship->getOtherSide());
     $relationship = $schema->getRelationship("Company", "Users");
     $this->assertInstanceOf("Rhubarb\\Stem\\Schema\\Relationships\\OneToMany", $relationship);
     $relationship = $schema->getRelationship("Company", "Unknown");
     $this->assertNull($relationship);
     $relationship = $schema->getRelationship("Example", "ExampleRelationshipName");
     $this->assertInstanceOf("Rhubarb\\Stem\\Schema\\Relationships\\OneToOne", $relationship);
     $columnRelationships = SolutionSchema::getAllOneToOneRelationshipsForModelBySourceColumnName("UnitTestUser");
     $this->assertArrayHasKey("CompanyID", $columnRelationships);
     $this->assertInstanceOf("Rhubarb\\Stem\\Schema\\Relationships\\OneToOne", $columnRelationships["CompanyID"]);
     $company = new Company();
     $company->CompanyName = "GCD";
     $company->save();
     $user = new User();
     $user->getRepository()->clearObjectCache();
     $user->Forename = "a";
     $user->save();
     $company->Users->Append($user);
     $b = $user = new User();
     $user->Forename = "b";
     $user->save();
     $company->Users->Append($user);
     // Just to make sure this doesn't get in our relationship!
     $user = new User();
     $user->Forename = "c";
     $user->save();
     $company = new Company($company->CompanyID);
     $this->assertCount(2, $company->Users);
     $this->assertEquals("a", $company->Users[0]->Forename);
     $this->assertEquals("b", $company->Users[1]->Forename);
     $company = $b->Company;
     $this->assertEquals("GCD", $company->CompanyName);
 }
Ejemplo n.º 6
0
 public function testCanBeFound()
 {
     $user = new User();
     $user->Username = "******";
     $user->save();
     $user = new User();
     $user->Username = "******";
     $user->save();
     $user = new User();
     $user->Username = "******";
     $user->save();
     $user = User::FromUsername("def");
     $this->assertEquals("def", $user->Username);
     $this->setExpectedException("Rhubarb\\Stem\\Exceptions\\RecordNotFoundException");
     User::FromUsername("123");
 }
Ejemplo n.º 7
0
 public function testAutoHydration()
 {
     MySql::executeStatement("TRUNCATE TABLE tblCompany");
     $company = new Company();
     $company->CompanyName = "GCD";
     $company->save();
     $user = new User();
     $user->Forename = "Andrew";
     $user->save();
     $company->Users->Append($user);
     $company->getRepository()->clearObjectCache();
     $user->getRepository()->clearObjectCache();
     $users = new Collection("Rhubarb\\Stem\\Tests\\Fixtures\\User");
     $users->filter(new Equals("Company.CompanyName", "GCD"));
     count($users);
     $this->assertStringStartsWith("SELECT `tblUser`.*, `Company`.`CompanyID` AS `CompanyCompanyID`, `Company`.`CompanyName` AS `CompanyCompanyName`, `Company`.`Balance` AS `CompanyBalance`, `Company`.`InceptionDate` AS `CompanyInceptionDate`, `Company`.`LastUpdatedDate` AS `CompanyLastUpdatedDate`, `Company`.`KnockOffTime` AS `CompanyKnockOffTime`, `Company`.`BlueChip` AS `CompanyBlueChip`, `Company`.`ProjectCount` AS `CompanyProjectCount`, `Company`.`CompanyData` AS `CompanyCompanyData` FROM `tblUser` LEFT JOIN `tblCompany` AS `Company` ON `tblUser`.`CompanyID` = `Company`.`CompanyID` WHERE `Company`.`CompanyName` = :", MySql::getPreviousStatement());
     $company->getRepository()->clearObjectCache();
     $user->getRepository()->clearObjectCache();
     $users = new Collection("Rhubarb\\Stem\\Tests\\Fixtures\\User");
     $users->replaceSort("Company.CompanyName", true);
     count($users);
     $this->assertStringStartsWith("SELECT `tblUser`.*, `Company`.`CompanyID` AS `CompanyCompanyID`, `Company`.`CompanyName` AS `CompanyCompanyName`, `Company`.`Balance` AS `CompanyBalance`, `Company`.`InceptionDate` AS `CompanyInceptionDate`, `Company`.`LastUpdatedDate` AS `CompanyLastUpdatedDate`, `Company`.`KnockOffTime` AS `CompanyKnockOffTime`, `Company`.`BlueChip` AS `CompanyBlueChip`, `Company`.`ProjectCount` AS `CompanyProjectCount`, `Company`.`CompanyData` AS `CompanyCompanyData` FROM `tblUser` LEFT JOIN `tblCompany` AS `Company` ON `tblUser`.`CompanyID` = `Company`.`CompanyID` ORDER BY `Company`.`CompanyName` ASC", MySql::getPreviousStatement());
     $user = $users[0];
     $this->assertCount(9, $user->ExportRawData(), "The user model should only have 9 columns. More means that the joined tables aren't being removed after the join.");
     $this->assertArrayHasKey($company->CompanyID, $company->getRepository()->cachedObjectData, "After an auto hydrated fetch the auto hydrated relationship should now be cached and ready for use in the repository");
     $this->assertCount(9, $company->getRepository()->cachedObjectData[$company->CompanyID], "The company model should only have 9 columns. More means that the joined tables aren't properly being broken up into their respective models.");
 }