Skip to content

Commit

Permalink
Changed command to work without programId option
Browse files Browse the repository at this point in the history
  • Loading branch information
donatas-jasinevicius committed Nov 7, 2016
1 parent 75e281e commit 45c97f4
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 36 deletions.
65 changes: 35 additions & 30 deletions Command/OneHydraFetchCommand.php
Expand Up @@ -48,7 +48,7 @@ protected function configure()
->addOption(
'programId',
null,
InputOption::VALUE_REQUIRED,
InputOption::VALUE_OPTIONAL,
'The programId to fetch pages for',
null
);
Expand All @@ -74,47 +74,52 @@ protected function execute(InputInterface $input, OutputInterface $output)
$api = $this->getOneHydraApi();
$pageManager = $this->getPageManager();

$programs = $container->getParameter('amara_one_hydra.programs');
$programId = $input->getOption('programId');
$availablePrograms = $container->getParameter('amara_one_hydra.programs');

$programsToFetch = $availablePrograms;
if ($input->getOption('programId') !== null) {
$programId = $input->getOption('programId');

if (!isset($availablePrograms[$programId])) {
$output->writeln('<error>There is no program with id='.$programId.'</error>');

if (!isset($programs[$programId])) {
$output->writeln('<error>There is no program with id=' . $programId . '</error>');
return 1;
}

return 1;
$programsToFetch = [$availablePrograms[$programId]];
}

$programDetails = $programs[$programId];
$authToken = $programDetails['auth_token'];
foreach ($programsToFetch as $programId => $programDetails) {
$authToken = $programDetails['auth_token'];

// We'll pass the auth key for our program through via a request attribute
$requestAttributes = [
'auth_token' => $authToken,
];
// We'll pass the auth key for our program through via a request attribute
$requestAttributes = [
'auth_token' => $authToken,
];

$output->writeln(
"<comment>OneHydra programId: {$input->getOption('programId')}</comment>"
);
$output->writeln("<comment>OneHydra programId: ".$programId."</comment>");

$pagesResult = $api->getPagesResult($count, $since, $requestAttributes);
$pageUrls = $pagesResult->getPageUrls();
$pagesResult = $api->getPagesResult($count, $since, $requestAttributes);
$pageUrls = $pagesResult->getPageUrls();

// List of all pages
$output->writeln('<info>Fetching a list of the pages to update</info>');
$progress = $this->getHelper('progress');
$progress->start($output, count($pageUrls));
$progress->setRedrawFrequency(1);
// List of all pages
$output->writeln('<info>Fetching a list of the pages to update</info>');
$progress = $this->getHelper('progress');
$progress->start($output, count($pageUrls));
$progress->setRedrawFrequency(1);

$output->writeln('<info>Fetching and updating each page</info>');
$output->writeln('<info>Fetching and updating each page</info>');

foreach ($pageUrls as $pageUrl) {
// Load the page from the OneHydra API
$pageResult = $api->getPageResult($pageUrl, $requestAttributes);
$page = $pageResult->getPage();
foreach ($pageUrls as $pageUrl) {
// Load the page from the OneHydra API
$pageResult = $api->getPageResult($pageUrl, $requestAttributes);
$page = $pageResult->getPage();

// Save it on our system
$pageManager->addPage($page, $programId);
// Save it on our system
$pageManager->addPage($page, $programId);

$progress->advance();
$progress->advance();
}
}

$output->writeln('<info>Done!</info>');
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/config.yml
Expand Up @@ -9,6 +9,8 @@ amara_one_hydra:
programs:
example:
auth_token: authtoken1
example2:
auth_token: authtoken2

services:
# Create a "mocked" entity manager
Expand Down
84 changes: 82 additions & 2 deletions Tests/Integration/CommandTest.php
Expand Up @@ -52,7 +52,7 @@ protected function setUp()
$this->entityManager = $entityManager;
}

public function testCommandWithMockedApiAndPageManager()
public function testCommandWithMockedApiAndPageManagerWhenProgramIdOptionUsed()
{
$application = new Application($this->kernel);
$application->add(new OneHydraFetchCommand());
Expand Down Expand Up @@ -106,4 +106,84 @@ public function testCommandWithMockedApiAndPageManager()

$this->assertEquals(0, $commandTester->getStatusCode());
}
}

