GitHub PullRequestReviewCommentEvent at 2014-05-30 10:54:22
Repository: Sylius
Actor: gonzalovilaseca
Payload: {"comment":{"url":"https://api.github.com/repos/Sylius/Sylius/pulls/comments/13224268","id":13224268,"diff_hunk":"@@ -0,0 +1,798 @@\n+<?php\n+\n+namespace Sylius\\Bundle\\JobSchedulerBundle\\Behat;\n+\n+use Behat\\Gherkin\\Node\\TableNode;\n+use Sylius\\Bundle\\ResourceBundle\\Behat\\DefaultContext;\n+use Symfony\\Component\\Locale\\Locale;\n+use Sylius\\Bundle\\JobSchedulerBundle\\Entity\\Job;\n+use Symfony\\Bundle\\FrameworkBundle\\Console\\Application;\n+use Symfony\\Component\\Console\\Tester\\CommandTester;\n+use Sylius\\Bundle\\JobSchedulerBundle\\Command\\RunActiveJobsCommand;\n+use Behat\\Behat\\Hook\\Scope\\AfterStepScope;\n+use Behat\\Testwork\\Tester\\Result\\TestResult;\n+use Sylius\\Bundle\\JobSchedulerBundle\\Entity\\JobStatus;\n+use Behat\\Mink\\Driver\\Selenium2Driver;\n+\n+require_once 'PHPUnit/Autoload.php';\n+require_once 'PHPUnit/Framework/Assert/Functions.php';\n+\n+/**\n+ * Feature context.\n+ */\n+class JobSchedulerContext extends DefaultContext\n+{\n+    private $application;\n+    private $tester;\n+    private $em;\n+\n+    /**\n+     * @BeforeScenario\n+     */\n+    public function setApp()\n+    {\n+        $this->application = new Application($this->kernel);\n+        $this->application->add(new RunActiveJobsCommand());\n+\n+        $this->em = $this->kernel->getContainer()->get('doctrine')->getManager();\n+\n+\n+    }\n+\n+    /**\n+     * @When /^I run \"([^\"]*)\" command$/\n+     */\n+    public function iRunCommand($arg1)\n+    {\n+        $command = $this->application->find($arg1);\n+        $this->tester = new CommandTester($command);\n+        $this->tester->execute(array('command' => $command->getName()));\n+    }\n+\n+    /**\n+     * @Given /^I have a job with command \"([^\"]*)\" and schedule \"([^\"]*)\" that should run on \"([^\"]*)\" server type$/\n+     */\n+    public function iHaveAJobWithCommandAndScheduleThatShouldRunOnServerType($command, $schedule, $serverType)\n+    {\n+        $this->thereIsJob($command, $schedule);\n+        $job = $this->getJob($command);\n+        $job->setServerType($serverType);\n+\n+        $this->em->persist($job);\n+        $this->em->flush();\n+\n+    }\n+\n+    /**\n+     * @When /^I run \"([^\"]*)\" command on a \"([^\"]*)\" server type$/\n+     */\n+    public function iRunCommandOnAServerType($command, $serverType)\n+    {\n+        putenv('ST=' . $serverType);\n+        $this->iRunCommand($command);\n+\n+    }\n+\n+    /**\n+     * @Given /^I have a job with command \"([^\"]*)\" and schedule \"([^\"]*)\" that should run on \"([^\"]*)\" environment$/\n+     */\n+    public function iHaveAJobWithCommandAndScheduleThatShouldRunOnEnvironment($command, $schedule, $environment)\n+    {\n+        $this->thereIsJob($command, $schedule);\n+        $job = $this->getJob($command);\n+        $job->setEnvironment($environment);\n+        $this->em->persist($job);\n+        $this->em->flush();\n+    }\n+\n+    /**\n+     * @When /^I run \"([^\"]*)\" command on a \"([^\"]*)\" environment$/\n+     */\n+    public function iRunCommandOnAEnvironment($command, $environment)\n+    {\n+        putenv('ENV=' . $environment);\n+        $this->iRunCommand($command);\n+    }\n+\n+    /**\n+     * @Then /^The last log of \"([^\"]*)\" command should contain$/\n+     */\n+    public function theLastLogOfCommandShouldContain($command, $string)\n+    {\n+        $output = $this->getOutput($command);\n+        assertSame($string->getRaw(), $output);\n+    }\n+\n+    /**\n+     * @Then /^The last log of \"([^\"]*)\" command should not contain$/\n+     */\n+    public function theLastLogOfCommandShouldNotContain($command, $string)\n+    {\n+        $output = $this->getOutput($command);\n+\n+        assertNotSame($string->getRaw(), $output);\n+    }\n+\n+    private function getOutput($command)\n+    {\n+        $job = $this->getJob($command);\n+        $logs = $this->em->getRepository('SyliusJobSchedulerBundle:JobLog')->findBy(array('job' => $job->getId()));\n+\n+        if (count($logs) > 0) {\n+            $log = $logs[0];\n+            $output = $log->getOutput();\n+        } else {\n+            $output = '';\n+        }\n+\n+        return $output;\n+    }\n+\n+    private function getJob($command)\n+    {\n+        return $job = $this->em->getRepository('SyliusJobSchedulerBundle:Job')->findOneByCommand($command);\n+    }\n+\n+    /**\n+     * @Given /^I wait \"([^\"]*)\" seconds$/\n+     */\n+    public function iWaitSeconds($seconds)\n+    {\n+        sleep($seconds);\n+    }\n+\n+    /**\n+     * @Then /^There should be \"([^\"]*)\" logs of command \"([^\"]*)\"$/\n+     */\n+    public function thereShouldBeLogsOfCommand($numLogs, $command)\n+    {\n+        $logs = $this->logsOfCommand($command, JobStatus::SUCCESS);\n+\n+        assertSame(count($logs), (int)$numLogs);\n+    }\n+\n+    /**\n+     * @Given /^Settings are \"([^\"]*)\"$/\n+     */\n+    public function iHaveSettings($arg1)\n+    {\n+        $active = false;\n+        if ($arg1 == 'enabled') {\n+            $active = true;\n+        }\n+        $settings = $this->kernel->getContainer()->get('sylius.settings.manager')->loadSettings('job_scheduler');\n+\n+        $settings->set('enabled', $active);\n+\n+        $this->kernel->getContainer()->get('sylius.settings.manager')->saveSettings('job_scheduler', $settings);\n+    }\n+\n+    /**\n+     * @Given /^I have a job with command \"([^\"]*)\" and schedule \"([^\"]*)\" and priority \"([^\"]*)\"$/\n+     */\n+    public function iHaveAJobWithCommandAndScheduleAndPriority($command, $schedule, $priority)\n+    {\n+        $this->thereIsJob($command, $schedule);\n+        $job = $this->getJob($command);\n+        $job->setPriority($priority);\n+\n+        $this->em->persist($job);\n+        $this->em->flush();\n+    }\n+\n+    /**\n+     * @Then /^\"([^\"]*)\" should have been executed before \"([^\"]*)\"$/\n+     */\n+    public function shouldHaveBeenExecutedBefore($command1, $command2)\n+    {\n+        $diff = 0;\n+        //We have to rely on log id as we cant store milliseconds\n+        $job = $this->getJob($command1);\n+        $logs = $this->em->getRepository('SyliusJobSchedulerBundle:JobLog')->findBy(array('job' => $job->getId()));\n+        if (array_key_exists(0, $logs)) {\n+            $exec1 = $logs[0]->getStartedAt();\n+        }\n+\n+\n+        $job = $this->getJob($command2);\n+        $logs = $this->em->getRepository('SyliusJobSchedulerBundle:JobLog')->findBy(array('job' => $job->getId()));\n+        if (array_key_exists(0, $logs)) {\n+            $exec2 = $logs[0]->getStartedAt();\n+        }\n+\n+        assertGreaterThan(0, $exec2-$exec1);\n+\n+    }\n+\n+\n+    /**\n+     * @When /^I run \"([^\"]*)\" manually$/\n+     */\n+    public function iRunManually($command)\n+    {\n+        $job = $this->getJob($command);\n+        $this->kernel->getContainer()->get('sylius.scheduler.job.manager')->runJobAsync($job->getId());\n+    }\n+\n+    /**\n+     * @Given /^There should be \"([^\"]*)\" error logs of command \"([^\"]*)\"$/\n+     */\n+    public function thereShouldBeErrorLogsOfCommand($numLogs, $command)\n+    {\n+        $logs = $this->logsOfCommand($command, JobStatus::FAILED);\n+        assertSame(count($logs), (int)$numLogs);\n+    }\n+\n+    private function logsOfCommand($command, $status)\n+    {\n+        $job = $this->getJob($command);\n+        return $this->em->getRepository('SyliusJobSchedulerBundle:JobLog')->findBy(array('job' => $job->getId(), 'status' => $status));\n+\n+    }\n+\n+    /**\n+     * @Given /^I have a job with command \"([^\"]*)\" and schedule \"([^\"]*)\" and active \"([^\"]*)\"$/\n+     */\n+    public function iHaveAJobWithCommandAndScheduleAndActive($command, $schedule, $active)\n+    {\n+        $this->thereIsJob($command, $schedule);\n+        $job = $this->getJob($command);\n+        $job->setActive($active);\n+\n+        $this->em->persist($job);\n+        $this->em->flush();\n+    }\n+\n+\n+    /**\n+     * @Given /^I am not logged in as an administrator$/\n+     */\n+    public function iAmNotLoggedInAsAnAdministrator()\n+    {\n+        $this->getSecurityContext()->setToken(null);\n+        $this->getContainer()->get('session')->invalidate();\n+    }\n+\n+\n+    /**\n+     * @Then /^I should not be able to access the page$/\n+     */\n+    public function iShouldNotBeAbleToAccessThePage()\n+    {\n+        $this->assertSession()->addressEquals($this->generatePageUrl('sylius_backend_security_login'));\n+        $this->assertSession()->statusCodeEquals(200);\n+    }\n+\n+    /**\n+     * @Given /^there are no jobs$/\n+     */\n+    public function thereAreNoJobs()\n+    {\n+\n+\n+        $jobs = $this->em->getRepository('SyliusJobSchedulerBundle:Job')->findAll();\n+        foreach ($jobs as $job) {\n+            $this->em->remove($job);\n+        }\n+        $this->em->flush();\n+    }\n+\n+\n+    /**\n+     * @Then /^I should be able to see \"([^\"]*)\" jobs in the list$/\n+     */\n+    public function iShouldBeAbleToSeeJobInTheList($num)\n+    {\n+\n+        $tables = $this->getSession()->getPage()->findAll('css', $this->getJobTableSelector());\n+        if (!isset($tables[0])) {\n+            throw new \\Exception(sprintf('The %d table \"%s\" was not found in the page'));\n+        }\n+\n+        $rows = $tables[0]->findAll('css', 'tbody tr');\n+        assertEquals($num, count($rows));\n+    }\n+\n+\n+    /**\n+     * @Given /^I should see a job with \"([^\"]*)\" value \"([^\"]*)\" in the list$/\n+     */\n+    public function iShouldSeeAJobWithValueInTheList($columnHeader, $value)\n+    {\n+        $colIndex = $this->headerColumn($columnHeader);\n+        $this->theStColumnOfTheStRowInTheTableShouldContain($colIndex, 1, $this->getJobTableSelector(), $value);\n+    }\n+\n+    /**\n+     * @Given /^I should see a job with \"([^\"]*)\" in the list$/\n+     */\n+    public function iShouldSeeAJobWithInTheList($columnHeader)\n+    {\n+        $colIndex = $this->headerColumn($columnHeader);\n+        assertGreaterThan(0, $colIndex);\n+    }\n+\n+\n+    /**\n+     * @Given /^I should see a job with active \"([^\"]*)\"$/\n+     */\n+    public function iShouldSeeAJobWithActive($active)\n+    {\n+        $this->theStColumnOfTheStRowInTheTableShouldContain(7, 1, 'table#job-table', $active);\n+    }\n+\n+    /**\n+     * @Given /^I should see \"([^\"]*)\" unchecked$/\n+     */\n+    public function iShouldSeeUnchecked($field)\n+    {\n+        $id = 'form_' . strtolower($field);\n+\n+        assertTrue($this->getSession()->getPage()->hasUncheckedField($id));\n+    }\n+\n+    /**\n+     * @Then /^I should be editing \"([^\"]*)\" job$/\n+     */\n+    public function iShouldBeEditingJob($command)\n+    {\n+        $job = $this->getJob($command);\n+        $this->assertSession()->addressEquals($this->generateUrl('sylius_backend_job_update', array('id' => $job->getId())));\n+\n+    }\n+\n+    /**\n+     * @Given /^I am editing \"([^\"]*)\" job$/\n+     */\n+    public function iAmEditingJob($command)\n+    {\n+        $job = $this->getJob($command);\n+        $this->getSession()->visit($this->generatePageUrl('sylius_backend_job_update', array('id' => $job->getId())));\n+    }\n+\n+    /**\n+     * @Given /^I should not see \"([^\"]*)\" job in the jobs list$/\n+     */\n+    public function iShouldNotSeeAJobInTheList($command)\n+    {\n+        $commandRowIndex = $this->commandRowIndex($command);\n+        assertEquals(0, $commandRowIndex);\n+    }\n+\n+    /**\n+     * @Given /^I should not see job \"([^\"]*)\" in the list$/\n+     */\n+    public function iShouldNotSeeJobInTheList($command)\n+    {\n+        $rows = $this->getSession()->getPage()->findAll('css', 'table#job-table');\n+        assertEquals(0, count($rows));\n+    }\n+\n+    private function commandRowIndex($command)\n+    {\n+        $colIndex = $this->headerColumn('Command');\n+        $commandRowIndex = 0;\n+        $rows = $this->getSession()->getPage()->findAll('css', $this->getJobTableRowSelector());\n+        foreach ($rows as $key => $row) {\n+            $cols = $row->findAll('css', '');\n+            if (strtoupper($cols[$colIndex]->getText()) === strtoupper($command)) {\n+                $commandRowIndex = $key + 1;\n+                break;\n+            }\n+        }\n+\n+        return $commandRowIndex;\n+    }\n+\n+    private function headerColumn($headerColumn)\n+    {\n+        $cols = $this->getTableHeaderRow();\n+        $column = '';\n+\n+        foreach ($cols as $key => $col) {\n+            if (strtoupper($col->getText()) === strtoupper($headerColumn)) {\n+                $column = $key;\n+                break;\n+            }\n+        }\n+\n+        return $column + 1;\n+    }\n+\n+    private function tableCellValue($colIndex, $rowIndex)\n+    {\n+\n+        $rows = $this->getSession()->getPage()->findAll('css', $this->getJobTableRowSelector());\n+\n+        if (!isset($rows[$rowIndex - 1])) {\n+            throw new \\Exception(sprintf(\"The row %d was not found in the %s table\", $rowIndex, 'table#job-table tr'));\n+        }\n+\n+        $row = $rows[$rowIndex - 1];\n+        $cols = $row->findAll('css', 'td');\n+\n+        if (!isset($cols[$colIndex - 1])) {\n+            throw new \\Exception(sprintf(\"The column %d was not found in the row %d of the %s table\", $colIndex, $rowIndex, 'table#job-table tr'));\n+        }\n+\n+        $actual = $cols[$colIndex - 1]->getText();\n+\n+        return $actual;\n+    }\n+\n+    private function tableCell($colIndex, $rowIndex)\n+    {\n+\n+        $rows = $this->getSession()->getPage()->findAll('css', $this->getJobTableRowSelector());\n+\n+        if (!isset($rows[$rowIndex - 1])) {\n+            throw new \\Exception(sprintf(\"The row %d was not found in the %s table\", $rowIndex, 'table#job-table tr'));\n+        }\n+\n+        $row = $rows[$rowIndex - 1];\n+        $cols = $row->findAll('css', 'td');\n+\n+        if (!isset($cols[$colIndex - 1])) {\n+            throw new \\Exception(sprintf(\"The column %d was not found in the row %d of the %s table\", $colIndex, $rowIndex, 'table#job-table tr'));\n+        }\n+\n+        $actual = $cols[$colIndex - 1];\n+\n+        return $actual;\n+    }\n+\n+    /**\n+     * @Then /^I should see something in column \"([^\"]*)\" of \"([^\"]*)\" job$/\n+     */\n+    public function iShouldSeeSomethingInColumnOfJob($headerColumn, $command)\n+    {\n+\n+        $commandRowIndex = $this->commandRowIndex($command);\n+\n+        $headerColumn = $this->headerColumn($headerColumn);\n+\n+        $value = $this->tableCellValue($headerColumn, $commandRowIndex);\n+\n+        assertNotEmpty($value);\n+    }\n+\n+    /**\n+     * @Then /^I should see \"([^\"]*)\" in column \"([^\"]*)\" of \"([^\"]*)\" job$/\n+     */\n+    public function iShouldSeeInColumnOfJob($expected, $columnHeader, $command)\n+    {\n+        $commandRowIndex = $this->commandRowIndex($command);\n+\n+        $headerColumn = $this->headerColumn($columnHeader);\n+\n+        $value = $this->tableCellValue($headerColumn, $commandRowIndex);\n+        assertEquals($expected, $value);\n+    }\n+\n+    private function isSpinnerVissible($columnHeader, $command)\n+    {\n+        $commandRowIndex = $this->commandRowIndex($command);\n+        $headerColumn = $this->headerColumn($columnHeader);\n+\n+        $tableCell = $this->tableCell($headerColumn, $commandRowIndex);\n+        $spinner = $tableCell->find('css', 'img');\n+\n+        return $spinner->isVisible();\n+    }\n+\n+    /**\n+     * @Given /^I should see the spinner next to \"([^\"]*)\" in column \"([^\"]*)\" of \"([^\"]*)\" job$/\n+     */\n+    public function iShouldSeeTheSpinnerNextToInColumnOfJob($expected, $columnHeader, $command)\n+    {\n+        $commandRowIndex = $this->commandRowIndex($command);\n+        $headerColumn = $this->headerColumn($columnHeader);\n+        $value = $this->tableCellValue($headerColumn, $commandRowIndex);\n+        assertEquals($expected, $value);\n+\n+        $visible = $this->isSpinnerVissible($columnHeader, $command);\n+        assertTrue($visible);\n+\n+    }\n+\n+    /**\n+     * @Given /^I should not see the spinner next to \"([^\"]*)\" in column \"([^\"]*)\" of \"([^\"]*)\" job$/\n+     */\n+    public function iShouldNotSeeTheSpinnerNextToInColumnOfJob($expected, $columnHeader, $command)\n+    {\n+        $commandRowIndex = $this->commandRowIndex($command);\n+        $headerColumn = $this->headerColumn($columnHeader);\n+        $value = $this->tableCellValue($headerColumn, $commandRowIndex);\n+        assertEquals($expected, $value);\n+\n+        $visible = $this->isSpinnerVissible($columnHeader, $command);\n+        assertFalse($visible);\n+    }\n+\n+    /**\n+     * @Given /^There are no jobs$/\n+     */\n+    public function thereAreNoJobs2()","path":"src/Sylius/Bundle/JobSchedulerBundle/Behat/JobSchedulerContext.php","position":515,"original_position":515,"commit_id":"b7262c219eee37c025ed6d029cd4385ffe861b6c","original_commit_id":"b7262c219eee37c025ed6d029cd4385ffe861b6c","user":{"login":"gonzalovilaseca","id":4030438,"avatar_url":"https://avatars.githubusercontent.com/u/4030438?","gravatar_id":"a7a86364e5e68f347010507769e6e2ac","url":"https://api.github.com/users/gonzalovilaseca","html_url":"https://github.com/gonzalovilaseca","followers_url":"https://api.github.com/users/gonzalovilaseca/followers","following_url":"https://api.github.com/users/gonzalovilaseca/following{/other_user}","gists_url":"https://api.github.com/users/gonzalovilaseca/gists{/gist_id}","starred_url":"https://api.github.com/users/gonzalovilaseca/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gonzalovilaseca/subscriptions","organizations_url":"https://api.github.com/users/gonzalovilaseca/orgs","repos_url":"https://api.github.com/users/gonzalovilaseca/repos","events_url":"https://api.github.com/users/gonzalovilaseca/events{/privacy}","received_events_url":"https://api.github.com/users/gonzalovilaseca/received_events","type":"User","site_admin":false},"body":"The purpose is to verify that when you run a job manually through the backend, the spinner displays and hides correctly.","created_at":"2014-05-30T10:54:22Z","updated_at":"2014-05-30T10:54:22Z","html_url":"https://github.com/Sylius/Sylius/pull/1580#discussion_r13224268","pull_request_url":"https://api.github.com/repos/Sylius/Sylius/pulls/1580","_links":{"self":{"href":"https://api.github.com/repos/Sylius/Sylius/pulls/comments/13224268"},"html":{"href":"https://github.com/Sylius/Sylius/pull/1580#discussion_r13224268"},"pull_request":{"href":"https://api.github.com/repos/Sylius/Sylius/pulls/1580"}}}}