Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn committed Sep 21, 2023
1 parent df786ce commit e7d667c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 102 deletions.
62 changes: 30 additions & 32 deletions Tests/Functional/Controller/CommentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sulu\Bundle\CommentBundle\Entity\Thread;
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;

class CommentControllerTest extends SuluTestCase
{
Expand All @@ -26,8 +27,14 @@ class CommentControllerTest extends SuluTestCase
*/
private $entityManager;

/**
* @var KernelBrowser
*/
private $client;

protected function setUp(): void
{
$this->client = $this->createAuthenticatedClient();
$this->entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');

$this->purgeDatabase();
Expand All @@ -40,11 +47,10 @@ public function testGet()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/comments/' . $comment->getId());
$this->client->request('GET', '/api/comments/' . $comment->getId());

$this->assertHttpStatusCode(200, $client->getResponse());
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$data = json_decode($this->client->getResponse()->getContent(), true);

$this->assertEquals($comment->getId(), $data['id']);
$this->assertEquals($comment->getMessage(), $data['message']);
Expand All @@ -59,11 +65,10 @@ public function testCGet()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/comments');
$this->client->request('GET', '/api/comments');

$this->assertHttpStatusCode(200, $client->getResponse());
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$data = json_decode($this->client->getResponse()->getContent(), true);

$this->assertCount(3, $data['_embedded']['comments']);
}
Expand All @@ -78,11 +83,10 @@ public function testCGetFilter()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/comments?state=' . CommentInterface::STATE_UNPUBLISHED);
$this->client->request('GET', '/api/comments?state=' . CommentInterface::STATE_UNPUBLISHED);

$this->assertHttpStatusCode(200, $client->getResponse());
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$data = json_decode($this->client->getResponse()->getContent(), true);

$this->assertCount(1, $data['_embedded']['comments']);
}
Expand All @@ -99,11 +103,10 @@ public function testCGetTypeFilter()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/comments?threadType=test-1,test-2');
$this->client->request('GET', '/api/comments?threadType=test-1,test-2');

$this->assertHttpStatusCode(200, $client->getResponse());
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$data = json_decode($this->client->getResponse()->getContent(), true);

$this->assertCount(2, $data['_embedded']['comments']);
}
Expand All @@ -115,11 +118,10 @@ public function testPut()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('PUT', '/api/comments/' . $comment->getId(), ['message' => 'My new Message']);
$this->client->request('PUT', '/api/comments/' . $comment->getId(), ['message' => 'My new Message']);

$this->assertHttpStatusCode(200, $client->getResponse());
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$data = json_decode($this->client->getResponse()->getContent(), true);

$this->assertEquals($comment->getId(), $data['id']);
$this->assertEquals('My new Message', $data['message']);
Expand All @@ -132,10 +134,9 @@ public function testDelete()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('DELETE', '/api/comments/' . $comment->getId());
$this->client->request('DELETE', '/api/comments/' . $comment->getId());

$this->assertHttpStatusCode(204, $client->getResponse());
$this->assertHttpStatusCode(204, $this->client->getResponse());

