Skip to content

Commit

Permalink
Merge pull request #174 from bearsunday/tests
Browse files Browse the repository at this point in the history
more test coverage
  • Loading branch information
koriym committed Feb 25, 2015
2 parents e6754b3 + fc03c48 commit 25dd2b8
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ composer.lock
vendor
build
preload.php

tests/stdout.log
2 changes: 1 addition & 1 deletion docs/demo-app/bootstrap/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace MyVendor\MyApp;

use BEAR\AppMeta\AppMeta;
use BEAR\Package\Bootstrap;
use BEAR\Package\AppMeta;
use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Annotations\AnnotationRegistry;

Expand Down
2 changes: 1 addition & 1 deletion docs/demo-app/src/Module/AppModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MyVendor\MyApp\Module;

use BEAR\Package\AppMeta;
use BEAR\AppMeta\AppMeta;
use BEAR\Package\PackageModule;
use BEAR\Package\Provide\Router\AuraRouterModule;
use Ray\Di\AbstractModule;
Expand Down
1 change: 0 additions & 1 deletion src/Context/HalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ class HalModule extends AbstractModule
protected function configure()
{
$this->bind(RenderInterface::class)->to(HalRenderer::class);
$this->bind(TransferInterface::class)->to(HalResponder::class);
}
}
33 changes: 26 additions & 7 deletions src/Provide/Router/CliRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Aura\Cli\CliFactory;
use Aura\Cli\Context\OptionFactory;
use Aura\Cli\Status;
use Aura\Cli\Stdio;
use BEAR\AppMeta\AbstractAppMeta;
use BEAR\Sunday\Extension\Router\RouterInterface;
use Ray\Di\Di\Inject;
Expand All @@ -26,6 +27,15 @@ class CliRouter implements RouterInterface
*/
private $appMeta;

/**
* @var \LogicException
*/
private $exception;

/**
* @var Stdio
*/
private $stdIo;

/**
* @param AbstractAppMeta $appMeta
Expand All @@ -43,9 +53,11 @@ public function setAppMeta(AbstractAppMeta $appMeta)
* @Inject
* @Named("original")
*/
public function __construct(RouterInterface $router)
public function __construct(RouterInterface $router, \LogicException $exception = null, Stdio $stdIo = null)
{
$this->router = $router;
$this->exception = $exception;
$this->stdIo = $stdIo ?: (new CliFactory)->newStdio();
}

/**
Expand All @@ -54,7 +66,8 @@ public function __construct(RouterInterface $router)
public function match(array $globals, array $server)
{
if ($globals['argc'] !== 3) {
$this->error(Status::USAGE, basename($globals['argv'][0]));
$this->error(basename($globals['argv'][0]));
$this->exitProgram(Status::USAGE);
};
list(, $method, $uri) = $globals['argv'];
$parsedUrl = parse_url($uri);
Expand Down Expand Up @@ -86,13 +99,19 @@ public function generate($name, $data)
* @param string $status
* @param string $command
*/
private function error($status, $command)
private function error($command)
{
$cliFactory = new CliFactory;
$stdio = $cliFactory->newStdio();

$help = new CliRouterHelp(new OptionFactory);
$stdio->outln($help->getHelp($command));
$this->stdIo->outln($help->getHelp($command));
}

private function exitProgram($status)
{
if ($this->exception) {
throw $this->exception;
}
// @codeCoverageIgnoreStart
exit($status);
// @codeCoverageIgnoreEnd
}
}
19 changes: 17 additions & 2 deletions src/Provide/Router/HttpMethodParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ final class HttpMethodParams implements HttpMethodParamsInterface

const APPLICATION_JSON = 'application/json';

/**
* @var string
*/
private $stdIn = 'php://input';

/**
* @param string $stdIn
*/
public function setStdIn($stdIn)
{
$this->stdIn = $stdIn;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -97,13 +110,15 @@ private function phpInput(array $server)
}
$isFormUrlEncoded = strpos($server[self::CONTENT_TYPE], self::FORM_URL_ENCODE) !== false;
if ($isFormUrlEncoded) {
parse_str(file_get_contents('php://input'), $put);
parse_str(rtrim(file_get_contents($this->stdIn)), $put);

return $put;
}
$isApplicationJson = strpos($server[self::CONTENT_TYPE], self::APPLICATION_JSON) !== false;
if ($isApplicationJson) {
return json_decode(file_get_contents('php://input'));
$content = (array) json_decode(file_get_contents($this->stdIn));

return $content;
}

