From b44d11a884bdc86f27cd5267358a534f29e60b43 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Sun, 18 Oct 2015 00:28:52 +0300 Subject: [PATCH 1/5] Add code activities to the ch5 (behat-hooks-background) --- .../CheckStepTestResultCoding.php | 209 ++++++++++++++++++ .../afterStepHookCoding.php | 188 ++++++++++++++++ 2 files changed, 397 insertions(+) create mode 100644 knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php create mode 100644 knpu/Challenges/BehatHooksBackground/afterStepHookCoding.php diff --git a/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php b/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php new file mode 100644 index 0000000..af089f2 --- /dev/null +++ b/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php @@ -0,0 +1,209 @@ +addFileContents('features/bootstrap/FeatureContext.php', <<output = shell_exec(\$command); + } + + /** + * @Then I should see :string in the output + */ + public function iShouldSeeInTheOutput(\$string) + { + if (strpos(\$this->output, \$string) === false) { + throw new \Exception(sprintf('Did not see "%s" in output "%s"', \$string, \$this->output)); + } + } +} +EOF + ) + ->addFileContents('ls.feature', <<setEntryPointFilename('features/bootstrap/FeatureContext.php') + ; + + return $fileBuilder; + } + + public function getExecutionMode() + { + return self::EXECUTION_MODE_PHP_NORMAL; + } + + public function setupContext(CodingContext $context) + { + // Create dummy interfaces if not exists that implemented by FeatureContext class. + if (!class_exists('\Behat\Behat\Context\Context')) { + eval(<<hasMethod('afterStepHook')) { + throw new GradingException('Method `afterStepHook()` doesn\'t found in the `FeatureContext` class. Did you create it?'); + } + if (!$featureContextClass->getMethod('afterStepHook')->isPublic()) { + throw new GradingException('Method `afterStepHook()` should be public.'); + } + $afterStepHookMethod = $featureContextClass->getMethod('afterStepHook'); + $docComment = $afterStepHookMethod->getDocComment(); + if (false === strpos($docComment, '@AfterStep')) { + throw new GradingException('You should to use `@AfterStep` annotation for `afterStepHook()` method.'); + } + $afterStepHookParameters = $afterStepHookMethod->getParameters(); + if (1 !== $afterStepHookMethod->getNumberOfRequiredParameters()) { + throw new GradingException('Method `afterStepHook()` should get one required parameter.'); + } + if (0 !== strcmp('event', $afterStepHookParameters[0]->getName())) { + throw new GradingException('First argument in the `afterStepHook()` method should be named `$event`.'); + } + } + + public function configureCorrectAnswer(CorrectAnswer $correctAnswer) + { + $correctAnswer + ->setFileContents('features/bootstrap/FeatureContext.php', <<getTestResult()->isPassed(); + + var_dump(\$isPassed); + } + + /** + * @Given I have a file named :filename + */ + public function iHaveAFileNamed(\$filename) + { + touch(\$filename); + } + + /** + * @When I run :command + */ + public function iRun(\$command) + { + \$this->output = shell_exec(\$command); + } + + /** + * @Then I should see :string in the output + */ + public function iShouldSeeInTheOutput(\$string) + { + if (strpos(\$this->output, \$string) === false) { + throw new \Exception(sprintf('Did not see "%s" in output "%s"', \$string, \$this->output)); + } + } +} +EOF + ) + ; + } +} diff --git a/knpu/Challenges/BehatHooksBackground/afterStepHookCoding.php b/knpu/Challenges/BehatHooksBackground/afterStepHookCoding.php new file mode 100644 index 0000000..6d2d2c7 --- /dev/null +++ b/knpu/Challenges/BehatHooksBackground/afterStepHookCoding.php @@ -0,0 +1,188 @@ +addFileContents('features/bootstrap/FeatureContext.php', <<output = shell_exec(\$command); + } + + /** + * @Then I should see :string in the output + */ + public function iShouldSeeInTheOutput(\$string) + { + if (strpos(\$this->output, \$string) === false) { + throw new \Exception(sprintf('Did not see "%s" in output "%s"', \$string, \$this->output)); + } + } +} +EOF + ) + ->addFileContents('ls.feature', <<setEntryPointFilename('features/bootstrap/FeatureContext.php') + ; + + return $fileBuilder; + } + + public function getExecutionMode() + { + return self::EXECUTION_MODE_PHP_NORMAL; + } + + public function setupContext(CodingContext $context) + { + // Create dummy interfaces if not exists that implemented by FeatureContext class. + if (!class_exists('\Behat\Behat\Context\Context')) { + eval(<<hasMethod('afterStepHook')) { + throw new GradingException('Method `afterStepHook()` doesn\'t found in the `FeatureContext` class. Did you create it?'); + } + if (!$featureContextClass->getMethod('afterStepHook')->isPublic()) { + throw new GradingException('Method `afterStepHook()` should be public.'); + } + $docComment = $featureContextClass->getMethod('afterStepHook')->getDocComment(); + if (false === strpos($docComment, '@AfterStep')) { + throw new GradingException('You should to use `@AfterStep` annotation for `afterStepHook()` method.'); + } + } + + public function configureCorrectAnswer(CorrectAnswer $correctAnswer) + { + $correctAnswer + ->setFileContents('features/bootstrap/FeatureContext.php', <<output = shell_exec(\$command); + } + + /** + * @Then I should see :string in the output + */ + public function iShouldSeeInTheOutput(\$string) + { + if (strpos(\$this->output, \$string) === false) { + throw new \Exception(sprintf('Did not see "%s" in output "%s"', \$string, \$this->output)); + } + } +} +EOF + ) + ; + } +} From 88c8cea195bbaa25c56b05d44e2356075f35f2a1 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Mon, 19 Oct 2015 08:36:04 -0400 Subject: [PATCH 2/5] activating challenges --- knpu/metadata.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/knpu/metadata.yml b/knpu/metadata.yml index aa649b5..0170511 100644 --- a/knpu/metadata.yml +++ b/knpu/metadata.yml @@ -52,6 +52,9 @@ chapters: behat-hooks-background: video_key: is_finished: false + challenges: + - Challenges\BehatHooksBackground\afterStepHookCoding + - Challenges\BehatHooksBackground\CheckStepTestResultCoding using-mink: video_key: is_finished: false From b6f29f10179171f3e30344a1c547387f7e687e0a Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Mon, 19 Oct 2015 08:46:49 -0400 Subject: [PATCH 3/5] removing the second challenge until we're ready - I really want the user to execute this --- knpu/metadata.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knpu/metadata.yml b/knpu/metadata.yml index 0170511..4726563 100644 --- a/knpu/metadata.yml +++ b/knpu/metadata.yml @@ -54,7 +54,7 @@ chapters: is_finished: false challenges: - Challenges\BehatHooksBackground\afterStepHookCoding - - Challenges\BehatHooksBackground\CheckStepTestResultCoding + # - Challenges\BehatHooksBackground\CheckStepTestResultCoding using-mink: video_key: is_finished: false From 18eec94801a78fe4a97f040ea88c5d59b3432407 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 19 Oct 2015 16:10:48 +0300 Subject: [PATCH 4/5] Fix incorrect bracket --- .../BehatHooksBackground/CheckStepTestResultCoding.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php b/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php index af089f2..08d6215 100644 --- a/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php +++ b/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php @@ -20,7 +20,7 @@ public function getQuestion() Whenever you have a hook function, you're actually passed an `\$event` argument object that contains information about what's happening inside Behat at this moment. The exact object depends on which hook you're using -(see [Behat Hooks](http://docs.behat.org/en/v3.0/guides/3.hooks.html#hooks)]. +(see [Behat Hooks](http://docs.behat.org/en/v3.0/guides/3.hooks.html#hooks)). In `afterStepHook()`, add an `\$event` argument and use it to figure out if the step that was just executed passed or failed. Replace `var_dump('After Step!')` From 99a8f8cd9396bcda00aef47197cd452277e58266 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 19 Oct 2015 16:27:25 +0300 Subject: [PATCH 5/5] Improve some grading error messages --- .../BehatHooksBackground/CheckStepTestResultCoding.php | 8 ++++---- .../BehatHooksBackground/afterStepHookCoding.php | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php b/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php index 08d6215..d9e03ec 100644 --- a/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php +++ b/knpu/Challenges/BehatHooksBackground/CheckStepTestResultCoding.php @@ -133,10 +133,10 @@ public function grade(CodingExecutionResult $result) { $featureContextClass = new \ReflectionClass('FeatureContext'); if (!$featureContextClass->hasMethod('afterStepHook')) { - throw new GradingException('Method `afterStepHook()` doesn\'t found in the `FeatureContext` class. Did you create it?'); + throw new GradingException('The `afterStepHook()` method wasn\'t found in the `FeatureContext` class.'); } if (!$featureContextClass->getMethod('afterStepHook')->isPublic()) { - throw new GradingException('Method `afterStepHook()` should be public.'); + throw new GradingException('The `afterStepHook()` method should be public.'); } $afterStepHookMethod = $featureContextClass->getMethod('afterStepHook'); $docComment = $afterStepHookMethod->getDocComment(); @@ -145,10 +145,10 @@ public function grade(CodingExecutionResult $result) } $afterStepHookParameters = $afterStepHookMethod->getParameters(); if (1 !== $afterStepHookMethod->getNumberOfRequiredParameters()) { - throw new GradingException('Method `afterStepHook()` should get one required parameter.'); + throw new GradingException('Make sure you give the `afterStepHook()` method exactly one argument.'); } if (0 !== strcmp('event', $afterStepHookParameters[0]->getName())) { - throw new GradingException('First argument in the `afterStepHook()` method should be named `$event`.'); + throw new GradingException('Though you can really call it anything, let\'s call the argument to `afterStepHook()` `$event` for clarity.'); } } diff --git a/knpu/Challenges/BehatHooksBackground/afterStepHookCoding.php b/knpu/Challenges/BehatHooksBackground/afterStepHookCoding.php index 6d2d2c7..c648f0c 100644 --- a/knpu/Challenges/BehatHooksBackground/afterStepHookCoding.php +++ b/knpu/Challenges/BehatHooksBackground/afterStepHookCoding.php @@ -123,10 +123,10 @@ public function grade(CodingExecutionResult $result) { $featureContextClass = new \ReflectionClass('FeatureContext'); if (!$featureContextClass->hasMethod('afterStepHook')) { - throw new GradingException('Method `afterStepHook()` doesn\'t found in the `FeatureContext` class. Did you create it?'); + throw new GradingException('The `afterStepHook()` method wasn\'t found in the `FeatureContext` class.'); } if (!$featureContextClass->getMethod('afterStepHook')->isPublic()) { - throw new GradingException('Method `afterStepHook()` should be public.'); + throw new GradingException('The `afterStepHook()` method should be public.'); } $docComment = $featureContextClass->getMethod('afterStepHook')->getDocComment(); if (false === strpos($docComment, '@AfterStep')) {