Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
DATABASE_URL="mysql://root:root@db:3306/app?serverVersion=mariadb-10.10.2&charset=utf8mb4"
SYMFONY_DEPRECATIONS_HELPER=disabled
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@
}
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4",
"doctrine/doctrine-fixtures-bundle": "^3.4.2",
"fakerphp/faker": "^1.21",
"friendsofphp/php-cs-fixer": "^3.13",
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3.14.2",
"phpunit/phpunit": "^9.6.4",
"symfony/browser-kit": "6.2.*",
"symfony/css-selector": "6.2.*",
"symfony/debug-bundle": "6.2.*",
"symfony/maker-bundle": "^1.0",
"symfony/phpunit-bridge": "^6.2",
"symfony/maker-bundle": "^1.48",
"symfony/phpunit-bridge": "^6.2.7",
"symfony/stopwatch": "6.2.*",
"symfony/web-profiler-bundle": "6.2.*"
}
Expand Down
859 changes: 430 additions & 429 deletions composer.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions config/services_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
public: true

# If you need to access services in a test, create an alias
# and then fetch that alias from the container. As a convention,
# aliases are prefixed with test.

test.App\Manager\LogManager: '@App\Manager\LogManager'
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ services:
image: dunglas/mercure
restart: unless-stopped
environment:
# Uncomment the following line to disable HTTPS
SERVER_NAME: ":3000"
MERCURE_PUBLISHER_JWT_KEY: "${MERCURE_JWT_SECRET:?err}"
MERCURE_SUBSCRIBER_JWT_KEY: "${MERCURE_JWT_SECRET:?err}"
Expand Down
1 change: 1 addition & 0 deletions src/Entity/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Profile

#[ORM\OneToOne(inversedBy: 'profile', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: false)]
#[Assert\NotNull]
#[Assert\Valid]
private ?User $user = null;

Expand Down
1 change: 1 addition & 0 deletions src/Entity/Tech.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Tech

#[ORM\OneToOne(inversedBy: 'tech', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: false)]
#[Assert\NotNull]
#[Assert\Valid]
private ?Request $request = null;

Expand Down
9 changes: 8 additions & 1 deletion src/Manager/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

final class LogManager
{
public const SUCCESS = 0;
public const FAILURE = 1;

public function __construct(private readonly LoggerInterface $logger)
{
}

public function logException(\Exception $exception, string $level = LogLevel::CRITICAL): void
public function logException(\Exception $exception, string $level = LogLevel::CRITICAL): int
{
$exceptionTrace = $exception->getTrace();

Expand All @@ -20,13 +23,17 @@ public function logException(\Exception $exception, string $level = LogLevel::CR
'exception' => $exception->getMessage(),
'trace' => $exceptionTrace,
]);

return self::SUCCESS;
} catch (\Exception $e) {
$this->logger->critical(sprintf('Log using App\Manager\LogUtilsTrait::log with level "%s" failed', $level), [
'exception' => $e->getMessage(),
'trace' => $e->getTrace(),
'originalException' => $exception->getMessage(),
'originalTrace' => $exceptionTrace,
]);

return self::FAILURE;
}
}
}
68 changes: 68 additions & 0 deletions tests/Entity/TechTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Tests\Entity;

use App\Entity\Category;
use App\Entity\Request;
use App\Entity\Tech;
use App\Enum\TechTypeEnum;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class TechTest extends KernelTestCase
{
private ValidatorInterface $validator;

protected function setUp(): void
{
self::bootKernel();
$this->validator = self::getContainer()->get(ValidatorInterface::class);
}

public static function techProvider(): array
{
return [
'Valid' => [0, (new Tech())
->setName('Tech')
->setDescription('Tech desc')
->addCategory(new Category())
->setRequest(new Request()),
],
'Invalid dependsOn (null but is required for type Library)' => [1, (new Tech())
->setName('Tech')
->setDescription('Tech desc')
->setType(TechTypeEnum::Library)
->addCategory(new Category())
->setRequest(new Request()),
],
'Invalid GitHub URL' => [1, (new Tech())
->setName('Tech')
->setDescription('Tech desc')
->setLinks([Tech::LINK_GITHUB => 'https://not-github.com'])
->addCategory(new Category())
->setRequest(new Request()),
],
];
}

/**
* @dataProvider techProvider
*/
public function testTechs(int $numErrors, Tech $tech): void
{
$errors = $this->validator->validate($tech);
$count = \count($errors);

$this->assertEquals(
$numErrors,
$count,
sprintf(
'Expected %s error(s) but got %s error(s) instead. Errors : %s',
$numErrors,
$count,
\PHP_EOL.implode(\PHP_EOL, array_map(static fn (ConstraintViolation $error): string => $error->getMessage(), [...$errors]))
)
);
}
}
66 changes: 66 additions & 0 deletions tests/Entity/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Tests\Entity;

use App\Entity\Profile;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class UserTest extends KernelTestCase
{
private ValidatorInterface $validator;

protected function setUp(): void
{
self::bootKernel();
$this->validator = self::getContainer()->get(ValidatorInterface::class);
}

public static function userProvider(): array
{
return [
'Valid' => [0, (new User())
->setEmail('moderator@stack-up.tech')
->setPlainPassword('ERGZ$vdbr4320')
->setProfile((new Profile())->setUsername('TestModerator')),
],
'Invalid email and password' => [2, (new User())
->setEmail('test123')
->setPlainPassword('test123')
->setProfile((new Profile())->setUsername('Test123')),
],
'Invalid profile (username contains invalid characters)' => [1, (new User())
->setEmail('user@stack-up.tech')
->setPlainPassword('ERGZ$vdbr4320')
->setProfile((new Profile())->setUsername('Test$$$')),
],
'Invalid profile (username is blank)' => [1, (new User())
->setEmail('user@stack-up.tech')
->setPlainPassword('ERGZ$vdbr4320')
->setProfile((new Profile())->setUsername(null)),
],
];
}

/**
* @dataProvider userProvider
*/
public function testUsers(int $numErrors, User $user): void
{
$errors = $this->validator->validate($user);
$count = \count($errors);

$this->assertEquals(
$numErrors,
$count,
sprintf(
'Expected %s error(s) but got %s error(s) instead. Errors : %s',
$numErrors,
$count,
\PHP_EOL.implode(\PHP_EOL, array_map(static fn (ConstraintViolation $error): string => $error->getMessage(), [...$errors]))
)
);
}
}
43 changes: 43 additions & 0 deletions tests/Manager/LogManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Tests\Manager;

use App\Manager\LogManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class LogManagerTest extends KernelTestCase
{
private LogManager $logManager;

protected function setUp(): void
{
self::bootKernel();
$this->logManager = self::getContainer()->get('test.'.LogManager::class);
}

public function testCriticalLog(): void
{
$this->assertEquals(LogManager::SUCCESS, $this->logManager->logException(new \Exception('Test exception.')));
}

public function testLogCallable(): void
{
/** @var LoggerInterface|MockObject */
$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->once())
->method('log')
->with(
$this->equalTo(LogLevel::EMERGENCY),
$this->stringEndsWith(__FUNCTION__),
$this->logicalAnd($this->arrayHasKey('exception'), $this->arrayHasKey('trace'))
)
;
$logManager = new LogManager($logger);

$logManager->logException(new \Exception('Test exception.'), LogLevel::EMERGENCY);
}
}