Esempio n. 1
0
 public function testCanFilterOnRelatedModelProperties()
 {
     $gcd = new Company();
     $gcd->CompanyName = "GCD";
     $gcd->save();
     $widgetCo = new Company();
     $widgetCo->CompanyName = "Widgets";
     $widgetCo->save();
     $example = new User();
     $example->Username = "******";
     $widgetCo->Users->Append($example);
     $example = new User();
     $example->Username = "******";
     $gcd->Users->Append($example);
     $example = new User();
     $example->Username = "******";
     $widgetCo->Users->Append($example);
     $example = new User();
     $example->Username = "******";
     $gcd->Users->Append($example);
     $list = new Collection("Rhubarb\\Stem\\Tests\\Fixtures\\User");
     $list->filter(new Equals("Company.CompanyName", "GCD"));
     $this->assertCount(2, $list);
     $this->assertEquals("b", $list[0]->Username);
     $this->assertEquals("d", $list[1]->Username);
 }
Esempio n. 2
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 testColumnFormatter()
 {
     Company::clearObjectCache();
     $company = new Company();
     $company->CompanyName = "abc";
     $company->save();
     $decorator = DataDecorator::getDecoratorForModel($company);
     $this->assertSame("00001", $decorator->CompanyID);
 }
 public function testSchemaDetectsWhenItCanUpdate()
 {
     $comparisonSchema = MySqlComparisonSchema::fromTable("tblCompany");
     $example = new Company();
     $schema = $example->getSchema();
     $compareTo = MySqlComparisonSchema::fromMySqlSchema($schema);
     $this->assertFalse($compareTo->createAlterTableStatementFor($comparisonSchema));
     $schema->addColumn(new String("Town", 60, null));
     $compareTo = MySqlComparisonSchema::fromMySqlSchema($schema);
     $this->assertContains("ADD COLUMN `Town` varchar(60) DEFAULT NULL", $compareTo->createAlterTableStatementFor($comparisonSchema));
 }
Esempio n. 5
0
 public function testRepositoryGetsDateFormat()
 {
     $company = new Company();
     $company->CompanyName = "GCD";
     $company->LastUpdatedDate = "2012-01-01 10:01:02";
     $company->save();
     $params = MySql::getPreviousParameters();
     $this->assertContains("2012-01-01 10:01:02", $params["LastUpdatedDate"]);
     $company->reload();
     $this->assertEquals("2012-01-01 10:01:02", $company->LastUpdatedDate->format("Y-m-d H:i:s"));
 }
Esempio n. 6
0
 public function testRepositoryGetsTimeFormat()
 {
     $company = new Company();
     $company->CompanyName = "GCD";
     $company->KnockOffTime = "17:01:02";
     $company->save();
     $params = MySql::getPreviousParameters();
     $this->assertContains("17:01:02", $params["KnockOffTime"]);
     $company->reload();
     $this->assertEquals("2000-01-01 17:01:02", $company->KnockOffTime->format("Y-m-d H:i:s"));
 }
Esempio n. 7
0
 public function testValidJsonTransform()
 {
     $company = new Company();
     $company->CompanyName = "Gcd Technologies";
     $company->CompanyData = $this->data;
     $company->save();
     $params = MySql::getPreviousParameters();
     $this->assertEquals($this->constraint, $params["CompanyData"]);
     Model::clearAllRepositories();
     $company = new Company($company->UniqueIdentifier);
     $this->assertEquals($this->data, $company->CompanyData);
 }
 public function testAppendingCreatesRowInModel()
 {
     $companyCategory = new CompanyCategory();
     $companyCategory->getRepository()->clearObjectCache();
     $company = new Company();
     $company->CompanyName = "GCD";
     $company->save();
     $category = new Category();
     $category->CategoryName = "AppendTest";
     $category->save();
     $company->Categories->Append($category);
     $this->assertCount(1, $company->Categories);
     $this->assertEquals($company->CompanyID, $companyCategory->getRepository()->cachedObjectData[1]["CompanyID"]);
     $this->assertEquals($category->CategoryID, $companyCategory->getRepository()->cachedObjectData[1]["CategoryID"]);
 }
