-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add more tests to blog post #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -184,9 +183,9 @@ private function getPhrases() | |||
]; | |||
} | |||
|
|||
private function getRandomPostTiles() | |||
public static function getRandomPostTiles() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yceruto, there is a typo in the method's name here. Should be getRandomPostTitles
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Fixed!
]); | ||
|
||
/** @var Post $post */ | ||
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yceruto, you could use an EntityManager#find
method:
$post = $client->getContainer()->get('doctrine')->find(Post::class, 1);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry your suggestion does not works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yceruto, Sorry for confusing you! I mixed up the doctrine
service and an entity manager. :(
'PHP_AUTH_USER' => 'john_user', | ||
'PHP_AUTH_PW' => 'kitten', | ||
]); | ||
|
||
$client->request($httpMethod, $url); | ||
$this->assertSame($statusCode, $client->getResponse()->getStatusCode()); | ||
$this->assertTrue($client->getResponse()->isForbidden()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You made it less strict, but I don't think it makes sense in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, isForbidden()
only checks the 403
status code ... so the code it's technically equivalent...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in this case it's technically equivalent.
@@ -70,7 +70,7 @@ public function testAdminBackendHomePage() | |||
$client = $this->getAdminClient(); | |||
|
|||
$crawler = $client->request('GET', '/en/admin/post/'); | |||
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); | |||
$this->assertTrue($client->getResponse()->isSuccessful()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same here, I think we want to know that status code is exactly 200 OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... however, here the isSuccessful()
method is too liberal:
public function isSuccessful()
{
return $this->statusCode >= 200 && $this->statusCode < 300;
}
I agree with @bocharsky-bw and I think we should revert this change and go back to Response::HTTP_OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 reverting....
@@ -108,7 +107,7 @@ private function getRandomTags($numTags = 0) | |||
return $tags; | |||
} | |||
|
|||
private function getPostContent() | |||
public static function getPostContent() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👎 I'd rather suggest to use PHP Traits instead of making private
methods public static
or do simple inheritance. We should always avoid public methods which we have to care about later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for Trait
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); | ||
$this->assertNull($post); | ||
/** @var Post $post */ | ||
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(31); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 31
id should never fail ... but maybe a findByTitle($postTitle)
would make the code more robust?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 sure
'PHP_AUTH_USER' => 'john_user', | ||
'PHP_AUTH_PW' => 'kitten', | ||
]); | ||
|
||
$client->request($httpMethod, $url); | ||
$this->assertSame($statusCode, $client->getResponse()->getStatusCode()); | ||
$this->assertTrue($client->getResponse()->isForbidden()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, isForbidden()
only checks the 403
status code ... so the code it's technically equivalent...
@@ -70,7 +70,7 @@ public function testAdminBackendHomePage() | |||
$client = $this->getAdminClient(); | |||
|
|||
$crawler = $client->request('GET', '/en/admin/post/'); | |||
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); | |||
$this->assertTrue($client->getResponse()->isSuccessful()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... however, here the isSuccessful()
method is too liberal:
public function isSuccessful()
{
return $this->statusCode >= 200 && $this->statusCode < 300;
}
I agree with @bocharsky-bw and I think we should revert this change and go back to Response::HTTP_OK
@javiereguiluz @bocharsky-bw done!
Status: Need Reviews :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps Javier could help me with a nice description here :)
tests/ControllerTestCase.php
Outdated
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class ControllerTestCase extends WebTestCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing description
tests/FixturesTrait.php
Outdated
|
||
namespace Tests; | ||
|
||
trait FixturesTrait |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing description
I'm not that sure about I'm not asking to removing this (yet) ... but let's think a bit about it. Thanks! |
This I think it's good to show to newcomers good practices related to how to simplify their tests too. For newcomers of "Symfony framework" it should be easy to understand I think, anyway a nice class description could clarify possible misinterpretations, EDIT: but I'm fine with removing this class, if you prefer :) (or part of it) |
Maybe I could remove the response assertions and leave only the client creation methods, what do you think? |
Last changes:
|
Rebased (#502 conflicts resolved) |
@yceruto thanks for all the changes and for rebasing, etc. There are a lot of things that I like in this PR, so I'm going to merge it now. However, there are some things that I don't like because it's not a practice promoted by Symfony (although your proposal works perfectly and it's technically correct). So, I'm going to create a PR soon after to revert some changes. Thanks! |
This PR was merged into the master branch. Discussion ---------- Add more tests to blog post * Testing new blog post * Testing show blog post (admin) * Testing add a comment to post * Changing some `PostFixtures` methods to static for reusability * Simplify and reordering some tests Commits ------- 2700136 Add more tests
This PR was merged into the master branch. Discussion ---------- Removed ControllerTestTrait In #481, @yceruto introduced some nice features. I like them all ... but one of them is not compatible with the way Symfony promotes to do things. The `ControllerTestTrait` is a PHP trait with some helper methods to reduce the boilerplate code in tests. Even if the solution is technically correct, we don't use it in the official Symfony docs. This can create confusion for new developers studying this app and reading the docs. I propose to revert this feature in this PR ... and later we can discuss about ways to improve this without using traits. Thanks! Commits ------- 17f9a10 Removed ControllerTestTrait
PostFixtures
methods to static for reusability