-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Use Post::addComment method to associate new comment with a post #529
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
What's the reasoning for removing these methods? Thanks! |
@javiereguiluz, we don't use these methods, so, I think we dont need them. |
Probably we have to use these methods in BlogController::commentNewAction() instead of setting the Post entity directly? This way we will update inverse side as well |
The two ways are fine to me, but will there some situation where we need that the inversed side stay updated to get all comments from current post "just after add a new one, before persist/flush the comment", so my opinion is to use |
Still when
|
bd36c5c
to
8017175
Compare
@bocharsky-bw, @yceruto, I think you're right. I've updated the PR. |
@yceruto @bocharsky-bw are you OK with the latest changes made by @voronkovich? They look OK to me. Thanks! |
LGTM, but some tests are failed |
👍 |
I'd like to merge this ... but there's still 1 failing test. Thanks! |
@javiereguiluz, See #554 |
@voronkovich the problem with the introduced changes is that the sorting is not correct anymore as stated in the comment here: https://github.com/symfony/symfony-demo/blob/master/tests/AppBundle/Controller/BlogControllerTest.php#L89 We append the comment at the end of the collection. To make the tests work again we have to remove the post from the EM and fetch it again to let Doctrine handle correct sorting and the first comment will match again. A simple --- a/tests/AppBundle/Controller/BlogControllerTest.php
+++ b/tests/AppBundle/Controller/BlogControllerTest.php
@@ -84,6 +84,8 @@ class BlogControllerTest extends WebTestCase
$client->submit($form);
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
+
+ $client->getContainer()->get('doctrine')->getManager()->clear();
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
// The first one is the most recent comment because of the automatic sorting |
@dmaicher, You're totally right! Thank you very much! |
@javiereguiluz, I've fixed the tests. |
13d51d3
to
5685b8c
Compare
@voronkovich something horrible has happened in this PR and some changes have been lost. I edited 1 file to add some help notes. These are the full contents of the test that I modified, so you can restore it: <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\AppBundle\Controller;
use AppBundle\DataFixtures\FixturesTrait;
use AppBundle\Entity\Post;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
/**
* Functional test for the controllers defined inside BlogController.
*
* See http://symfony.com/doc/current/book/testing.html#functional-tests
*
* Execute the application tests using this command (requires PHPUnit to be installed):
*
* $ cd your-symfony-project/
* $ ./vendor/bin/phpunit
*/
class BlogControllerTest extends WebTestCase
{
use FixturesTrait;
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/en/blog/');
$this->assertCount(
Post::NUM_ITEMS,
$crawler->filter('article.post'),
'The homepage displays the right number of posts.'
);
}
public function testRss()
{
$client = static::createClient();
$crawler = $client->request('GET', '/en/blog/rss.xml');
$this->assertSame(
'text/xml; charset=UTF-8',
$client->getResponse()->headers->get('Content-Type')
);
$this->assertCount(
Post::NUM_ITEMS,
$crawler->filter('item'),
'The xml file displays the right number of posts.'
);
}
/**
* This test changes the database contents by creating a new comment. However,
* thanks to the DAMADoctrineTestBundle and its PHPUnit listener, all changes
* to the database are rolled back when this test completes. This means that
* all the application tests begin with the same database contents.
*/
public function testNewComment()
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'john_user',
'PHP_AUTH_PW' => 'kitten',
]);
/** @var Post $post */
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
$commentContent = $this->getRandomCommentContent();
$commentsCount = $post->getComments()->count();
$crawler = $client->request('GET', '/en/blog/posts/'.$post->getSlug());
$form = $crawler->selectButton('Publish comment')->form([
'comment[content]' => $commentContent,
]);
$client->submit($form);
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
// in order to improve performance, Doctrine doesn't make database calls to
// get the objects already present in memory. This means that this test will fail
// because new comments are added at the end of the list of comments and the
// "@ORM\OrderBy()" defined in the Post entity won't be respected. The solution
// is to call the clear() method on the Entity Manager, which removes the in-memory
// changes and forces loading objects from the database again.
$client->getContainer()->get('doctrine')->getManager()->clear();
// The first one is the most recent comment because of the automatic sorting
// defined in the comments association of the Post entity. This test can only
// work when using the clear() method as explained in the above comment.
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
$comment = $post->getComments()->first();
$this->assertSame($commentsCount + 1, $post->getComments()->count());
$this->assertSame($commentContent, $comment->getContent());
}
} |
@javiereguiluz, I've dropped some changes, so now this PR can be merged. |
@javiereguiluz, AppVeyour says: "AppVeyor was unable to build non-mergeable pull request", but I don't know how to fix it :( |
Thanks! So this means that we no longer need the ->clear() method in the test? |
@javiereguiluz, yes! |
It's merged now. Thanks! |
This test is no longer valid. Ping @javiereguiluz
|
@20uf not sure what you mean? Master builds on travis are passing fine: https://github.com/symfony/symfony-demo/commits/master Maybe your build fail is related to this one as its currently using the dev DB? #556 |
@20uf, try to update dependecies by running: |
Nice thanks @voronkovich . |
No description provided.