Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brainexe committed Jun 5, 2016
1 parent 38f1eaf commit fd6c7c5
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 65 deletions.
3 changes: 2 additions & 1 deletion Tests/BrainExe/Core/Application/AppKernelTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Tests\BrainExe\Core\Application;

use ArrayIterator;
use BrainExe\Core\Application\AppKernel;
use BrainExe\Core\Application\ControllerResolver;
use BrainExe\Core\Application\SerializedRouteCollection;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function testHandle()
->willReturn(null);

$callable = function ($arguments) {
return $arguments;
return new ArrayIterator($arguments);
};

$this->controllerResolver
Expand Down
38 changes: 28 additions & 10 deletions Tests/BrainExe/Core/Console/ListServicesCommandTest.php
Expand Up @@ -49,12 +49,13 @@ public function testExecuteShowAll()
$commandTester->execute([]);
$output = $commandTester->getDisplay();

$this->assertEquals("+------------+------------+
| service-id | visibility |
+------------+------------+
| service_1 | public |
| service_2 | private |
+------------+------------+\n", $output);
$this->assertEquals("+-------------+------------+
| service-id | visibility |
+-------------+------------+
| __service_4 | protected |
| service_1 | public |
| service_2 | private |
+-------------+------------+\n", $output);
}

public function testExecuteFilterPublic()
Expand Down Expand Up @@ -98,6 +99,7 @@ private function setupMocks()
$containerBuilder = $this->createMock(ContainerBuilder::class);
$definition1 = $this->createMock(Definition::class);
$definition2 = $this->createMock(Definition::class);
$definition4 = $this->createMock(Definition::class);

$this->rebuild
->expects($this->once())
Expand All @@ -109,34 +111,46 @@ private function setupMocks()
'service_2',
'service_1',
'service_3',
'__service_4',
];

$containerBuilder
->expects($this->at(0))
->method('getServiceIds')
->willReturn($serviceIds);

$containerBuilder
->expects($this->at(1))
->method('hasDefinition')
->with('service_1')
->with('__service_4')
->willReturn(true);
$containerBuilder
->expects($this->at(2))
->method('getDefinition')
->with('__service_4')
->willReturn($definition4);
$containerBuilder
->expects($this->at(3))
->method('hasDefinition')
->with('service_1')
->willReturn(true);
$containerBuilder
->expects($this->at(4))
->method('getDefinition')
->with('service_1')
->willReturn($definition1);
$containerBuilder
->expects($this->at(3))
->expects($this->at(5))
->method('hasDefinition')
->with('service_2')
->willReturn(true);
$containerBuilder
->expects($this->at(4))
->expects($this->at(6))
->method('getDefinition')
->with('service_2')
->willReturn($definition2);
$containerBuilder
->expects($this->at(5))
->expects($this->at(7))
->method('hasDefinition')
->with('service_3')
->willReturn(false);
Expand All @@ -149,6 +163,10 @@ private function setupMocks()
->expects($this->once())
->method('isPublic')
->willReturn(false);
$definition4
->expects($this->once())
->method('isPublic')
->willReturn(true);

return $commandTester;
}
Expand Down
13 changes: 7 additions & 6 deletions Tests/BrainExe/Core/Logger/ChannelStreamHandlerTest.php
Expand Up @@ -41,12 +41,13 @@ public function testIsHandling(array $record, $channel, $expectedResult)
public function provideIsHandling()
{
return [
[['level' => Logger::DEBUG], 'channel', false],
[['level' => Logger::INFO, 'context' => ['channel' => true]], '', false],
[['level' => Logger::INFO, 'context' => ['channel' => false]], '', true],
[['level' => Logger::INFO, 'context' => ['channel' => false]], 'context', false],
[['level' => Logger::INFO, 'context' => ['channel' => 'context']], 'context', true],
[['level' => Logger::INFO, 'context' => ['channel' => 'wrongcontext']], 'context', false],
[['level' => Logger::DEBUG], 'channel', false],
[['level' => Logger::DEBUG, 'context' => []], 'channel', false],
[['level' => Logger::INFO, 'context' => ['channel' => true]], '', false],
[['level' => Logger::INFO, 'context' => ['channel' => false]], '', true],
[['level' => Logger::INFO, 'context' => ['channel' => false]], 'context', false],
[['level' => Logger::INFO, 'context' => ['channel' => 'context']], 'context', true],
[['level' => Logger::INFO, 'context' => ['channel' => 'wrongcontext']], 'context', false],
];
}
}
44 changes: 44 additions & 0 deletions Tests/BrainExe/Core/Logger/FormatterTest.php
@@ -0,0 +1,44 @@
<?php

