-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Zylius/update_tests
Type update command now understands elasticsearch date format
- Loading branch information
Showing
6 changed files
with
428 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <info@nfq.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ElasticsearchBundle\Mapping; | ||
|
||
/** | ||
* Helps to format elasticsearch time string to interval in seconds. | ||
*/ | ||
class DateHelper | ||
{ | ||
/** | ||
* Parses elasticsearch type of string into milliseconds. | ||
* | ||
* @param string $timeString | ||
* | ||
* @return int | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function parseString($timeString) | ||
{ | ||
$results = []; | ||
preg_match_all('/(\d+)([a-zA-Z]+)/', $timeString, $results); | ||
$values = $results[1]; | ||
$units = $results[2]; | ||
|
||
if (count($values) != count($units) || count($values) == 0) { | ||
throw new \InvalidArgumentException("Invalid time string '{$timeString}'."); | ||
} | ||
|
||
$result = 0; | ||
foreach ($values as $key => $value) { | ||
$result += $value * self::charToInterval($units[$key]); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Converts a string to time interval. | ||
* | ||
* @param string $value | ||
* | ||
* @return int | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
private static function charToInterval($value) | ||
{ | ||
switch($value) { | ||
case 'w': | ||
return 604800000; | ||
case 'd': | ||
return 86400000; | ||
case 'h': | ||
return 3600000; | ||
case 'm': | ||
return 60000; | ||
case 'ms': | ||
return 1; | ||
default: | ||
throw new \InvalidArgumentException("Unknown time unit '{$value}'."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <info@nfq.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ElasticsearchBundle\Tests\Functional\Command; | ||
|
||
use ONGR\ElasticsearchBundle\Command\IndexCreateCommand; | ||
use ONGR\ElasticsearchBundle\Command\TypeUpdateCommand; | ||
use ONGR\ElasticsearchBundle\ORM\Manager; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\Config\Definition\Exception\Exception; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Functional tests for type update command. | ||
*/ | ||
class TypeUpdateCommandTest extends WebTestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $documentDir; | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
private $manager; | ||
|
||
/** | ||
* @var Application | ||
*/ | ||
private $app; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $file; | ||
|
||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp() | ||
{ | ||
// Only a single instance of container should be used on all commands and throughout the test. | ||
$this->container = self::createClient()->getContainer(); | ||
$this->manager = $this->container->get('es.manager'); | ||
|
||
// Set up custom document to test mapping with. | ||
$this->documentDir = $this->container->get('kernel')->locateResource('@ONGRTestingBundle/Document/'); | ||
$this->file = $this->documentDir . 'Article.php'; | ||
|
||
// Create index for testing. | ||
$this->app = new Application(); | ||
$this->createIndexCommand(); | ||
} | ||
|
||
/** | ||
* Check if update works as expected. | ||
*/ | ||
public function testExecute() | ||
{ | ||
$this->assertMappingNotSet("Article mapping shouldn't be defined yet."); | ||
|
||
copy($this->documentDir . 'documentSample.txt', $this->file); | ||
|
||
$this->runUpdateCommand(); | ||
$this->assertMappingSet('Article mapping should be defined after update.'); | ||
} | ||
|
||
/** | ||
* Check if updating works with type selected. | ||
*/ | ||
public function testExecuteType() | ||
{ | ||
$this->assertMappingNotSet("Article mapping shouldn't be defined yet."); | ||
|
||
copy($this->documentDir . 'documentSample.txt', $this->file); | ||
|
||
$this->runUpdateCommand('product'); | ||
$this->assertMappingNotSet("Article mapping shouldn't be defined, type selected was `product`."); | ||
|
||
$this->runUpdateCommand('article'); | ||
$this->assertMappingSet('Article mapping should be defined after update, type selected was `article`.'); | ||
} | ||
|
||
/** | ||
* Check if up to date mapping check works. | ||
*/ | ||
public function testExecuteUpdated() | ||
{ | ||
$this->assertStringStartsWith('Types are already up to date.', $this->runUpdateCommand()); | ||
$this->assertMappingNotSet("Article was never added, type shouldn't be added."); | ||
} | ||
|
||
/** | ||
* Asserts mapping is set and correct. | ||
* | ||
* @param string $message | ||
*/ | ||
protected function assertMappingSet($message) | ||
{ | ||
$mapping = $this->manager->getConnection()->getMapping('article'); | ||
$this->assertNotNull($mapping, $message); | ||
$expectedMapping = [ | ||
'properties' => [ | ||
'title' => [ | ||
'type' => 'string', | ||
] | ||
] | ||
]; | ||
$this->assertEquals($expectedMapping, $mapping); | ||
} | ||
|
||
/** | ||
* Asserts mapping isn't set. | ||
* | ||
* @param string $message | ||
*/ | ||
protected function assertMappingNotSet($message) | ||
{ | ||
$this->assertNull($this->manager->getConnection()->getMapping('article'), $message); | ||
} | ||
|
||
/** | ||
* Runs update command. | ||
* | ||
* @param string $type | ||
* | ||
* @return string | ||
*/ | ||
protected function runUpdateCommand($type = '') | ||
{ | ||
$command = new TypeUpdateCommand(); | ||
$command->setContainer($this->container); | ||
|
||
$this->app->add($command); | ||
$commandToTest = $this->app->find('es:type:update'); | ||
$commandTester = new CommandTester($commandToTest); | ||
|
||
$result = $commandTester->execute( | ||
[ | ||
'command' => $commandToTest->getName(), | ||
'--force' => true, | ||
'--type' => $type, | ||
] | ||
); | ||
|
||
$this->assertEquals(0, $result, "Mapping update wasn't executed successfully."); | ||
|
||
return $commandTester->getDisplay(); | ||
} | ||
|
||
/** | ||
* Creates index for testing. | ||
* | ||
* @param string $manager | ||
*/ | ||
protected function createIndexCommand($manager = 'default') | ||
{ | ||
$command = new IndexCreateCommand(); | ||
$command->setContainer($this->container); | ||
|
||
$this->app->add($command); | ||
$command = $this->app->find('es:index:create'); | ||
$commandTester = new CommandTester($command); | ||
$commandTester->execute( | ||
[ | ||
'command' => $command->getName(), | ||
'--manager' => $manager, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function tearDown() | ||
{ | ||
try { | ||
$this->manager->getConnection()->dropIndex(); | ||
} catch (Exception $ex) { | ||
// Index wasn't actually created. | ||
} | ||
@unlink($this->file); | ||
} | ||
} |
Oops, something went wrong.