return [];
Expand Down
15 changes: 15 additions & 0 deletions tests/Context/ApiModuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace BEAR\Package\Context;

use BEAR\Sunday\Annotation\DefaultSchemeHost;
use Ray\Di\Injector;

class ApiModuleTest extends \PHPUnit_Framework_TestCase
{
public function testModule()
{
$scheme = (new Injector(new ApiModule))->getInstance('', DefaultSchemeHost::class);
$this->assertSame('app://self', $scheme);
}
}
19 changes: 19 additions & 0 deletions tests/Context/HalModuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace BEAR\Package\Context;

use BEAR\Package\Provide\Representation\HalRenderer;
use BEAR\Package\Provide\Router\WebRouterModule;
use BEAR\Resource\RenderInterface;
use BEAR\Sunday\Module\Annotation\DoctrineAnnotationModule;
use BEAR\Sunday\Provide\Router\RouterModule;
use Ray\Di\Injector;

class HalModuleTest extends \PHPUnit_Framework_TestCase
{
public function testModule()
{
$renderer = (new Injector(new HalModule(new RouterModule(new DoctrineAnnotationModule))))->getInstance(RenderInterface::class);
$this->assertInstanceOf(HalRenderer::class, $renderer);
}
}
2 changes: 1 addition & 1 deletion tests/Fake/fake-app/var/www/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace FakeVendor\HelloWorld;

use BEAR\Package\AppMeta;
use BEAR\AppMeta\AppMeta;
use BEAR\Package\Bootstrap;
use Doctrine\Common\Cache\ApcCache;

Expand Down
27 changes: 24 additions & 3 deletions tests/Provide/Router/CliRouterTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace BEAR\Package;
namespace BEAR\Package\Provide\Router;

use BEAR\Package\Provide\Router\CliRouter;
use Aura\Cli\CliFactory;
use BEAR\Sunday\Provide\Router\WebRouter;

class CliRouterTest extends \PHPUnit_Framework_TestCase
Expand All @@ -14,7 +14,9 @@ class CliRouterTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->router = new CliRouter(new WebRouter('page://self'));
$stdOut = $_ENV['TEST_DIR'] . '/stdout.log';
$stdIo = (new CliFactory())->newStdio('php://stdin', $stdOut);
$this->router = new CliRouter(new WebRouter('page://self'), new \InvalidArgumentException, $stdIo);
}

public function testMatch()
Expand All @@ -32,4 +34,23 @@ public function testMatch()
$this->assertSame('page://self/', $request->path);
$this->assertSame(['name' => 'bear'], $request->query);
}

public function testGenerate()
{
$actual = $this->router->generate('', []);
$this->assertFalse($actual);
}

public function testError()
{
$this->setExpectedException(\InvalidArgumentException::class);
$globals = [
'argv' => [
'php',
'get'
],
'argc' => 2
];
$this->router->match($globals, []);
}
}
52 changes: 46 additions & 6 deletions tests/Provide/Router/HttpMethodParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace BEAR\Package\Provide\Router;

require __DIR__ . '/file_get_contents.php';

class HttpMethodParamsTest extends \PHPUnit_Framework_TestCase
{
public function testGet()
Expand Down Expand Up @@ -31,7 +29,9 @@ public function testPut()
$server = ['REQUEST_METHOD' => 'PUT', HttpMethodParams::CONTENT_TYPE => HttpMethodParams::FORM_URL_ENCODE];
$get = ['name' => 'bear'];
$post = ['name' => 'sunday'];
list($method, $params) = (new HttpMethodParams)->get($server, $get, $post);
$httpMethodParam = new HttpMethodParams;
$httpMethodParam->setStdIn(__DIR__ . '/query.txt');
list($method, $params) = $httpMethodParam->get($server, $get, $post);
$this->assertSame('put', $method);
$this->assertSame(['name' => 'kuma'], $params);
}
Expand All @@ -41,8 +41,9 @@ public function testPatch()
$server = ['REQUEST_METHOD' => 'PATCH', HttpMethodParams::CONTENT_TYPE => HttpMethodParams::FORM_URL_ENCODE];
$get = ['name' => 'bear'];
$post = ['name' => 'sunday'];
list($method, $params) = (new HttpMethodParams)->get($server, $get, $post);
$this->assertSame('patch', $method);
$httpMethodParam = new HttpMethodParams;
$httpMethodParam->setStdIn(__DIR__ . '/query.txt');
list($method, $params) = $httpMethodParam->get($server, $get, $post);
$this->assertSame(['name' => 'kuma'], $params);
}

