-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove resetDatabase trait from tests
- Loading branch information
1 parent
d182205
commit 77315fc
Showing
15 changed files
with
311 additions
and
269 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
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,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Starfleet Project. | ||
* | ||
* (c) Starfleet <[email protected]> | ||
* | ||
* For the full copyright and license information, | ||
* please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests; | ||
|
||
use App\Entity\User; | ||
use App\Factory\UserFactory; | ||
use Doctrine\Common\DataFixtures\Purger\ORMPurger; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Zenstruck\Foundry\Test\Factories; | ||
|
||
abstract class AbstractStarfleetTest extends WebTestCase | ||
{ | ||
use Factories; | ||
|
||
private ?KernelBrowser $client = null; | ||
|
||
protected function setUp(): void | ||
{ | ||
$purger = new ORMPurger($this->getEntityManager()); | ||
$purger->purge(); | ||
$this->generateData(); | ||
} | ||
|
||
protected function getClient(?User $user = null) | ||
{ | ||
if (!$this->client) { | ||
$user = $user ?: $this->getTestUser(); | ||
$this->ensureKernelShutdown(); | ||
$this->client = $this->createClient(); | ||
$this->client->followRedirects(); | ||
$this->client->loginUser($user); | ||
} | ||
|
||
return $this->client; | ||
} | ||
|
||
protected function getTestUser() | ||
{ | ||
$user = UserFactory::repository()->findOneBy([ | ||
'email' => '[email protected]', | ||
]); | ||
|
||
if (!$user) { | ||
$user = UserFactory::createOne([ | ||
'name' => 'Starfleet User', | ||
'email' => '[email protected]', | ||
'password' => 'password', | ||
]); | ||
} | ||
|
||
return $user->object(); | ||
} | ||
|
||
protected function getAdminUser() | ||
{ | ||
$user = UserFactory::repository()->findOneBy([ | ||
'email' => '[email protected]', | ||
]); | ||
|
||
if (!$user) { | ||
$user = UserFactory::createOne([ | ||
'name' => 'Starfleet Admin', | ||
'email' => '[email protected]', | ||
'password' => 'password', | ||
'roles' => ['ROLE_ADMIN'], | ||
]); | ||
} | ||
|
||
return $user->object(); | ||
} | ||
|
||
/** | ||
* This method must create all the entities your test will need. | ||
*/ | ||
abstract protected function generateData(); | ||
|
||
private function getEntityManager() | ||
{ | ||
return $this->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
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 |
---|---|---|
|
@@ -11,54 +11,32 @@ | |
|
||
namespace App\Tests\Controller\Front; | ||
|
||
use App\Factory\UserFactory; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use App\Tests\AbstractStarfleetTest; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
|
||
class SecurityControllerTest extends WebTestCase | ||
class SecurityControllerTest extends AbstractStarfleetTest | ||
{ | ||
use Factories; | ||
use ResetDatabase; | ||
|
||
public function testAdminCanAccessToAdmin() | ||
{ | ||
$userProxy = UserFactory::createOne([ | ||
'email' => '[email protected]', | ||
'roles' => ['ROLE_ADMIN'], | ||
'name' => 'Admin', | ||
'password' => 'password', | ||
]); | ||
|
||
$this->ensureKernelShutdown(); | ||
$client = $this->createClient(); | ||
$client->loginUser($userProxy->object()); | ||
$client->followRedirects(); | ||
|
||
$client = $this->getClient($this->getAdminUser()); | ||
$client->request('GET', '/admin/'); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertSelectorTextContains('span.user-name', 'Admin'); | ||
$this->assertSelectorTextContains('span.user-name', 'Starfleet Admin'); | ||
} | ||
|
||
public function testUserCannotAccessToAdmin() | ||
{ | ||
$userProxy = UserFactory::createOne([ | ||
'roles' => ['ROLE_USER'], | ||
]); | ||
|
||
$this->ensureKernelShutdown(); | ||
$client = $this->createClient(); | ||
$client->followRedirects(); | ||
$client->loginUser($userProxy->object()); | ||
|
||
$client->request('GET', '/admin/'); | ||
$this->getClient()->request('GET', '/admin/'); | ||
|
||
$this->assertResponseStatusCodeSame(403); | ||
} | ||
|
||
public function testNotAuthenticatedUserCanNotAccessToAdmin() | ||
{ | ||
$this->ensureKernelShutdown(); | ||
$client = $this->createClient(); | ||
$client->followRedirects(); | ||
|
||
|
@@ -67,4 +45,9 @@ public function testNotAuthenticatedUserCanNotAccessToAdmin() | |
$this->assertResponseIsSuccessful(); | ||
$this->assertRouteSame('login'); | ||
} | ||
|
||
protected function generateData() | ||
{ | ||
// No specific data needed for these tests. | ||
} | ||
} |
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
Oops, something went wrong.