Skip to content

Commit

Permalink
Merge pull request #26 from knpuniversity/code-blocks-ch13
Browse files Browse the repository at this point in the history
Add code blocks to the ch13 (control-database)
  • Loading branch information
weaverryan committed Nov 9, 2015
2 parents f5c1432 + 297533e commit ac5b5ca
Show file tree
Hide file tree
Showing 26 changed files with 91 additions and 51 deletions.
4 changes: 2 additions & 2 deletions _tuts/bootstrap-clearing-data.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index e0c8ddf..11418c9 100644
index 4dd8366..5468a0d 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -40,6 +40,16 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand All @@ -18,7 +18,7 @@ index e0c8ddf..11418c9 100644
+ /**
* @Given there is an admin user :username with password :password
*/
public function thereIsAUserWithPassword($username, $password)
public function thereIsAnAdminUserWithPassword($username, $password)
diff --git a/features/web/authentication.feature b/features/web/authentication.feature
index 4c76748..f5634d5 100644
--- a/features/web/authentication.feature
Expand Down
4 changes: 2 additions & 2 deletions _tuts/bootstrap-load-symfony.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 4e9d5b3..e0c8ddf 100644
index 4e9d5b3..4dd8366 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -13,6 +13,8 @@ require_once __DIR__.'/../../vendor/phpunit/phpunit/src/Framework/Assert/Functio
Expand Down Expand Up @@ -32,7 +32,7 @@ index 4e9d5b3..e0c8ddf 100644
+ * @Given there is an admin user :username with password :password
*/
- public function thereIsAUserWithPassword($arg1, $arg2)
+ public function thereIsAUserWithPassword($username, $password)
+ public function thereIsAnAdminUserWithPassword($username, $password)
{
- throw new PendingException();
+ $user = new \AppBundle\Entity\User();
Expand Down
2 changes: 1 addition & 1 deletion _tuts/bootstrap-symfony2extension.diff
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ index 7630124..0a05ac3 100644
"version": "v2.0.4",
"source": {
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 11418c9..ec12eb6 100644
index 5468a0d..e197abc 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -13,7 +13,7 @@ require_once __DIR__.'/../../vendor/phpunit/phpunit/src/Framework/Assert/Functio
Expand Down
2 changes: 1 addition & 1 deletion _tuts/bootstrap-use-ormpurger.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index ec12eb6..184135e 100644
index e197abc..3f76945 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -5,6 +5,7 @@ use Behat\Behat\Context\SnippetAcceptingContext;
Expand Down
2 changes: 1 addition & 1 deletion _tuts/deleting-add-delete-scenario.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index e1620ae..8c40d34 100644
index 3267239..4611444 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -96,7 +96,7 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/deleting-changing-to-press.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index ad6ea82..d46f7a9 100644
index 552c040..fc52f68 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -127,16 +127,15 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/deleting-click-link-in-row-definition.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 8c40d34..2c3002c 100644
index 4611444..e54aebf 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -121,18 +121,21 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/deleting-debugging-bad-link.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index a5f5e41..ad6ea82 100644
index 6b1710d..552c040 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -133,6 +133,7 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/deleting-making-tablenode-more-flexible.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 2c3002c..a5f5e41 100644
index e54aebf..6b1710d 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -106,7 +106,7 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/deleting-use-pressbutton.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index d46f7a9..a81707f 100644
index fc52f68..3e3962b 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -131,11 +131,7 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/gherkin-optional-language.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 49872d0..ba024a0 100644
index 4545d06..793519c 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -80,7 +80,7 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/javascript-add-long-wait.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index b64867c..fe8751a 100644
index e434ed3..e753184 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -128,6 +128,14 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/javascript-add-proper-wait.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index fe8751a..8f5ced4 100644
index e753184..17376e7 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -132,7 +132,10 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/javascript-breaking.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 8f5ced4..4503f37 100644
index 17376e7..8282265 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -139,6 +139,19 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/javascript-screenshot.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 4503f37..49872d0 100644
index 8282265..4545d06 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -152,6 +152,17 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/steps.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,5 @@
"description": null
}
],
"sha": "278362a9ee1a4bcb2a25f7fbef55176f0639c6a4"
"sha": "08f7848e7fdcd84922f33a56ebf3372e5fafaee4"
}
2 changes: 1 addition & 1 deletion _tuts/tablenode-create-products.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 6b36bd6..f0453cc 100644
index 13636c4..3054669 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -101,8 +101,19 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/tablenode-look-for-text-in-table.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index f0453cc..e1620ae 100644
index 3054669..3267239 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -117,6 +117,17 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/tablenode-loop-and-dump.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index ba024a0..6b36bd6 100644
index 793519c..13636c4 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -96,6 +96,16 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/tags-adding-fixtures-code.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 884135a..f0cb487 100644
index 847b875..8a2285b 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -7,7 +7,9 @@ use Behat\Behat\Context\SnippetAcceptingContext;
Expand Down
4 changes: 2 additions & 2 deletions _tuts/tags-tagging-fixtures-with-hook.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index a81707f..884135a 100644
index 3e3962b..847b875 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -41,6 +41,14 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand All @@ -16,7 +16,7 @@ index a81707f..884135a 100644
+ /**
* @Given there is an admin user :username with password :password
*/
public function thereIsAUserWithPassword($username, $password)
public function thereIsAnAdminUserWithPassword($username, $password)
diff --git a/features/web/search.feature b/features/web/search.feature
index a1373d0..469a811 100644
--- a/features/web/search.feature
Expand Down
2 changes: 1 addition & 1 deletion _tuts/user-add-definitions-for-product-list.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 184135e..4837e8c 100644
index 3f76945..a217a23 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -1,5 +1,6 @@
Expand Down
2 changes: 1 addition & 1 deletion _tuts/user-login-first.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 4837e8c..555e142 100644
index a217a23..d004426 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -111,6 +111,19 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
2 changes: 1 addition & 1 deletion _tuts/user-new-scenario-for-owning-products.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 555e142..5aaaba1 100644
index d004426..be2d716 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -1,6 +1,7 @@
Expand Down
2 changes: 1 addition & 1 deletion _tuts/user-store-currentuser-property.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php
index 5aaaba1..b64867c 100644
index be2d716..e434ed3 100644
--- a/features/bootstrap/FeatureContext.php
+++ b/features/bootstrap/FeatureContext.php
@@ -18,6 +18,8 @@ class FeatureContext extends RawMinkContext implements Context, SnippetAccepting
Expand Down
86 changes: 63 additions & 23 deletions knpu/control-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ Instead, you should put any data you need there intentionally with a `Given`.

