Fixed code highlighting in tutorial

This commit is contained in:
Ingo Schommer 2013-10-21 19:13:14 +02:00
parent 2ae8cba590
commit 8c8fd5923d

View File

@ -43,7 +43,8 @@ In this spirit, we'll start "from the outside in", and
write our features before implementing them. A first draft might look write our features before implementing them. A first draft might look
something like the following: something like the following:
Feature: Report Abuse ```cucumber
Feature: Report Abuse
As a website user As a website user
I want to report inappropriate content I want to report inappropriate content
@ -55,6 +56,7 @@ something like the following:
When I select "Outdated" When I select "Outdated"
And I press the button And I press the button
Then I should see "Thanks for your submission" Then I should see "Thanks for your submission"
```
The "syntax" conventions used here are called the The "syntax" conventions used here are called the
["Gherkin" language](https://github.com/cucumber/cucumber/wiki/Gherkin). ["Gherkin" language](https://github.com/cucumber/cucumber/wiki/Gherkin).
@ -76,12 +78,14 @@ so our next move is to review what's already available:
The step definitions include form interactions, so we only The step definitions include form interactions, so we only
need to adjust our steps a bit to make them executable. need to adjust our steps a bit to make them executable.
Scenario: Report abuse through preselected options ```cucumber
Scenario: Report abuse through preselected options
Given I go to a page Given I go to a page
Then I should see "Report this page" Then I should see "Report this page"
When I select "Outdated" from "Reason" When I select "Outdated" from "Reason"
And I press "Submit Report" And I press "Submit Report"
Then I should see "Thanks for your submission" Then I should see "Thanks for your submission"
```
This type of refactoring is quite common, since step definitions This type of refactoring is quite common, since step definitions
are ideally abstracted and shared between features. In this case, are ideally abstracted and shared between features. In this case,
@ -97,10 +101,12 @@ Each test run starts with a clean database, meaning there's no pages
to open in a browser either. Let's fix this by defining one, to open in a browser either. Let's fix this by defining one,
and asking Behat to open it: and asking Behat to open it:
Scenario: Report abuse through preselected options ```cucumber
Scenario: Report abuse through preselected options
Given a "page" "My Page" Given a "page" "My Page"
Given I go to the "page" "My Page" Given I go to the "page" "My Page"
... ...
```
## SilverStripe Code ## SilverStripe Code
@ -110,12 +116,12 @@ but overall we're creating a new `PageAbuseReport` class with
a `has_one` relationship to `Page`. This new object gets written a `has_one` relationship to `Page`. This new object gets written
by a form generated through `Page_Controller->ReportForm()`. by a form generated through `Page_Controller->ReportForm()`.
```php ```php
// mysite/code/Page.php // mysite/code/Page.php
class Page extends SiteTree { class Page extends SiteTree {
private static $has_many = array('PageAbuseReports' => 'PageAbuseReport'); private static $has_many = array('PageAbuseReports' => 'PageAbuseReport');
} }
class Page_Controller extends ContentController { class Page_Controller extends ContentController {
// ... // ...
@ -150,29 +156,29 @@ by a form generated through `Page_Controller->ReportForm()`.
return $this->redirectBack(); return $this->redirectBack();
} }
} }
``` ```
```php ```php
// mysite/code/PageAbuseReport.php // mysite/code/PageAbuseReport.php
class PageAbuseReport extends DataObject { class PageAbuseReport extends DataObject {
private static $db = array('Reason' => 'Text'); private static $db = array('Reason' => 'Text');
private static $has_one = array('Page' => 'Page'); private static $has_one = array('Page' => 'Page');
} }
``` ```
Now we just need to render the form on every page, Now we just need to render the form on every page,
by placing it at the bottom of `themes/simple/templates/Layout/Page.ss`: by placing it at the bottom of `themes/simple/templates/Layout/Page.ss`:
```html ```html
<div class="content-container unit size3of4 lastUnit"> <div class="content-container unit size3of4 lastUnit">
<article> <article>
<h1>$Title</h1> <h1>$Title</h1>
<div class="content">$Content</div> <div class="content">$Content</div>
</article> </article>
$ReportForm $ReportForm
</div> </div>
``` ```
You can try out this feature in your browser without Behat. You can try out this feature in your browser without Behat.
Don't forget to rebuild the database (`dev/build`) and flush the Don't forget to rebuild the database (`dev/build`) and flush the
@ -193,23 +199,25 @@ and a clean slate on each run.
At the end of our `report-abuse.feature` file, add the following: At the end of our `report-abuse.feature` file, add the following:
Scenario: Report abuse through preselected options ```cucumber
Scenario: Report abuse through preselected options
... ...
Then I should see "Thanks for your submission" Then I should see "Thanks for your submission"
And there should be an abuse report for "My Page" with reason "Outdated" And there should be an abuse report for "My Page" with reason "Outdated"
```
Running behat again will produce an undefined step, with a helpful PHP boilerplate Running behat again will produce an undefined step, with a helpful PHP boilerplate
to get us started: to get us started:
```php ```php
/** /**
* @Given /^there should be an abuse report for "([^"]*)" with reason "([^"]*)"$/ * @Given /^there should be an abuse report for "([^"]*)" with reason "([^"]*)"$/
*/ */
public function thereShouldBeAnAbuseReportForWithReason($arg1, $arg2) public function thereShouldBeAnAbuseReportForWithReason($arg1, $arg2)
{ {
throw new PendingException(); throw new PendingException();
} }
``` ```
This code can be placed in a "context" class which was created during our This code can be placed in a "context" class which was created during our
module initialization. Its located in module initialization. Its located in
@ -231,16 +239,16 @@ so if you have done unit testing before the `assertEquals()` call might
look familiar. Either way, it will throw an exception if there's look familiar. Either way, it will throw an exception if there's
not exactly one record found in the relation, and hence fail that step for Behat. not exactly one record found in the relation, and hence fail that step for Behat.
```php ```php
/** /**
* @Given /^there should be an abuse report for "([^"]*)" with reason "([^"]*)"$/ * @Given /^there should be an abuse report for "([^"]*)" with reason "([^"]*)"$/
*/ */
public function thereShouldBeAnAbuseReportForWithReason($id, $reason) public function thereShouldBeAnAbuseReportForWithReason($id, $reason)
{ {
$page = $this->fixtureFactory->get('Page', $id); $page = $this->fixtureFactory->get('Page', $id);
assertEquals(1, $page->PageAbuseReports()->filter('Reason', $reason)->Count()); assertEquals(1, $page->PageAbuseReports()->filter('Reason', $reason)->Count());
} }
``` ```
Re-run the Behat test one last time, and you should see it pass with Re-run the Behat test one last time, and you should see it pass with
a more solid feature definition. Success! If you want to get your hands dirty, a more solid feature definition. Success! If you want to get your hands dirty,