Skip to content

Commit

Permalink
tests: Add spec tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 14, 2021
1 parent 01c115a commit b24b06c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 19 deletions.
1 change: 1 addition & 0 deletions fixtures/user-invalid.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foobar!
1 change: 1 addition & 0 deletions fixtures/user-memory-limit.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
memory_limit=1234M
102 changes: 102 additions & 0 deletions spec/EcPhp/PhpDirectiveBundle/EventListener/SetPhpDirectiveSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ecphp
*/

declare(strict_types=1);

namespace spec\EcPhp\PhpDirectiveBundle\EventListener;

use InvalidArgumentException;
use PhpSpec\Exception\Example\FailureException;
use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use function dirname;

final class SetPhpDirectiveSpec extends ObjectBehavior
{
public function it_set_php_directives(ParameterBagInterface $parameterBag, LoggerInterface $logger, KernelInterface $kernel, RequestEvent $event): void
{
$kernel
->getProjectDir()
->willReturn(dirname(__DIR__, 4) . '/fixtures');

$parameterBag
->get('php_directive')
->willReturn(
[
'user_ini_file' => 'user-memory-limit.ini',
]
);

$this->beConstructedWith($parameterBag, $logger, $kernel);

$memoryLimit = ini_get('memory_limit');

$this
->__invoke($event);

if ('1234M' !== $size = ini_get('memory_limit')) {
throw new FailureException('Invalid size: ' . $size);
}

ini_set('memory_limit', $memoryLimit);
}

public function it_throws_an_exception_is_the_ini_file_is_not_found_or_readable(ParameterBagInterface $parameterBag, LoggerInterface $logger, KernelInterface $kernel, RequestEvent $event): void
{
$kernel
->getProjectDir()
->willReturn(dirname(__DIR__, 4) . '/fixtures/not-existing-path');

$parameterBag
->get('php_directive')
->willReturn(
[
'user_ini_file' => 'user.ini',
]
);

$this->beConstructedWith($parameterBag, $logger, $kernel);

$this
->shouldThrow(InvalidArgumentException::class)
->during('__invoke', [$event]);
}

public function it_throws_an_exception_is_the_ini_file_is_not_parseable(): void
{
throw new SkippingException(
'Unable to find a test where parse_ini_file() returns false.'
);
}

public function it_throws_an_exception_is_the_ini_file_is_valid(ParameterBagInterface $parameterBag, LoggerInterface $logger, KernelInterface $kernel, RequestEvent $event): void
{
$kernel
->getProjectDir()
->willReturn(dirname(__DIR__, 4) . '/fixtures');

$parameterBag
->get('php_directive')
->willReturn(
[
'user_ini_file' => 'user-invalid.ini',
]
);

$this->beConstructedWith($parameterBag, $logger, $kernel);

$this
->shouldThrow(InvalidArgumentException::class)
->during('__invoke', [$event]);
}
}
45 changes: 26 additions & 19 deletions src/EventListener/SetPhpDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace EcPhp\PhpDirectiveBundle\EventListener;

use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Throwable;

use const DIRECTORY_SEPARATOR;

Expand All @@ -35,31 +37,36 @@ public function __construct(ParameterBagInterface $parameterBag, LoggerInterface

public function __invoke(RequestEvent $event): void
{
$realIniFile = realpath(
$this->kernel->getProjectDir() .
DIRECTORY_SEPARATOR .
$this->parameterBag->get('php_directive')['user_ini_file']
);
$iniFile = $this->kernel->getProjectDir() .
DIRECTORY_SEPARATOR .
$this->parameterBag->get('php_directive')['user_ini_file'];

$realIniFile = realpath($iniFile);

if (false === $realIniFile) {
$this->logger->error('Unable to find ini file.');
throw new InvalidArgumentException('Unable to find ini file: ' . $iniFile);
}

return;
try {
$parsedIniFile = parse_ini_file($realIniFile);
} catch (Throwable $e) {
throw new InvalidArgumentException(
sprintf(
'Unable to parse ini file at %s.',
$realIniFile
)
);
}

$parsedIniFile = parse_ini_file($realIniFile);
var_dump($parsedIniFile);

if (false === $parsedIniFile) {
$this
->logger
->error(
sprintf(
'Unable to parse ini file at %s.',
$realIniFile
)
);

return;
throw new InvalidArgumentException(
sprintf(
'Unable to parse ini file at %s.',
$realIniFile
)
);
}

foreach ($parsedIniFile as $key => $value) {
Expand All @@ -70,7 +77,7 @@ public function __invoke(RequestEvent $event): void
->logger
->error(
sprintf(
'Unable to update PHP property %s.',
'Unable to update PHP property "%s", skipping.',
$key
)
);
Expand Down

0 comments on commit b24b06c

Please sign in to comment.