Expand All @@ -51,7 +52,9 @@ public function testDelete()
$server = ['REQUEST_METHOD' => 'DELETE', HttpMethodParams::CONTENT_TYPE => HttpMethodParams::FORM_URL_ENCODE];
$get = ['name' => 'bear'];
$post = ['name' => 'sunday'];
list($method, $params) = (new HttpMethodParams)->get($server, $get, $post);
$httpMethodParam = new HttpMethodParams;
$httpMethodParam->setStdIn(__DIR__ . '/query.txt');
list($method, $params) = $httpMethodParam->get($server, $get, $post);
$this->assertSame('delete', $method);
$this->assertSame(['name' => 'kuma'], $params);
}
Expand Down Expand Up @@ -103,4 +106,41 @@ public function testOverrideHeaderDelete()
list($method) = (new HttpMethodParams)->get($server, [], $post);
$this->assertSame('delete', $method);
}

public function testContentTypeJson()
{
$httpMethodParam = new HttpMethodParams;
$httpMethodParam->setStdIn(__DIR__ . '/json.txt');
$server = [
'REQUEST_METHOD' => 'PUT',
'Content-Type' => 'application/json'
];
list(, $params) = $httpMethodParam->get($server, [], []);
$expected = ['name' => 'BEAR.Sunday v1.0', 'age' => 0];
$this->assertSame($expected, $params);

}

public function testContentTypeUnknown()
{
$httpMethodParam = new HttpMethodParams;
$server = [
'REQUEST_METHOD' => 'PUT',
'Content-Type' => 'text/xml'
];
list(, $params) = $httpMethodParam->get($server, [], []);
$expected = [];
$this->assertSame($expected, $params);
}

public function testNoContentType()
{
$httpMethodParam = new HttpMethodParams;
$server = [
'REQUEST_METHOD' => 'PUT',
];
list(, $params) = $httpMethodParam->get($server, [], []);
$expected = [];
$this->assertSame($expected, $params);
}
}
36 changes: 36 additions & 0 deletions tests/Provide/Router/RouterCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Provide\Router;

use Aura\Router\RouterFactory;
use BEAR\Package\Provide\Router\AuraRouter;
use BEAR\Package\Provide\Router\HttpMethodParams;
use BEAR\Package\Provide\Router\RouterCollection;
use BEAR\Sunday\Provide\Router\WebRouter;

class RouterCollectionTest extends \PHPUnit_Framework_TestCase
{
public function testMatch()
{
$routerAdapter = (new RouterFactory)->newInstance();
$auraRouter = new AuraRouter($routerAdapter, 'page://self', new HttpMethodParams);
$webRouter = new WebRouter('page://self', new HttpMethodParams);
$routerCollection = new RouterCollection([$auraRouter, $webRouter]);
$routerAdapter
->addPost(null, '/blog/{id}')
->addValues(['path' => '/blog']);
$globals = [
'_POST' => ['title' => 'hello']
];
$server = [
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => 'http://localhost/blog/PC6001?query=value#fragment'
];

$request = $routerCollection->match($globals, $server);
$this->assertSame('post', $request->method);
$this->assertSame('page://self/blog', $request->path);
$this->assertSame(['id' => 'PC6001', 'title' => 'hello'], $request->query);
}

}
8 changes: 0 additions & 8 deletions tests/Provide/Router/file_get_contents.php

This file was deleted.

1 change: 1 addition & 0 deletions tests/Provide/Router/json.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": "BEAR.Sunday v1.0", "age": 0}
1 change: 1 addition & 0 deletions tests/Provide/Router/query.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name=kuma
Loading

0 comments on commit 25dd2b8

Please sign in to comment.