Esempio n. 9
0
 public function testRepositoryGetsDateFormat()
 {
     $company = new Company();
     $company->CompanyName = "GCD";
     $company->InceptionDate = "2012-01-01";
     $company->save();
     $params = MySql::getPreviousParameters();
     $this->assertContains("2012-01-01", $params["InceptionDate"]);
     $company->reload();
     $this->assertEquals("2012-01-01", $company->InceptionDate->format("Y-m-d"));
     $company->InceptionDate = "2011-01-01";
     $company->save();
     $company->reload();
     $this->assertEquals("2011-01-01", $company->InceptionDate->format("Y-m-d"));
 }
Esempio n. 10
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);
 }
Esempio n. 11
0
 public function testManyToMany()
 {
     // UnitTestingSolutionSchema sets up a many to many relationship between company and category
     $company1 = new Company();
     $company2 = new Company();
     $company3 = new Company();
     $company1->getRepository()->clearObjectCache();
     $companyCategory = new CompanyCategory();
     $companyCategory->getRepository()->clearObjectCache();
     $category1 = new Category();
     $category2 = new Category();
     $category1->getRepository()->clearObjectCache();
     $company1->CompanyName = "GCD";
     $company1->save();
     $company2->CompanyName = "UTV";
     $company2->save();
     $company3->CompanyName = 'Inactive';
     $company3->Active = 0;
     $company3->save();
     $category1->CategoryName = "Fruit";
     $category1->save();
     $category2->CategoreName = "Apples";
     $category2->save();
     $companyCategory->CategoryID = $category1->CategoryID;
     $companyCategory->CompanyID = $company1->CompanyID;
     $companyCategory->save();
     $companyCategory = new CompanyCategory();
     $companyCategory->CategoryID = $category1->CategoryID;
     $companyCategory->CompanyID = $company2->CompanyID;
     $companyCategory->save();
     $companyCategory = new CompanyCategory();
     $companyCategory->CategoryID = $category2->CategoryID;
     $companyCategory->CompanyID = $company2->CompanyID;
     $companyCategory->save();
     $companyCategory = new CompanyCategory();
     $companyCategory->CategoryID = $category1->CategoryID;
     $companyCategory->CompanyID = $company3->CompanyID;
     $companyCategory->save();
     // At this point GCD is in Fruit, while UTV is in Fruit and Apples.
     $company1 = new Company($company1->CompanyID);
     $this->assertCount(1, $company1->Categories);
     $this->assertCount(2, $company2->Categories);
     $this->assertCount(2, $category1->Companies);
     $this->assertCount(1, $category2->Companies);
     $this->assertEquals("UTV", $category2->Companies[0]->CompanyName);
 }
 public function testCount()
 {
     $company = new Company();
     $company->CompanyName = "a";
     $company->save();
     $company = new Company();
     $company->CompanyName = "b";
     $company->save();
     $company = new Company();
     $company->CompanyName = "b";
     $company->save();
     $company = new Company();
     $company->CompanyName = "a";
     $company->save();
     $collection = Company::find();
     list($companies) = $collection->calculateAggregates(new CountDistinct("CompanyName"));
     $this->assertEquals(2, $companies);
 }
 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);
 }
Esempio n. 14
0
 protected function setUp()
 {
     parent::setUp();
     MySql::executeStatement("TRUNCATE TABLE tblCompany");
     $example = new Company();
     $example->getRepository()->clearObjectCache();
     $example = new Company();
     $example->CompanyName = "a";
     $example->Balance = 1;
     $example->save();
     $example = new Company();
     $example->CompanyName = "b";
     $example->Balance = 2;
     $example->save();
     $example = new Company();
     $example->CompanyName = "c";
     $example->Balance = 3;
     $example->save();
 }
