Skip to content

Commit 5a768b0

Browse files
committed
Create functional tests for credentials actions
1 parent 09b9fa8 commit 5a768b0

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Tests\FunctionalTests\Controller\Dashboard;
4+
5+
use CodedMonkey\Dirigent\Controller\Dashboard\DashboardCredentialsController;
6+
use CodedMonkey\Dirigent\Doctrine\Entity\Credentials;
7+
use CodedMonkey\Dirigent\Doctrine\Entity\CredentialsType;
8+
use CodedMonkey\Dirigent\Tests\Helper\MockEntityFactoryTrait;
9+
use CodedMonkey\Dirigent\Tests\Helper\WebTestCaseTrait;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
12+
use Symfony\Component\HttpFoundation\Response;
13+
14+
#[CoversClass(DashboardCredentialsController::class)]
15+
class DashboardCredentialsControllerTest extends WebTestCase
16+
{
17+
use MockEntityFactoryTrait;
18+
use WebTestCaseTrait;
19+
20+
public function testCreate(): void
21+
{
22+
$client = static::createClient();
23+
$this->loginUser('admin');
24+
25+
$client->request('GET', '/credentials/new');
26+
27+
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
28+
29+
$client->submitForm('Create', [
30+
'Credentials[name]' => 'Test credentials',
31+
'Credentials[type]' => CredentialsType::HttpBasic->value,
32+
'Credentials[username]' => 'testuser',
33+
'Credentials[password]' => 'testpassword',
34+
]);
35+
36+
$this->assertResponseStatusCodeSame(Response::HTTP_FOUND);
37+
38+
$credentials = $this->findEntity(Credentials::class, ['name' => 'Test credentials']);
39+
40+
self::assertNotNull($credentials, 'A credentials entry was created.');
41+
self::assertSame(CredentialsType::HttpBasic, $credentials->getType());
42+
self::assertSame('testuser', $credentials->getUsername());
43+
}
44+
45+
public function testEdit(): void
46+
{
47+
$client = static::createClient();
48+
$this->loginUser('admin');
49+
50+
$credentials = new Credentials();
51+
$credentials->setName('Original name');
52+
$credentials->setType(CredentialsType::HttpBasic);
53+
$credentials->setUsername('originaluser');
54+
$credentials->setPassword('originalpassword');
55+
56+
$this->persistEntities($credentials);
57+
58+
$credentialsId = $credentials->getId();
59+
60+
$client->request('GET', "/credentials/{$credentialsId}/edit");
61+
62+
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
63+
64+
$client->submitForm('Save changes', [
65+
'Credentials[name]' => 'Updated name',
66+
'Credentials[type]' => CredentialsType::HttpBasic->value,
67+
'Credentials[username]' => 'updateduser',
68+
'Credentials[password]' => 'updatedpassword',
69+
]);
70+
71+
$this->assertResponseStatusCodeSame(Response::HTTP_FOUND);
72+
73+
$this->clearEntities();
74+
$credentials = $this->findEntity(Credentials::class, $credentialsId);
75+
76+
self::assertNotNull($credentials);
77+
self::assertSame('Updated name', $credentials->getName());
78+
self::assertSame('updateduser', $credentials->getUsername());
79+
}
80+
}

tests/Helper/MockEntityFactoryTrait.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ protected function createMockVersion(Package $package, string $versionName = '1.
4949
return $version;
5050
}
5151

52+
/**
53+
* Find a single entity by its ID or an array of criteria.
54+
*
55+
* @template T of object
56+
*
57+
* @param class-string<T> $className
58+
*
59+
* @return T|null
60+
*/
61+
protected function findEntity(string $className, array|int $criteria): ?object
62+
{
63+
if (is_array($criteria)) {
64+
return $this->getService(EntityManagerInterface::class)->getRepository($className)->findOneBy($criteria);
65+
}
66+
67+
return $this->getService(EntityManagerInterface::class)->find($className, $criteria);
68+
}
69+
5270
/**
5371
* Persist and flush all given entities.
5472
*
@@ -67,8 +85,6 @@ protected function persistEntities(...$entities): void
6785

6886
protected function clearEntities(): void
6987
{
70-
$entityManager = $this->getService(EntityManagerInterface::class);
71-
72-
$entityManager->clear();
88+
$this->getService(EntityManagerInterface::class)->clear();
7389
}
7490
}

0 commit comments

Comments
 (0)