Chapters
28 Chapters
|
1:45:06
|
This course is archived!
This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still valid, but the code we use can't be used in a real application.
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
13.
Handling Data in Tests
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still valid, but the code we use can't be used in a real application.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"silex/silex": "~1.0", // v1.3.2
"symfony/twig-bridge": "~2.1", // v2.7.3
"symfony/security": "~2.4", // v2.7.3
"doctrine/dbal": "^2.5.4", // v2.5.4
"monolog/monolog": "~1.7.0", // 1.7.0
"symfony/validator": "~2.4", // v2.7.3
"symfony/expression-language": "~2.4" // v2.7.3
},
"require-dev": {
"behat/mink": "~1.5", // v1.5.0
"behat/mink-goutte-driver": "~1.0.9", // v1.0.9
"behat/mink-selenium2-driver": "~1.1.1", // v1.1.1
"behat/behat": "~2.5", // v2.5.5
"behat/mink-extension": "~1.2.0", // v1.2.0
"phpunit/phpunit": "~5.7.0", // 5.7.27
"guzzle/guzzle": "~3.7" // v3.9.3
}
}
12 Comments
Feature: Programmer
In order to battle projects
As an API client
I need to be able to create programmers and power them up
Background: # features\api\programmer.feature:6
Given the user "weaverryan" exists # ProjectContext::theUserExists()
Scenario: Create a programmer # features\api\programmer.feature:9
Given I have the payload: # ApiFeatureContext::iHaveThePayload()
"""
{
"nickname" : "ObjectOrienter",
"avatarNumber" : 12,
"tagLine" : "I'm from a test!"
}
"""
When I request "POST /api/programmers" # ApiFeatureContext::iRequest()
Then the response status code should be 201 # ApiFeatureContext::iGetAResponse()
And the "Location" header should be "/api/programmers/ObjectOrienter" # ApiFeatureContext::theHeaderShouldBe()
And the "nickname" property should equal "ObjectOrienter" # ApiFeatureContext::thePropertyEquals()
Scenario: GET one programmer # features\api\programmer.feature:23
Given the following programmers exist: # ProjectContext::theFollowingProgrammersExist()
| nickname | avatarNumber |
| UnitTester | 3 |
When I request "GET /api/programmers/UnitTester" # ApiFeatureContext::iRequest()
Then the response status code should be 200 # ApiFeatureContext::iGetAResponse()
Output is "text/html; charset=UTF-8", which is not JSON and is therefore scary. Run the request manually.
Failed asserting that 404 is identical to 200.
And the following properties should exist: # ApiFeatureContext::thePropertiesExist()
"""
nickname
avatarNumber
powerLevel
tagLine
"""
And the "nickname" property should equal "UnitTester" # ApiFeatureContext::thePropertyEquals()
Failure! when making the following request:
GET: http://localhost:8000/api/programmers/UnitTester
Failure! Below is a summary of the HTML response from the server.
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in BaseController.php line 171:
Oh no! This programmer has deserted. we'll send a serch patrol to search him
2 scenarios (1 passed, 1 failed)
12 steps (9 passed, 2 skipped, 1 failed)
0m2.255s
Hey Beniamin!
What is your question here? It's not clear enough what you are trying to say us in the comment.
Cheers!
Hello guys! just to mention something
Your first phrase when you have written the video speech, under the video is:
Let’s run our test a second time:
$ php vendir/bin/behat
but of course the command to the terminal is
$ php vendor/bin/behat
Cheers!
Hey argy_13 ,
Ah, a silly misprint and then its copy/paste in a few more places. Thanks for let us know! I fixed it in: https://github.com/knpunive...
Cheers!
Also a few lines later the same command
Hiya ! How can I target my test database via behat ? Is there a specific behat config file for this ? Or do I reference a specific test environment when running behat ?
GREAT question. Unless you're using the "symfony2" driver via the Symfony2Extension (which I don't use), Behat is making *real* HTTP requests. So, you'll need to create a new app_test.php file that boots Symfony in the "test" environment. Then, go into your config_test.yml file and override doctrine's "dbname" key to be a different database name. Finally, update behat.yml to use this app_test.php in your base URL - like localhost:8000/app_test.php
Does that make sense?
Even GREATER answer. Smooth and elegant solution - I'll try that. Thnxbye :)
NOTE THAT: yml config syntax will be changed.
Not quoting a scalar starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0
That's right Tael! Just surround any % characters with quotes and you'll be just fine now and going forward :)
Am getting this error
UserBundle\Tests\Controller\RegisterControllerTest::testRegister
Failed asserting that false is true.
FAILURES!
Tests: 3, Assertions: 7, Failures: 1.
Remaining deprecation notices (2)
UserBundle\Form\RegisterFormType: The FormTypeInterface::setDefaultOptions() method is deprecated since version 2.7 and will be removed in 3.0. Use configureOptions() instead. This method will be added to the FormTypeInterface with Symfony 3.0: 2x
2x in RegisterControllerTest::testRegister from UserBundle\Tests\Controller
Hi Sarang!
Ah, this is actually pretty cool! When something changes in Symfony (e.g. from version 2.7 to 2.8), instead of just changing it and breaking your code, Symfony deprecates the old functionality. AND, it also tells you about any deprecated code paths when you run your tests so you know about them and can upgrade :). Some things that we show in this tutorial are deprecated now (we're working on new versions currently), and this is a great example. In RegisterFormType, you should rename setDefaultOptions to configureOptions - here's an example (look at the first 2 code blocks): https://github.com/symfony/...
When you fix the deprecation, your tests will pass :). This is actually one of the *killer* features of Symfony. If you want to learn more about it, this is a great presentation about it: http://www.slideshare.net/n...
Cheers!
"Houston: no signs of life"
Start the conversation!