/** * @test */ public function removeFromDatabaseTest() { $databaseIdentifier = 'contacts'; $testEmail = '*****@*****.**'; /** @var Database $database */ $database = $this->fixture->getDatabase($databaseIdentifier); $dataInstance = new Document(); $dataInstance->setData(array('firstName' => 'Paul', 'lastName' => 'McKenzy', 'email' => $testEmail)); $database->remove($dataInstance); $this->assertEquals($this->numberOfContacts - 1, $database->count()); // A database just loaded from the filesystem should only contain the original number of entries /** @var DatabaseInterface $newlyLoadedDatabase */ $newlyLoadedDatabase = $this->databaseReader->loadDatabase($databaseIdentifier); $this->assertEquals($this->numberOfContacts, $newlyLoadedDatabase->count()); $this->assertEquals($database->count() + 1, $newlyLoadedDatabase->count()); // A database again retrieved from the coordinator should contain the added entry /** @var DatabaseInterface $databaseRetrievedFromTheCoordinator */ $databaseRetrievedFromTheCoordinator = $this->fixture->getDatabase($databaseIdentifier); $this->assertEquals($this->numberOfContacts - 1, $databaseRetrievedFromTheCoordinator->count()); $this->assertEquals($database->count(), $databaseRetrievedFromTheCoordinator->count()); $expectedPath = ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('writeDataPath') . $databaseIdentifier . '.json'; $this->fixture->commitDatabase($database); $this->assertFileExists($expectedPath); $writtenData = json_decode(file_get_contents($expectedPath), TRUE); $this->assertTrue($writtenData !== FALSE); foreach ($writtenData as $data) { $this->assertNotEquals($testEmail, isset($data['email']) ? $data['email'] : 'no-email-at-all'); } // $this->assertEquals($database->count(), count($writtenData)); // $this->assertEquals($this->numberOfContacts - 1, count($writtenData)); unlink($expectedPath); }
/** * A test that should validate the behavior of data object references in a database * * @test */ public function objectLiveCycleTest() { $database2 = $this->coordinator->getDatabase('people'); /** @var DocumentInterface $personFromDatabase2 */ $personFromDatabase2 = $database2->current(); /** @var DocumentInterface $personFromFixture */ $personFromFixture = $this->fixture->current(); $this->assertEquals($personFromDatabase2, $personFromFixture); $movie = 'Star Wars'; $key = 'favorite_movie'; $personFromDatabase2->setValueForKey($movie, $key); $this->assertEquals($personFromDatabase2, $personFromFixture); $this->assertSame($personFromDatabase2, $personFromFixture); $this->assertEquals($movie, $personFromFixture->valueForKey($key)); $this->assertEquals($movie, $personFromDatabase2->valueForKey($key)); }
/** * @test */ public function sortPersonsByDistanceWithCallbackTest() { $this->checkPersonFile(); /** @var Database $database */ $database = $this->coordinator->getDatabase('people'); // Sort the people database by comparing the persons distance to me (47.235934, 9.599398) // Nearer persons should appear first $myLatitude = 47.235934; $myLongitude = 9.599398000000001; $sortedDatabase = $this->fixture->sortCollectionByCallback($database, function ($itemA, $itemB) use($myLongitude, $myLatitude) { /** @var DocumentInterface $itemA */ /** @var DocumentInterface $itemB */ $distanceA = SorterTest::distance($itemA->valueForKey('latitude'), $itemA->valueForKey('longitude'), $myLatitude, $myLongitude); $distanceB = SorterTest::distance($itemB->valueForKey('latitude'), $itemB->valueForKey('longitude'), $myLatitude, $myLongitude); if ($distanceA == $distanceB) { return 0; } return $distanceA < $distanceB ? -1 : 1; }); $maxIterations = 100; // $maxIterations = $database->count(); // TODO: Make this work // $this->assertEquals($database->count(), $sortedDatabase->count()); $lastDistance = 0; for ($i = 0; $i < $maxIterations; $i++) { /** @var DocumentInterface $item */ $item = $sortedDatabase[$i]; $this->assertNotNull($item); $currentDistance = $distanceA = self::distance($item->valueForKey('latitude'), $item->valueForKey('longitude'), $myLatitude, $myLongitude); $this->assertGreaterThanOrEqual($lastDistance, $currentDistance, 'Current distance is not bigger than or equal to last for loop number ' . $i); $lastDistance = $currentDistance; } // $sortedDatabase = $this->fixture->sortCollectionByPropertyKeyPath($database, 'latitude', TRUE); // // $lastLatitude = PHP_INT_MAX; // for ($i = 0; $i < 100; $i++) { // /** @var DocumentInterface $item */ // $item = $sortedDatabase[$i++]; // $this->assertNotNull($item); // // $this->assertLessThan($lastLatitude, $item->valueForKey('latitude')); // $lastLatitude = $item->valueForKey('latitude'); // } }
/** * @test */ public function exampleTest() { $startTime = microtime(TRUE); /** @var DocumentInterface $currentObject */ // Load a database called 'people' /** @var Database $database */ $database = $this->fixture->getDatabase('people'); // Count the number of people in the database $this->assertGreaterThan(0, $database->count()); // Get the first person from the database $currentObject = $database->current(); $this->assertNotNull($currentObject); // Lets check if this is the person we look for $this->assertSame('*****@*****.**', $currentObject->valueForKey('email')); $this->assertSame('Daniel Corn', $currentObject->valueForKey('name')); $this->assertContains('developer', $currentObject->valueForKey('tags')); $this->assertContains('brown', $currentObject->valueForKey('eyeColor')); // Brown eyes are ok, but lets search for someone with blue eyes $filterResult = $database->filter(new Filter\Comparison\PropertyComparison('eyeColor', ComparisonInterface::TYPE_EQUAL_TO, 'blue')); $currentObject = $filterResult->current(); $this->assertNotNull($currentObject); $this->assertSame('*****@*****.**', $currentObject->valueForKey('email')); $this->assertSame('Robert Gonzalez', $currentObject->valueForKey('name')); $this->assertContains('blue', $currentObject->valueForKey('eyeColor')); // Ok, that's a guy... lets look for a girl $filterResult = $database->filter(new Filter\Comparison\LogicalComparison(ComparisonInterface::TYPE_AND, array(new Filter\Comparison\PropertyComparison('eyeColor', ComparisonInterface::TYPE_EQUAL_TO, 'blue'), new Filter\Comparison\PropertyComparison('gender', ComparisonInterface::TYPE_EQUAL_TO, 'female')))); $currentObject = $filterResult->current(); $this->assertNotNull($currentObject); $this->assertSame('*****@*****.**', $currentObject->valueForKey('email')); $this->assertSame('Angela Roberts', $currentObject->valueForKey('name')); $this->assertContains('blue', $currentObject->valueForKey('eyeColor')); // It's getting hotter! But is there another one? $filterResult->next(); $currentObject = $filterResult->current(); $this->assertNotNull($currentObject); $this->assertSame('*****@*****.**', $currentObject->valueForKey('email')); $this->assertSame('Frankie Horn', $currentObject->valueForKey('name')); $this->assertContains('blue', $currentObject->valueForKey('eyeColor')); $endTime = microtime(TRUE); // printf('All this took us %0.6f seconds' . PHP_EOL, $endTime - $startTime); // Let's see how many people in the database have blue eyes $filterResult = $database->filter(new Filter\Comparison\PropertyComparison('eyeColor', ComparisonInterface::TYPE_EQUAL_TO, 'blue')); $blueEyes = $filterResult->count(); $this->assertSame(1684, $blueEyes); $endTime = microtime(TRUE); // printf('All this took us %0.6f seconds' . PHP_EOL, $endTime - $startTime); // Let's see how many people in the database have brown eyes $filterResult = $database->filter(new Filter\Comparison\PropertyComparison('eyeColor', ComparisonInterface::TYPE_EQUAL_TO, 'brown')); $brownEyes = $filterResult->count(); $this->assertSame(1601, $brownEyes); $endTime = microtime(TRUE); // printf('All this took us %0.6f seconds' . PHP_EOL, $endTime - $startTime); // Let's see how many people in the database have brown or blue eyes $filterResult = $database->filter(new Filter\Comparison\PropertyComparison('eyeColor', ComparisonInterface::TYPE_IN, array('blue', 'brown'))); $blueBrownEyes = $filterResult->count(); $this->assertSame($blueEyes + $brownEyes, $blueBrownEyes); $endTime = microtime(TRUE); // printf('All this took us %0.6f seconds' . PHP_EOL, $endTime - $startTime); $filterResult = $database->filter(new Filter\Comparison\LogicalComparison(ComparisonInterface::TYPE_OR, new Filter\Comparison\LogicalComparison(ComparisonInterface::TYPE_AND, array(new Filter\Comparison\PropertyComparison('eyeColor', ComparisonInterface::TYPE_EQUAL_TO, 'brown'), new Filter\Comparison\PropertyComparison('gender', ComparisonInterface::TYPE_EQUAL_TO, 'male'))), new Filter\Comparison\LogicalComparison(ComparisonInterface::TYPE_AND, array(new Filter\Comparison\PropertyComparison('eyeColor', ComparisonInterface::TYPE_EQUAL_TO, 'blue'), new Filter\Comparison\PropertyComparison('gender', ComparisonInterface::TYPE_EQUAL_TO, 'female'))))); $currentObject = $filterResult->current(); $this->assertEquals('Daniel Corn', $currentObject->valueForKey('name')); $this->assertEquals('male', $currentObject->valueForKey('gender')); $this->assertEquals('brown', $currentObject->valueForKey('eyeColor')); $filterResult->next(); $currentObject = $filterResult->current(); $this->assertEquals('Angela Roberts', $currentObject->valueForKey('name')); $this->assertEquals('female', $currentObject->valueForKey('gender')); $this->assertEquals('blue', $currentObject->valueForKey('eyeColor')); $filterResult->next(); $currentObject = $filterResult->current(); $this->assertEquals('Frankie Horn', $currentObject->valueForKey('name')); $this->assertEquals('female', $currentObject->valueForKey('gender')); $this->assertEquals('blue', $currentObject->valueForKey('eyeColor')); $endTime = microtime(TRUE); // printf('All this took us %0.6f seconds' . PHP_EOL, $endTime - $startTime); }