Go to the top of this scenario and add:

Given there is an admin user "admin" with password "admin"
[[[ code('cad91eec8d') ]]]

I'm inventing this language - it describes something that needs to be setup using
natural language. Change the `Given` line below this to `And` for readability.
natural language. Change the `Given` line below this to `And` for readability:

[[[ code('c006d7de91') ]]]

Run it again: it should print out the step definition for this new language.

Expand All @@ -21,7 +23,9 @@ Run it again: it should print out the step definition for this new language.
```

And it does! Copy that and put it into our `FeatureContext` class. Change `:arg1`
to "username" and `:arg2` to ":password". Update the arguments to match.
to `:username` and `:arg2` to `:password`. Update the arguments to match:

[[[ code('69a871dda0') ]]]

To fill this in, we want to insert a new user in the database with this username and
password. If Symfony were loaded, that would be really easy: I'd create a User
Expand All @@ -33,37 +37,73 @@ to get access to all of your normal useful objects and functions.

We only need to boot Symfony once at the beginning of the test suite. Afterwards
all of our scenarios will be able to access Symfony's container. Make a new
`public function bootstrapSymfony()` and inside, we'll do exactly what its name says.
We'll need a couple of require statements for `autoload.php` and the `AppKernel.php`
class. Then, it's as easy as `$kernel = new AppKernel();`. Pass it the environment -
`test` - and the debug value - `true` - so we can see errors. Finish with
`$kernel->boot();`. Congrats - you just bootstrapped your Symfony app.
`public function bootstrapSymfony()`:

[[[ code('7db161e9a8') ]]]

And inside, we'll do exactly what its name says.

We'll need a couple of require statements for `autoload.php` and the `AppKernel.php` class:

[[[ code('51198ff16b') ]]]

Then, it's as easy as `$kernel = new AppKernel();`. Pass it the environment - `test` -
and the debug value - `true` - so we can see errors. Finish with `$kernel->boot();`:

[[[ code('7d9a8314f9') ]]]

Congrats - you just bootstrapped your Symfony app.

What we really want is access to the service container. To get that, create a new
`private static $container;` property. Then in the method, set that with
`self::$container = $kernel->getContainer();`. Now, as long as we call `boostrapSymfony()`
first, we'll have access to the container. Oh, and update the method to be a
`public static function`: I'm making this all static because it allows us to have
one container across all of our different scenarios. Because remember, each scenario
gets its own context instances.
`private static $container;` property:

[[[ code('b48a7907a9') ]]]

Then in the method, set that with `self::$container = $kernel->getContainer();`:

[[[ code('4e65f60b6c') ]]]

Now, as long as we call `bootstrapSymfony()` first, we'll have access to the container.
Oh, and update the method to be a `public static function`:

[[[ code('ecf15325f4') ]]]

I'm making this all static because it allows us to have one container across all of our
different scenarios. Because remember, each scenario gets its own context instances.

We could call this method manually, but that's not fancy! Remember the hook system?
We used `@BeforeScenario` and `@AfterScenario` before, but there are other hooks,
like `@BeforeSuite`. Let's use that! Behat will call this method one time, even if
we're testing 10 features and 100 scenarios.
like `@BeforeSuite`. Let's use that!

[[[ code('30d74cbe9c') ]]]

Behat will call this method one time, even if we're testing 10 features and 100 scenarios.

## Saving a New User

Inside of the`ThereIsAnAdminUserWithPassword` step definition, let's go to work!
Inside of the`thereIsAnAdminUserWithPassword()` step definition, let's go to work!
I already have a `User` entity setup in the project, so we can say `$user = new User()`.
Then set the username and the "plainPassword": I have a Doctrine listener already
setup that will encode the password automatically. Which is good: it's well-known
that raptors can smell un-encoded passwords...
Then set the username and the "plainPassword":

[[[ code('cc7213d2ab') ]]]

I have a Doctrine listener already setup that will encode the password automatically.
Which is good: it's well-known that raptors can smell un-encoded passwords...

In this app, to make this an "admin" user, we need to give the user `ROLE_ADMIN`:

[[[ code('4c81a8d91e') ]]]

In this app, to make this an "admin" user, we need to give the user `ROLE_ADMIN`.
Now the moment of truth: we need the entity manager. Cool! Grab it with
`$em = self::$container->get('doctrine')` (to get the Doctrine service) and then
`->getManager();`. It's easy from here: `$em->persist($user);` and `$em->flush();`.
`->getManager();`:

[[[ code('fb96fd3486') ]]]

It's easy from here: `$em->persist($user);` and `$em->flush();`:

[[[ code('dae7557c9c') ]]]

And that should do it!

We already have a user called `admin` in the database, so let's test this using `admin2`.
Expand All @@ -76,5 +116,5 @@ Give it a go!
This should not work, since this user isn't in the database yet... unless it's
created by the code we just wrote! Brilliant!

This is huge: we're guaranteeing there's an admin2 user by bootstrapping our app
This is huge: we're guaranteeing there's an `admin2` user by bootstrapping our app
and being dangerous with all of our useful services.

0 comments on commit ac5b5ca

Please sign in to comment.