From 9f06f906d11560b5944cb4c16b55a946720752fa Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 20 Sep 2017 13:45:07 +0200 Subject: [PATCH] updated tests --- spec/build/api/core/CheckerSpec.php | 5 +- spec/build/api/core/RequestSpec.php | 489 ++++++++++++++-------------- 2 files changed, 249 insertions(+), 245 deletions(-) diff --git a/spec/build/api/core/CheckerSpec.php b/spec/build/api/core/CheckerSpec.php index cbd3f79..c1fe1d0 100644 --- a/spec/build/api/core/CheckerSpec.php +++ b/spec/build/api/core/CheckerSpec.php @@ -3,9 +3,7 @@ use \api\core\Checker; describe('api', function(){ - describe('core', function(){ - describe('Checker', function(){ /* (1) Unknown type @@ -861,8 +859,7 @@ }); + }); - }); - }); \ No newline at end of file diff --git a/spec/build/api/core/RequestSpec.php b/spec/build/api/core/RequestSpec.php index bcc5028..91ce6d4 100644 --- a/spec/build/api/core/RequestSpec.php +++ b/spec/build/api/core/RequestSpec.php @@ -7,275 +7,282 @@ use error\core\Error; use error\core\Err; - describe('api/core/Request', function(){ + describe('api', function(){ + describe('core', function(){ + describe('Request', function(){ - beforeEach(function(){ - $_SERVER = [ - 'REQUEST_METHOD' => 'POST' - ]; - }); - - describe('[check] config file', function(){ - - it('pass when config file exists', function(){ - - expect( file_exists(Request::config_path()) )->toBeTruthy(); - - }); - - it('pass when we can read the config file', function(){ - - expect( @file_get_contents(Request::config_path()) )->not->toBe(false); - - }); - - it('pass when the config file is valid json', function(){ - - $read = @file_get_contents(Request::config_path()); - expect($read)->not->toBe(false); - expect( json_decode($read, true) )->not->toBeNull(); - expect( json_decode($read, true) )->toBeA('array'); - - }); - - }); - - describe('__construct(@path, @params)', function(){ - - context('with argument combinations', function(){ - - it('fail if @path is missing', function(){ - - $req = new Request(); - - expect($req->error->get())->toBe(Err::MissingPath); + beforeEach(function(){ + $_SERVER = [ + 'REQUEST_METHOD' => 'POST' + ]; }); - it('fail if @path is not a string', function(){ + describe('[check] config file', function(){ - expect('is_string')->toBeCalled(); - $req = new Request(1); + it('pass when config file exists', function(){ - expect($req->error->get())->toBe(Err::WrongPathModule); + expect( file_exists(Request::config_path()) )->toBeTruthy(); - }); - - it('pass if @params is an array', function(){ - - unset($_SERVER); - expect('is_array')->toBeCalled(); - - $req = new Request('someString', ['a', 'b']); - - expect($req->error->get())->toBe(Err::UnknownHttpMethod); - - }); - - it('pass if @params is not an array (default: [])', function(){ - - unset($_SERVER); - - expect('is_array')->toBeCalled(); - - $types = [true, false, null, 1, 2.3, -1.2, 0, 'blabla']; - - foreach($types as $type){ - - $req = new Request('someString', true); - expect($req->error->get())->toBe(Err::UnknownHttpMethod); - - } - - }); - - }); - - - context('with config errors', function(){ - - it('fail if the config file exists', function(){ - - // Request::config_path -> 'aa' - allow(Request::class)->toReceive('::config_path')->andReturn('invalid_fname'); - expect(Request::config_path())->toBe('invalid_fname'); - - // file_exists -> false - allow('file_exists')->toBeCalled()->andReturn(false); - expect('file_exists')->toBeCalled(); - - $req = new Request('a/b'); - - expect($req->error->get())->toBe(Err::UnreachableResource); - - }); - - it('fail if the config file cannot be read', function(){ - - // file_get_contents -> false - allow('file_get_contents')->toBeCalled()->andReturn(false); - expect('file_get_contents')->toBeCalled(); - - $req = new Request('a/b'); - - expect($req->error->get())->toBe(Err::UnreachableResource); - - }); - - it('fail if the json format is not valid', function(){ - - // file_exists -> true - allow('file_exists')->toBeCalled()->andReturn(true); - expect('file_exists')->toBeCalled(); - - // file_get_contents -> false - allow('file_get_contents')->toBeCalled()->andReturn('{incorrect_json'); - expect('file_get_contents')->toBeCalled(); - - // json_decode -> called - expect('json_decode')->toBeCalled(); - - $req = new Request('a/b'); - - expect($req->error->get())->toBe(Err::ParsingFailed); - - }); - - }); - - context('with checks errors', function(){ - - it('fail with checkPath() fails', function(){ - - allow(Request::class)->toReceive('checkPath')->andRun(function(){ - $this->error->set(Err::UnknownError); - return false; }); - $req = new Request('a/b'); + it('pass when we can read the config file', function(){ - expect($req->error->get())->toBe(Err::UnknownError); + expect( @file_get_contents(Request::config_path()) )->not->toBe(false); - }); - - it('fail with checkPermission() fails', function(){ - - // bypass checkPath(); - allow(Request::class)->toReceive('checkPath')->andReturn(true); - - allow(Request::class)->toReceive('checkPermission')->andRun(function(){ - $this->error->set(Err::UnknownError); - return false; }); - $req = new Request('a/b'); + it('pass when the config file is valid json', function(){ - expect($req->error->get())->toBe(Err::UnknownError); + $read = @file_get_contents(Request::config_path()); + expect($read)->not->toBe(false); + expect( json_decode($read, true) )->not->toBeNull(); + expect( json_decode($read, true) )->toBeA('array'); - }); - - it('fail with checkParams() fails', function(){ - - // bypass checkPath(); + checkPermission(); - allow(Request::class)->toReceive('checkPath')->andReturn(true); - allow(Request::class)->toReceive('checkPermission')->andReturn(true); - - allow(Request::class)->toReceive('checkParams')->andRun(function(){ - $this->error->set(Err::UnknownError); - return false; }); - $req = new Request('a/b'); + }); - expect($req->error->get())->toBe(Err::UnknownError); + describe('__construct(@path, @params)', function(){ + + context('with argument combinations', function(){ + + it('fail if @path is missing', function(){ + + $req = new Request(); + + expect($req->error->get())->toBe(Err::MissingPath); + + }); + + it('fail if @path is not a string', function(){ + + expect('is_string')->toBeCalled(); + $req = new Request(1); + + expect($req->error->get())->toBe(Err::WrongPathModule); + + }); + + it('pass if @params is an array', function(){ + + unset($_SERVER); + expect('is_array')->toBeCalled(); + + $req = new Request('someString', ['a', 'b']); + + expect($req->error->get())->toBe(Err::UnknownHttpMethod); + + }); + + it('pass if @params is not an array (default: [])', function(){ + + unset($_SERVER); + + expect('is_array')->toBeCalled(); + + $types = [true, false, null, 1, 2.3, -1.2, 0, 'blabla']; + + foreach($types as $type){ + + $req = new Request('someString', true); + expect($req->error->get())->toBe(Err::UnknownHttpMethod); + + } + + }); + + }); + + + context('with config errors', function(){ + + it('fail if the config file exists', function(){ + + // Request::config_path -> 'aa' + allow(Request::class)->toReceive('::config_path')->andReturn('invalid_fname'); + expect(Request::config_path())->toBe('invalid_fname'); + + // file_exists -> false + allow('file_exists')->toBeCalled()->andReturn(false); + expect('file_exists')->toBeCalled(); + + $req = new Request('a/b'); + + expect($req->error->get())->toBe(Err::UnreachableResource); + + }); + + it('fail if the config file cannot be read', function(){ + + // file_get_contents -> false + allow('file_get_contents')->toBeCalled()->andReturn(false); + expect('file_get_contents')->toBeCalled(); + + $req = new Request('a/b'); + + expect($req->error->get())->toBe(Err::UnreachableResource); + + }); + + it('fail if the json format is not valid', function(){ + + // file_exists -> true + allow('file_exists')->toBeCalled()->andReturn(true); + expect('file_exists')->toBeCalled(); + + // file_get_contents -> false + allow('file_get_contents')->toBeCalled()->andReturn('{incorrect_json'); + expect('file_get_contents')->toBeCalled(); + + // json_decode -> called + expect('json_decode')->toBeCalled(); + + $req = new Request('a/b'); + + expect($req->error->get())->toBe(Err::ParsingFailed); + + }); + + }); + + context('with checks errors', function(){ + + it('fail with checkPath() fails', function(){ + + allow(Request::class)->toReceive('checkPath')->andRun(function(){ + $this->error->set(Err::UnknownError); + return false; + }); + + $req = new Request('a/b'); + + expect($req->error->get())->toBe(Err::UnknownError); + + }); + + it('fail with checkPermission() fails', function(){ + + // bypass checkPath(); + allow(Request::class)->toReceive('checkPath')->andReturn(true); + + allow(Request::class)->toReceive('checkPermission')->andRun(function(){ + $this->error->set(Err::UnknownError); + return false; + }); + + $req = new Request('a/b'); + + expect($req->error->get())->toBe(Err::UnknownError); + + }); + + it('fail with checkParams() fails', function(){ + + // bypass checkPath(); + checkPermission(); + allow(Request::class)->toReceive('checkPath')->andReturn(true); + allow(Request::class)->toReceive('checkPermission')->andReturn(true); + + allow(Request::class)->toReceive('checkParams')->andRun(function(){ + $this->error->set(Err::UnknownError); + return false; + }); + + $req = new Request('a/b'); + + expect($req->error->get())->toBe(Err::UnknownError); + + }); + + it('pass with all checks ok', function(){ + + // bypass all checks + allow(Request::class)->toReceive('checkPath')->andReturn(true); + allow(Request::class)->toReceive('checkPermission')->andReturn(true); + allow(Request::class)->toReceive('checkParams')->andReturn(true); + + // bypass buildOptions(); + allow(Request::class)->toReceive('buildOptions')->andReturn(true); + + $req = new Request('a/b'); + + expect($req->error->get())->toBe(Err::Success); + + }); + + }); }); - it('pass with all checks ok', function(){ - // bypass all checks - allow(Request::class)->toReceive('checkPath')->andReturn(true); - allow(Request::class)->toReceive('checkPermission')->andReturn(true); - allow(Request::class)->toReceive('checkParams')->andReturn(true); + describe('checkPath(@path)', function(){ - // bypass buildOptions(); - allow(Request::class)->toReceive('buildOptions')->andReturn(true); + it('fail when wrong path format: \'module/method\'', function(){ - $req = new Request('a/b'); + $values = ['a-b', 'a /b', 'a/b ']; - expect($req->error->get())->toBe(Err::Success); + foreach($values as $value){ + + $req = new Request($value); + expect($req->error->get())->toBe(Err::WrongPathModule); + + } + + }); + + it('pass when valid path format: letter/number/-/_', function(){ + + $values = ['a_b-c/d-e_f', 'abc/def', '_a_/_b_']; + + foreach($values as $value){ + + $req = new Request($value); + expect($req->error->get())->not->toBe(Err::WrongPathModule); + + } + + }); + + it('fail when unknown module', function(){ + + allow('json_decode')->toBeCalled()->andReturn([ + 'mo-du_leA' => [], + 'moduleB' => [] + ]); + + $values = ['modulea/method', 'MODULEA/method', 'moduleC/method']; + + foreach($values as $value){ + + $req = new Request($value); + expect($req->error->get())->toBe(Err::UnknownModule); + + } + + }); + + it('fail when unknown method', function(){ + + allow('json_decode')->toBeCalled()->andReturn([ + 'mo-du_leA' => [ 'POST::me-th_odA' => [] ], + 'moduleB' => [] + ]); + + $values = ['mo-du_leA/me-thodA', 'mo-du_leA/meth_odA', 'mo-du_leA/me-th_oda']; + + foreach($values as $value){ + + $req = new Request($value); + expect($req->error->get())->toBe(Err::UnknownMethod); + + } + + }); }); - }); + + }); }); - - - describe('checkPath(@path)', function(){ - - it('fail when wrong path format: \'module/method\'', function(){ - - $values = ['a-b', 'a /b', 'a/b ']; - - foreach($values as $value){ - - $req = new Request($value); - expect($req->error->get())->toBe(Err::WrongPathModule); - - } - - }); - - it('pass when valid path format: letter/number/-/_', function(){ - - $values = ['a_b-c/d-e_f', 'abc/def', '_a_/_b_']; - - foreach($values as $value){ - - $req = new Request($value); - expect($req->error->get())->not->toBe(Err::WrongPathModule); - - } - - }); - - it('fail when unknown module', function(){ - - allow('json_decode')->toBeCalled()->andReturn([ - 'mo-du_leA' => [], - 'moduleB' => [] - ]); - - $values = ['modulea/method', 'MODULEA/method', 'moduleC/method']; - - foreach($values as $value){ - - $req = new Request($value); - expect($req->error->get())->toBe(Err::UnknownModule); - - } - - }); - - it('fail when unknown method', function(){ - - allow('json_decode')->toBeCalled()->andReturn([ - 'mo-du_leA' => [ 'POST::me-th_odA' => [] ], - 'moduleB' => [] - ]); - - $values = ['mo-du_leA/me-thodA', 'mo-du_leA/meth_odA', 'mo-du_leA/me-th_oda']; - - foreach($values as $value){ - - $req = new Request($value); - expect($req->error->get())->toBe(Err::UnknownMethod); - - } - - }); - - }); - }); \ No newline at end of file