Esempio n. 15
0
 public function testModelReturnsConsistencyErrors()
 {
     $company = new Company();
     $errors = [];
     try {
         $company->isConsistent();
     } catch (ModelConsistencyValidationException $er) {
         $errors = $er->GetErrors();
     }
     $this->assertCount(1, $errors);
     $this->assertEquals("CompanyName", key($errors));
 }
 public function testLimits()
 {
     MySql::executeStatement("TRUNCATE TABLE tblCompany");
     $company = new Company();
     $repos = $company->getRepository();
     $repos->clearObjectCache();
     $company = new Company();
     $company->CompanyName = "A";
     $company->save();
     $company = new Company();
     $company->CompanyName = "B";
     $company->save();
     $company = new Company();
     $company->CompanyName = "B";
     $company->save();
     $company = new Company();
     $company->CompanyName = "B";
     $company->save();
     $company = new Company();
     $company->CompanyName = "C";
     $company->save();
     $company = new Company();
     $company->CompanyName = "D";
     $company->save();
     $list = new Collection("\\Rhubarb\\Stem\\Tests\\Fixtures\\Company");
     $list->setRange(2, 6);
     $this->assertCount(6, $list);
     $this->assertEquals("C", $list[2]->CompanyName);
     $sql = MySql::getPreviousStatement(true);
     $this->assertContains("LIMIT 2, 6", $sql);
     // Sorting by a computed column should mean that limits are no longer used.
     $list->addSort("CompanyIDSquared", true);
     $this->assertCount(6, $list);
     $this->assertEquals("C", $list[2]->CompanyName);
     $sql = MySql::getPreviousStatement();
     $this->assertNotContains("LIMIT 2, 6", $sql);
     $sql = MySql::getPreviousStatement(true);
     $this->assertNotContains("LIMIT 2, 6", $sql);
 }
Esempio n. 17
0
 public function testIsNullFilter()
 {
     MySql::executeStatement("TRUNCATE TABLE tblCompany");
     $company = new \Rhubarb\Stem\Tests\Fixtures\Company();
     $company->CompanyName = "GCD";
     $company->save();
     $companies = new Collection('Rhubarb\\Stem\\Tests\\Fixtures\\Company');
     $companies->filter(new Equals("CompanyName", null));
     $this->assertEquals(0, $companies->count());
     $companies = new Collection('Rhubarb\\Stem\\Tests\\Fixtures\\Company');
     $companies->filter(new Equals("ProjectCount", null));
     $this->assertEquals(1, $companies->count());
 }
Esempio n. 18
0
 public function testMySqlAggregateSupport()
 {
     MySql::executeStatement("TRUNCATE TABLE tblCompany");
     MySql::executeStatement("TRUNCATE TABLE tblUser");
     $company1 = new Company();
     $company1->getRepository()->clearObjectCache();
     $company1->CompanyName = "1";
     $company1->save();
     $company2 = new Company();
     $company2->CompanyName = "2";
     $company2->save();
     $user1 = new User();
     $user1->Wage = 100;
     $company1->Users->Append($user1);
     $user2 = new User();
     $user2->Wage = 200;
     $company1->Users->Append($user2);
     $user3 = new User();
     $user3->Wage = 300;
     $company2->Users->Append($user3);
     $user4 = new User();
     $user4->Wage = 400;
     $company2->Users->Append($user4);
     $companies = new Collection("Company");
     $companies->addAggregateColumn(new Sum("Users.Wage"));
     $results = [];
     foreach ($companies as $company) {
         $results[] = $company->SumOfUsersWage;
     }
     $this->assertEquals([300, 700], $results);
     $sql = MySql::getPreviousStatement();
     $this->assertEquals("SELECT `tblCompany`.*, SUM( `Users`.`Wage` ) AS `SumOfUsersWage` FROM `tblCompany` LEFT JOIN `tblUser` AS `Users` ON `tblCompany`.`CompanyID` = `Users`.`CompanyID` GROUP BY `tblCompany`.`CompanyID`", $sql);
     $companies = new Collection("Company");
     $companies->addAggregateColumn(new Sum("Users.BigWage"));
     $results = [];
     foreach ($companies as $company) {
         $results[] = $company->SumOfUsersBigWage;
     }
     $this->assertEquals([3000, 7000], $results);
 }