$this->assertNull($this->entityManager->find(CommentInterface::class, $comment->getId()));
}
Expand All @@ -151,8 +152,7 @@ public function testCDelete()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request(
$this->client->request(
'DELETE',
'/api/comments?ids=' . implode(
',',
Expand All @@ -165,7 +165,7 @@ function(CommentInterface $comment) {
)
);

$this->assertHttpStatusCode(204, $client->getResponse());
$this->assertHttpStatusCode(204, $this->client->getResponse());

foreach ([$comments[0], $comments[1]] as $comment) {
$this->assertNull($this->entityManager->find(CommentInterface::class, $comment->getId()));
Expand All @@ -183,9 +183,8 @@ public function testPublish()

$this->assertFalse($comment->isPublished());

$client = $this->createAuthenticatedClient();
$client->request('POST', '/api/comments/' . $comment->getId() . '?action=publish');
$this->assertHttpStatusCode(200, $client->getResponse());
$this->client->request('POST', '/api/comments/' . $comment->getId() . '?action=publish');
$this->assertHttpStatusCode(200, $this->client->getResponse());

$result = $this->entityManager->find(CommentInterface::class, $comment->getId());
$this->assertTrue($result->isPublished());
Expand All @@ -200,9 +199,8 @@ public function testUnpublish()

$this->assertTrue($comment->isPublished());

$client = $this->createAuthenticatedClient();
$client->request('POST', '/api/comments/' . $comment->getId() . '?action=unpublish');
$this->assertHttpStatusCode(200, $client->getResponse());
$this->client->request('POST', '/api/comments/' . $comment->getId() . '?action=unpublish');
$this->assertHttpStatusCode(200, $this->client->getResponse());

$result = $this->entityManager->find(CommentInterface::class, $comment->getId());
$this->assertFalse($result->isPublished());
Expand Down
52 changes: 26 additions & 26 deletions Tests/Functional/Controller/ThreadControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sulu\Bundle\CommentBundle\Entity\Thread;
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;

class ThreadControllerTest extends SuluTestCase
{
Expand All @@ -24,8 +25,14 @@ class ThreadControllerTest extends SuluTestCase
*/
private $entityManager;

/**
* @var KernelBrowser
*/
private $client;

protected function setUp(): void
{
$this->client = $this->createAuthenticatedClient();
$this->entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');

$this->purgeDatabase();
Expand All @@ -37,11 +44,10 @@ public function testGet()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/threads/' . $thread->getId());
$this->client->request('GET', '/api/threads/' . $thread->getId());

$this->assertHttpStatusCode(200, $client->getResponse());
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$data = json_decode($this->client->getResponse()->getContent(), true);

$this->assertEquals($thread->getId(), $data['id']);
$this->assertEquals($thread->getTitle(), $data['title']);
Expand All @@ -56,11 +62,10 @@ public function testCGetFilter()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/threads?type=page');
$this->client->request('GET', '/api/threads?type=page');

$this->assertHttpStatusCode(200, $client->getResponse());
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$data = json_decode($this->client->getResponse()->getContent(), true);

$this->assertCount(2, $data['_embedded']['threads']);
}
Expand All @@ -71,11 +76,10 @@ public function testPut()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('PUT', '/api/threads/' . $thread->getId(), ['title' => 'My new Title']);
$this->client->request('PUT', '/api/threads/' . $thread->getId(), ['title' => 'My new Title']);

$this->assertHttpStatusCode(200, $client->getResponse());
$data = json_decode($client->getResponse()->getContent(), true);
$this->assertHttpStatusCode(200, $this->client->getResponse());
$data = json_decode($this->client->getResponse()->getContent(), true);

$this->assertEquals($thread->getId(), $data['id']);
$this->assertEquals('My new Title', $data['title']);
Expand All @@ -87,10 +91,9 @@ public function testDelete()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('DELETE', '/api/threads/' . $thread->getId());
$this->client->request('DELETE', '/api/threads/' . $thread->getId());

$this->assertHttpStatusCode(204, $client->getResponse());
$this->assertHttpStatusCode(204, $this->client->getResponse());

$this->assertNull($this->entityManager->find(ThreadInterface::class, $thread->getId()));
}
Expand All @@ -105,8 +108,7 @@ public function testCDelete()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request(
$this->client->request(
'DELETE',
'/api/threads?ids=' . implode(
',',
Expand All @@ -119,7 +121,7 @@ function(ThreadInterface $thread) {
)
);

$this->assertHttpStatusCode(204, $client->getResponse());
$this->assertHttpStatusCode(204, $this->client->getResponse());

foreach ([$threads[0], $threads[1]] as $comment) {
$this->assertNull($this->entityManager->find(ThreadInterface::class, $comment->getId()));
Expand All @@ -139,12 +141,11 @@ public function testCGet()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/threads?fields=id,title');
$this->client->request('GET', '/api/threads?fields=id,title');

$this->assertHttpStatusCode(200, $client->getResponse());
$this->assertHttpStatusCode(200, $this->client->getResponse());

$result = json_decode($client->getResponse()->getContent(), true);
$result = json_decode($this->client->getResponse()->getContent(), true);
$result = $result['_embedded']['threads'];
for ($i = 0, $length = count($threads); $i < $length; ++$i) {
$this->assertEquals($threads[$i]->getId(), $result[$i]['id']);
Expand All @@ -163,12 +164,11 @@ public function testCGetTypes()
$this->entityManager->flush();
$this->entityManager->clear();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/threads?fields=id,title&types=Test1,Test3');
$this->client->request('GET', '/api/threads?fields=id,title&types=Test1,Test3');

$this->assertHttpStatusCode(200, $client->getResponse());
$this->assertHttpStatusCode(200, $this->client->getResponse());

$result = json_decode($client->getResponse()->getContent(), true);
$result = json_decode($this->client->getResponse()->getContent(), true);
$result = $result['_embedded']['threads'];

$expected = [$threads[0], $threads[2]];
Expand Down
Loading

0 comments on commit e7d667c

Please sign in to comment.