Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
joesama committed Oct 8, 2019
1 parent 90f1b3f commit 8b35234
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/Routings/Concerns/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ trait Grammar
/**
* Path naming convention definition.
*
* @param string $type
* @param string $function
* @param array $attributes
*
* @return string
*/
protected function pathConvention(string $type, string $function, array $attributes): string
public function pathConvention(string $type, string $function, array $attributes): string
{
$keymap = $this->keymapIsString(Arr::get($attributes, 'keymap'));

Expand Down Expand Up @@ -76,7 +77,7 @@ private function appendIdParameter(array $keymap): array
*
* @return string
*/
protected function classConvention(string $type, string $controller, string $function): string
public function classConvention(string $type, string $controller, string $function): string
{
return Str::ucfirst($controller) . 'Controller@' . Str::camel(Str::lower($type) . '_' . $function);
}
Expand All @@ -91,12 +92,12 @@ protected function classConvention(string $type, string $controller, string $fun
*
* @return string
*/
protected function namedConvention(string $type, string $controller, string $function, array $attributes): string
public function namedConvention(string $type, string $controller, string $function, array $attributes): string
{
$named = Arr::get($attributes, 'named', null);

if ($named === '' || $named === null) {
$named = Str::lower($controller . '.' . $type . '.' . $function);
$named = Str::lower($controller . '.' . $type . '.l' . $function);
}

return Str::lower($named);
Expand Down
14 changes: 12 additions & 2 deletions src/Routings/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public function setNameSpace(string $namespace = null)
$this->namespace = $namespace;
}

protected function getComponentNameSpace()
/**
* Get component controller namespace.
*
* @return string
*/
public function getComponentNameSpace(): string
{
if ($this->namespace === null) {
return Arr::get($this->component->getComponentNameSpace(), 'component');
Expand All @@ -68,7 +73,12 @@ protected function getComponentNameSpace()
return $this->namespace;
}

protected function getApiNameSpace()
/**
* Get API controller namespace.
*
* @return string
*/
public function getApiNameSpace(): string
{
if ($this->namespace === null) {
return Arr::get($this->component->getComponentNameSpace(), 'api');
Expand Down
31 changes: 27 additions & 4 deletions tests/Feature/Components/ComponentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Tests\Feature\Components;

use Exception;
use ReflectionClass;
use Illuminate\Routing\Router;
use Joesama\Pintu\PintuProvider;
use Orchestra\Testbench\TestCase;
use Joesama\Pintu\Components\Manager;
use Illuminate\Contracts\Support\Arrayable;
use Joesama\Pintu\Services\ComponentServices;

Expand All @@ -28,13 +30,34 @@ protected function getEnvironmentSetUp($app)

/**
* @test
* @testdox Validate the class builder exist
* @testdox Validate the component service without service provider.
*/
public function theComponentServicesIsExist()
public function theComponentServicesWithNonServiceProvider()
{
$file = realpath(__DIR__ . '/../../../src/Services/ComponentServices.php');
$this->expectException(Exception::class);

$this->assertFileExists($file);
$componentServices = new ComponentServices(Router::class);

$componentpath = $componentServices->getComponentFilePath();
}

/**
* @test
* @testdox Validate component file path and stub.
*/
public function theComponentFilePathAndStub()
{
$provider = new ReflectionClass($this->testNameSpace);

$componentManger = new Manager($provider);

$componentPath = $componentManger->getComponentFilePath();

$componentStub = $componentManger->getComponentFileStub();

$this->assertIsString($componentPath);

$this->assertIsString($componentStub);
}

/**
Expand Down
204 changes: 202 additions & 2 deletions tests/Feature/Routings/RoutingBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
namespace Tests\Feature\Routings;

use Mockery as mock;
use ReflectionClass;
use Illuminate\Support\Str;
use Illuminate\Routing\Router;
use Orchestra\Testbench\TestCase;
use Joesama\Pintu\PintuProvider;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Collection;
use Joesama\Pintu\Routings\Builder;
use Joesama\Pintu\Routings\Manager;
use Joesama\Pintu\Services\RoutingServices;
use Joesama\Pintu\Components\Manager as ComponentManager;

class RoutingBuilderTest extends TestCase
{
Expand Down Expand Up @@ -38,7 +44,10 @@ public function callRoutingServices()
$this->assertEquals(null, $service->router(PintuProvider::class, $this->router));
}

/** @test */
/**
* @test
* @testdox Initiate routing service.
* */
public function initiateRouteService()
{
$this->assertEquals(0, $this->router->getRoutes()->count());
Expand All @@ -47,4 +56,195 @@ public function initiateRouteService()

$this->assertEquals(0, $this->router->getRoutes()->count());
}

/**
*
* @test
* @testdox Validate routing manager.
*
*/
public function theRoutingManagerShouldBuildTheRouting()
{
$provider = new ReflectionClass(PintuProvider::class);

$componentManger = new ComponentManager($provider);

$namespace = $componentManger->getComponentNameSpace();

$routingManger = new Manager($componentManger);

$routingManger->setNameSpace();

$this->assertEquals($routingManger->getApiNameSpace(), $namespace['api']);

$this->assertEquals($routingManger->getComponentNameSpace(), $namespace['component']);

$routingManger->setNameSpace('Controller');

$this->assertNotEquals($routingManger->getApiNameSpace(), $namespace['api']);

$this->assertNotEquals($routingManger->getComponentNameSpace(), $namespace['component']);
}

/**
*
* @test
* @testdox Validate builder naming % path convention working fine.
*
*/
public function theBuilderShouldUseGrammarConvention()
{
$builder = mock::mock(Builder::class, [$this->router]);

$type = 'type';

$controller = 'controller';

$function = 'function';

$pathConventionExpected = $function;

$builder->shouldReceive('pathConvention')
->with($type, $function, [])
->andReturn($pathConventionExpected);

$this->assertIsString($builder->pathConvention($type, $function, []));

$this->assertEquals($pathConventionExpected, $builder->pathConvention($type, $function, []));

$pathConventionKeymapEmptyArrayExpected = $function;

$keymap = ['keymap' => []];

$builder->shouldReceive('pathConvention')
->with($type, $function, $keymap)
->andReturn($pathConventionKeymapEmptyArrayExpected);

$this->assertIsString($builder->pathConvention($type, $function, $keymap));

$this->assertEquals(
$pathConventionKeymapEmptyArrayExpected,
$builder->pathConvention($type, $function, $keymap)
);

$pathConventionKeymapExpected = $function . '/{id_name}';

$keymap = ['keymap' => 'id_name'];

$builder->shouldReceive('pathConvention')
->with($type, $function, $keymap)
->andReturn($pathConventionKeymapExpected);

$this->assertIsString($builder->pathConvention($type, $function, $keymap));

$this->assertEquals($pathConventionKeymapExpected, $builder->pathConvention($type, $function, $keymap));

$classConventionExpected = Str::ucfirst($controller) . 'Controller@' . Str::camel(Str::lower($type) . '_' . $function);

$builder->shouldReceive('classConvention')
->with($type, $controller, $function)
->andReturn($classConventionExpected);

$this->assertIsString($builder->classConvention($type, $controller, $function));

$this->assertEquals($classConventionExpected, $builder->classConvention($type, $controller, $function));

$namedConventionExpected = Str::lower($controller . '.' . $type . '.' . $function);

$builder->shouldReceive('namedConvention')
->with($type, $controller, $function, [])
->andReturn($namedConventionExpected);

$this->assertIsString($builder->namedConvention($type, $controller, $function, []));

$this->assertEquals($namedConventionExpected, $builder->namedConvention($type, $controller, $function, []));
}

/**
*
* @test
* @testdox Register component routing.
* @return void
*
*/
public function theBuilderCanRegisterComponentRouting()
{
$builder = mock::mock(Builder::class, [$this->router])->makePartial();

$collection = mock::mock(Collection::class)->makePartial();

$component = [
'controll' => [
'function' => [
'keymap' => 'id',
'auth' => true,
'named' => ''
]
]
];

$collection->shouldReceive('make')->with($component)->twice()->andReturnSelf();

$namespace = 'namespace';

$builder->shouldReceive('componentRouting')
->with($collection->make($component), $namespace)
->once();

$builder->componentRouting($collection->make($component), $namespace);

$build = new Builder($this->router);

$build->componentRouting(collect($component), $namespace);

$this->assertCount(3, $this->router->getRoutes());
}

/**
*
* @test
* @testdox Register API routing.
* @return void
*
*/
public function theBuilderCanRegisterApiRouting()
{
$builder = mock::mock(Builder::class, [$this->router])->makePartial();

$collection = mock::mock(Collection::class)->makePartial();

$api = [
'get' => [
['path', 'controller@method', 'named']
],
'post' => [
['path', 'controller@method', 'named']
],
'put' => [
['path', 'controller@method', 'named']
],
'patch' => [
['path', 'controller@method', 'named']
],
'delete' => [
['path', 'controller@method', 'named']
]
];

$collection->shouldReceive('make')->with($api)->twice()->andReturnSelf();

$namespace = 'namespace';

$builder->shouldReceive('apiRouting')
->with($collection->make($api), $namespace)
->once();

$builder->apiRouting($collection->make($api), $namespace);

$build = new Builder($this->router);

$build->apiRouting(collect($api), $namespace);

$this->assertCount(5, $this->router->getRoutes());
}
}

0 comments on commit 8b35234

Please sign in to comment.