namespace BrainExe\Tests\Core\Logger;

use BrainExe\Core\Logger\Formatter;
use PHPUnit_Framework_TestCase;

/**
* @covers \BrainExe\Core\Logger\Formatter
*/
class FormatterTest extends PHPUnit_Framework_TestCase
{

/**
* @var Formatter
*/
private $subject;

public function setup()
{
$this->subject = new Formatter(
null,
null,
false,
true
);
}

public function testFormat()
{
$actual = $this->subject->format([
'message' => 'myMessage',
'level_name' => 'myLevelName',
'datetime' => '2015/04/02',
'channel' => 'myChannel',
'extra' => [],
'context' => ['channel' => 'myChannel']
]);

$expected = '[2015/04/02] myChannel.myLevelName: myMessage ' . PHP_EOL;

$this->assertEquals($expected, $actual);
}
}
4 changes: 2 additions & 2 deletions src/Application/AppKernel.php
Expand Up @@ -5,8 +5,8 @@
use BrainExe\Annotations\Annotations\Inject;
use BrainExe\Annotations\Annotations\Service;
use BrainExe\Core\Middleware\MiddlewareInterface;
use Iterator;
use Throwable;
use Generator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -116,7 +116,7 @@ private function handleRequest(Request $request)
*/
private function prepareResponse($response) : Response
{
if ($response instanceof Generator) {
if ($response instanceof Iterator) {
$response = iterator_to_array($response);
}

Expand Down
11 changes: 10 additions & 1 deletion src/Console/SwaggerDumpCommand.php
Expand Up @@ -135,9 +135,18 @@ protected function getResources(array $routes) : array
$data['parameters'] = $parameters;
}

$resources[$route->getPath()][strtolower(implode(',', $route->getMethods()) ?: 'get')] = $data;
$resources[$route->getPath()][$this->getRouteMethods($route)] = $data;
}

return $resources;
}