public function testCommandWithMockedApiAndPageManagerWhenProgramIdOptionNotUsed()
{
$application = new Application($this->kernel);
$application->add(new OneHydraFetchCommand());

$programId = 'example';
$programId2 = 'example2';

$url1 = '/foo/bar1';
$url2 = '/foo/baz2';
$url3 = '/foo/baz3';
$url4 = '/foo/baz4';

$pagesResult = new PagesResult(new HttpResponse(), [$url1, $url2]);
$pagesResult2 = new PagesResult(new HttpResponse(), [$url3, $url4]);

$page1 = $this->prophesize(PageInterface::class);
$page2 = $this->prophesize(PageInterface::class);
$page3 = $this->prophesize(PageInterface::class);
$page4 = $this->prophesize(PageInterface::class);

$pageResult1 = new PageResult(new HttpResponse(), $page1->reveal());
$pageResult2 = new PageResult(new HttpResponse(), $page2->reveal());
$pageResult3 = new PageResult(new HttpResponse(), $page3->reveal());
$pageResult4 = new PageResult(new HttpResponse(), $page4->reveal());

$api = $this->prophesize(Api::class);

$requestAttributes = ['auth_token' => 'authtoken1'];
$requestAttributes2 = ['auth_token' => 'authtoken2'];

$api->getPagesResult(
50,
Argument::type('DateTime'),
$requestAttributes
)->willReturn($pagesResult);

$api->getPagesResult(
50,
Argument::type('DateTime'),
$requestAttributes2
)->willReturn($pagesResult2);

$api->getPageResult(
$url1,
$requestAttributes
)->willReturn($pageResult1);

$api->getPageResult(
$url2,
$requestAttributes
)->willReturn($pageResult2);

$api->getPageResult(
$url3,
$requestAttributes2
)->willReturn($pageResult3);

$api->getPageResult(
$url4,
$requestAttributes2
)->willReturn($pageResult4);

$this->container->set('amara_one_hydra.api', $api->reveal());

$pageManager = $this->prophesize(PageManager::class);
$pageManager->addPage($page1, $programId)->shouldBeCalled();
$pageManager->addPage($page2, $programId)->shouldBeCalled();
$pageManager->addPage($page3, $programId2)->shouldBeCalled();
$pageManager->addPage($page4, $programId2)->shouldBeCalled();

$this->container->set('amara_one_hydra.page_manager', $pageManager->reveal());

$command = $application->find('onehydra:fetch');
$commandTester = new CommandTester($command);
$commandTester->execute(['command' => $command->getName()]);

$this->assertEquals(0, $commandTester->getStatusCode());
}
}
2 changes: 1 addition & 1 deletion Tests/Integration/ContainerTest.php
Expand Up @@ -85,7 +85,7 @@ public function testDefaultServicesAndParamsLookOkay()
$this->assertEquals(false, $this->container->getParameter('amara_one_hydra.is_not_uat'));
$this->assertEquals('PT15M', $this->container->getParameter('amara_one_hydra.dateinterval'));
$this->assertEquals(
['example' => ['auth_token' => 'authtoken1']],
['example' => ['auth_token' => 'authtoken1'], 'example2' => ['auth_token' => 'authtoken2']],
$this->container->getParameter('amara_one_hydra.programs')
);
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/Service/PageManagerTest.php
Expand Up @@ -53,7 +53,7 @@ public function testGetPage()
$pageStorage = $this->prophesize(PageStorageInterface::class);

// Should be called just once because of the local cache!
$pageStorage->getPageEntity($pageName, $programId)->willReturn($pageEntity)->shouldBeCalledTimes(1);
$pageStorage->getPageEntity($pageName, $programId)->willReturn($pageEntity)->shouldBeCalledTimes(2);

$pageManager = new PageManager();
$pageManager->setPageStorage($pageStorage->reveal());
Expand All @@ -76,7 +76,7 @@ public function testGetPageByRequestWithoutAttributesAlready()

$pageStorage = $this->prophesize(PageStorageInterface::class);
// Should be called just once because of the local cache!
$pageStorage->getPageEntity($pageName, $programId)->willReturn($pageEntity)->shouldBeCalledTimes(1);
$pageStorage->getPageEntity($pageName, $programId)->willReturn($pageEntity)->shouldBeCalledTimes(2);

$pageTransformStrategy = $this->prophesize(PageTransformStrategyInterface::class);
$pageTransformStrategy->getLookupProgramId($request)->willReturn($programId);
Expand Down Expand Up @@ -109,7 +109,7 @@ public function testGetPageByRequestWithAttributesAlready()

$pageStorage = $this->prophesize(PageStorageInterface::class);
// Should be called just once because of the local cache!
$pageStorage->getPageEntity($pageName, $programId)->willReturn($pageEntity)->shouldBeCalledTimes(1);
$pageStorage->getPageEntity($pageName, $programId)->willReturn($pageEntity)->shouldBeCalledTimes(2);

$pageTransformStrategy = $this->prophesize(PageTransformStrategyInterface::class);
$pageTransformStrategy->getLookupProgramId($request)->shouldNotBeCalled();
Expand Down

0 comments on commit 45c97f4

Please sign in to comment.