-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
184 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Tests\Repository; | ||
|
||
use App\Entity\Post; | ||
use App\Kernel; | ||
use App\Repository\PostRepository; | ||
use App\Repository\TagRepository; | ||
use Symfony\Bridge\PhpUnit\ClockMock; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
abstract class RepositoryTestBase extends KernelTestCase | ||
{ | ||
|
||
/** | ||
* @var \Doctrine\ORM\EntityManager | ||
*/ | ||
protected $entityManager; | ||
|
||
protected static function getKernelClass() | ||
{ | ||
return Kernel::class; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
// Set a fake clock time of 2020-11-15 07:36:41 and register all our classes that use the time() function. | ||
ClockMock::withClockMock(1605425801); | ||
ClockMock::register(self::class); | ||
ClockMock::register(PostRepository::class); | ||
ClockMock::register(TagRepository::class); | ||
ClockMock::register(Post::class); | ||
|
||
$kernel = self::bootKernel(); | ||
$this->entityManager = $kernel->getContainer() | ||
->get('doctrine') | ||
->getManager(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Tests\Repository; | ||
|
||
use App\Entity\Post; | ||
use App\Entity\Tag; | ||
use App\Entity\User; | ||
use App\Entity\UserGroup; | ||
use App\Repository\PostRepository; | ||
use App\Repository\TagRepository; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class TagRepositoryTest extends RepositoryTestBase | ||
{ | ||
|
||
public function testGetPostList() | ||
{ | ||
/** @var TagRepository $tagRepo */ | ||
$tagRepo = $this->entityManager->getRepository(Tag::class); | ||
/** @var PostRepository $postRepo */ | ||
$postRepo = $this->entityManager->getRepository(Post::class); | ||
|
||
$tag = new Tag(); | ||
$tag->setTitle('Tag 1'); | ||
$this->entityManager->persist($tag); | ||
$privateGroup = new UserGroup(); | ||
$privateGroup->setName('Private'); | ||
$this->entityManager->persist($privateGroup); | ||
$this->entityManager->flush(); | ||
|
||
// Create two posts tagged the same, one of which is private. | ||
$publicPost = new Post(); | ||
$publicPostData = ['tags' => 'Tag 1', 'view_group' => UserGroup::PUBLIC, 'author' => 1]; | ||
$postRepo->saveFromRequest($publicPost, new Request([], $publicPostData)); | ||
$privatePost = new Post(); | ||
$privatePostData = ['tags' => 'Tag 1', 'view_group' => $privateGroup->getId(), 'author' => 1]; | ||
$postRepo->saveFromRequest($privatePost, new Request([], $privatePostData)); | ||
|
||
// Make sure the user is only in the public group. | ||
$user = new User(); | ||
|
||
// Check that only one post is returned. | ||
$this->assertSame(1, $tagRepo->countPosts($tag, $user)); | ||
$posts = $tagRepo->findPosts($tag, $user); | ||
$this->assertGreaterThan(0, $posts[0]->getId()); | ||
} | ||
} |