/**
* @param Route $route
* @return string
*/
protected function getRouteMethods(Route $route) : string
{
return strtolower(implode(',', $route->getMethods()) ?: 'get');
}
}
18 changes: 8 additions & 10 deletions src/Console/TestCreateCommand.php
Expand Up @@ -195,16 +195,14 @@ protected function generateMethods(ReflectionClass $serviceReflection, TestData
foreach ($methods as $method) {
$methodName = $method->getName();

if (in_array($methodName, $blacklistedMethods)) {
continue;
}

if ($method->getDeclaringClass() == $serviceReflection) {
$testData->defaultTests[] = $methodCodeGenerator->getDummyTestCode(
$testData,
$method,
$serviceFullClassName
);
if (!in_array($methodName, $blacklistedMethods)) {
if ($method->getDeclaringClass() == $serviceReflection) {
$testData->defaultTests[] = $methodCodeGenerator->getDummyTestCode(
$testData,
$method,
$serviceFullClassName
);
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Console/TestGenerator/HandleExistingFile.php
Expand Up @@ -81,6 +81,20 @@ public function handleExistingFile(
$originalTest = file_get_contents($testFileName);

$answerId = array_flip($choices)[$answer];

return $this->handleQuestion($output, $template, $answerId, $originalTest);
}

/**
* @param OutputInterface $output
* @param $template
* @param $answerId
* @param $originalTest
* @return bool|string
* @throws Exception
*/
private function handleQuestion(OutputInterface $output, string $template, string $answerId, $originalTest)
{
switch ($answerId) {
case 'replace':
break;
Expand Down
19 changes: 15 additions & 4 deletions src/DependencyInjection/CompilerPass/EventCompilerPass.php
Expand Up @@ -64,15 +64,26 @@ private function handleEvent(ReflectionClass $reflection, array &$events, string
));
}

$parameters = [];
foreach ($reflection->getConstructor()->getParameters() as $parameter) {
$parameters[] = $parameter->getName();
}
$parameters = $this->getParameters($reflection);

$events[$constant] = [
'class' => $class,
'parameters' => $parameters
];
}
}

/**
* @param ReflectionClass $reflection
* @return array
*/
private function getParameters(ReflectionClass $reflection)
{
$parameters = [];
foreach ($reflection->getConstructor()->getParameters() as $parameter) {
$parameters[] = $parameter->getName();
}

return $parameters;
}
}
37 changes: 29 additions & 8 deletions src/DependencyInjection/CompilerPass/GlobalCompilerPass.php
Expand Up @@ -18,14 +18,7 @@ public function process(ContainerBuilder $container)
{
$container->setParameter('application.root', ROOT);

$serviceIds = $container->findTaggedServiceIds(self::TAG);
$servicePriorities = [];

foreach ($serviceIds as $serviceId => $tag) {
$servicePriorities[$serviceId] = $tag[0]['priority'];
}

arsort($servicePriorities);
$servicePriorities = $this->loadCompilerPasses($container);

/** @var Logger $logger */
$totalTime = 0;
Expand All @@ -39,6 +32,34 @@ public function process(ContainerBuilder $container)
$loggerStore[] = sprintf('DIC: %0.2fms %s', $diff * 1000, $serviceId);
}

$this->logResult($container, $loggerStore, $totalTime);
}

/**
* @param ContainerBuilder $container
* @return array
*/
private function loadCompilerPasses(ContainerBuilder $container)
{
$serviceIds = $container->findTaggedServiceIds(self::TAG);
$servicePriorities = [];

foreach ($serviceIds as $serviceId => $tag) {
$servicePriorities[$serviceId] = $tag[0]['priority'];
}

arsort($servicePriorities);

return $servicePriorities;
}

/**
* @param ContainerBuilder $container
* @param array $loggerStore
* @param float $totalTime
*/
private function logResult(ContainerBuilder $container, array $loggerStore, float $totalTime)
{
$container->reset();

/** @var Logger $logger */
Expand Down
4 changes: 1 addition & 3 deletions src/MessageQueue/Gateway.php
Expand Up @@ -32,9 +32,7 @@ class Gateway
*/
public function deleteEvent(string $eventId, string $eventType = null) : bool
{
if (!empty($eventType)) {
$eventId = sprintf('%s:%s', $eventType, $eventId);
}
$eventId = sprintf('%s:%s', $eventType, $eventId);

$redis = $this->getRedis();
$delayed = $redis->zrem(self::QUEUE_DELAYED, $eventId);
Expand Down
20 changes: 15 additions & 5 deletions src/Middleware/Authentication.php
Expand Up @@ -59,11 +59,8 @@ public function processRequest(Request $request, Route $route)
return null;
}

if (!$userId) {
if ($request->isXmlHttpRequest()) {
throw new UserException(gettext('Not logged in'));
}
return new RedirectResponse('#/login');
if (empty($userId)) {
return $this->handleNotAuthenticatedRequest($request);
}

return null;
Expand Down Expand Up @@ -100,4 +97,17 @@ private function loadUser(int $userId) : UserVO
return new AnonymusUserVO();
}
}

/**
* @param Request $request
* @return RedirectResponse
* @throws UserException
*/
private function handleNotAuthenticatedRequest(Request $request)
{
if ($request->isXmlHttpRequest()) {
throw new UserException(gettext('Not logged in'));
}
return new RedirectResponse('#/login');
}
}

0 comments on commit fd6c7c5

Please sign in to comment.