From 7a20b9ff34a643decde613f7b3d019cf481b57dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20Jack=C5=ABnas?= Date: Mon, 18 Jan 2021 21:59:51 +0200 Subject: [PATCH 1/4] Implement CodeMashTestCase --- tests/Unit/CodeMashTestCase.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/Unit/CodeMashTestCase.php diff --git a/tests/Unit/CodeMashTestCase.php b/tests/Unit/CodeMashTestCase.php new file mode 100644 index 0000000..25ea2df --- /dev/null +++ b/tests/Unit/CodeMashTestCase.php @@ -0,0 +1,25 @@ +client = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->onlyMethods(['request']) + ->getMock(); + } +} From 8152685852eec5156656a71c2052588a62304e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20Jack=C5=ABnas?= Date: Mon, 18 Jan 2021 22:00:11 +0200 Subject: [PATCH 2/4] Implement AuthTest --- tests/Unit/AuthTest.php | 158 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 tests/Unit/AuthTest.php diff --git a/tests/Unit/AuthTest.php b/tests/Unit/AuthTest.php new file mode 100644 index 0000000..aae0e1c --- /dev/null +++ b/tests/Unit/AuthTest.php @@ -0,0 +1,158 @@ +auth = new Auth($this->client); + } + + /** + * @dataProvider provideTestAuthenticateFail + */ + public function testAuthenticateFail( + array $params, + string $expectedException, + string $expectedExceptionMessage + ): void { + $this->expectException($expectedException); + $this->expectExceptionMessage($expectedExceptionMessage); + + $this->auth->authenticate($params); + } + + public static function provideTestAuthenticateFail(): Generator + { + yield 'Empty params' => [ + '$params' => [], + '$expectedException' => RequestValidationException::class, + '$expectedExceptionMessage' => '"password" is required!', + ]; + + yield 'Missing userName' => [ + '$params' => ['password' => 'fooBar'], + '$expectedException' => RequestValidationException::class, + '$expectedExceptionMessage' => '"userName" is required!', + ]; + } + + public function testAuthenticate(): void + { + $params = [ + 'password' => 'testPassword', + 'userName' => 'testUserName', + ]; + + $expectedResult = ['foo' => 'bar']; + + $this->client + ->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'v2/auth/credentials', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ], + 'body' => '{"password":"testPassword","userName":"testUserName"}', + ] + )->willReturn(['result' => $expectedResult]); + + $actualResult = $this->auth->authenticate($params); + + $this->assertSame($expectedResult, $actualResult); + } + + public function testCheckAuth(): void + { + $expectedResult = ['foo' => 'bar']; + + $this->client + ->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'v2/auth/', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ], + ] + )->willReturn(['result' => $expectedResult]); + + $actualResult = $this->auth->checkAuth(); + + $this->assertSame($expectedResult, $actualResult); + } + + /** + * @dataProvider provideTestLogoutFail + */ + public function testLogoutFail( + array $params, + string $expectedException, + string $expectedExceptionMessage + ): void { + $this->expectException($expectedException); + $this->expectExceptionMessage($expectedExceptionMessage); + + $this->auth->logout($params); + } + + public static function provideTestLogoutFail(): Generator + { + yield 'Empty params' => [ + '$params' => [], + '$expectedException' => RequestValidationException::class, + '$expectedExceptionMessage' => '"bearerToken" is required!', + ]; + } + + public function testLogout(): void + { + $params = [ + 'bearerToken' => 'testBearerToken', + ]; + + $expectedResult = [ + 'result' => [ + 'foo' => 'bar' + ], + ]; + + $this->client + ->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'auth/logout', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Authorization' => 'Bearer testBearerToken', + ], + ] + )->willReturn($expectedResult); + + $actualResult = $this->auth->logout($params); + + $this->assertSame($expectedResult, $actualResult); + } +} From 770fd0d3d8e27f4e3a92ddb350a72656d21fdce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20Jack=C5=ABnas?= Date: Mon, 18 Jan 2021 22:00:52 +0200 Subject: [PATCH 3/4] Implement CodeTest --- tests/Unit/CodeTest.php | 99 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/Unit/CodeTest.php diff --git a/tests/Unit/CodeTest.php b/tests/Unit/CodeTest.php new file mode 100644 index 0000000..f4daf25 --- /dev/null +++ b/tests/Unit/CodeTest.php @@ -0,0 +1,99 @@ +code = new Code($this->client); + } + + /** @dataProvider provideTestExecuteFunctionFail */ + public function testExecuteFunctionFail( + array $params, + string $expectedException, + string $expectedExceptionMessage + ): void { + $this->expectException($expectedException); + $this->expectExceptionMessage($expectedExceptionMessage); + + $this->code->executeFunction($params); + } + + public static function provideTestExecuteFunctionFail(): Generator + { + yield 'Empty params' => [ + '$params' => [], + '$expectedException' => RequestValidationException::class, + '$expectedExceptionMessage' => '"id" is required!', + ]; + } + + /** + * @dataProvider provideTestExecuteFunction + */ + public function testExecuteFunction( + array $params, + string $expectedBody, + array $expectedResponse, + array $expectedResult + ): void { + $this->client + ->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'v2/serverless/functions/testId/execute', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ], + 'body' => $expectedBody, + ] + )->willReturn($expectedResponse); + + $actualResult = $this->code->executeFunction($params); + + $this->assertSame($expectedResult, $actualResult); + } + + public static function provideTestExecuteFunction(): Generator + { + yield 'All parameters are set' => [ + '$params' => [ + 'id' => 'testId', + 'template' => 'testTemplate', + 'qualifier' => 'testQualifier', + ], + '$expectedBody' => '{"id":"testId","template":"testTemplate","qualifier":"testQualifier"}', + '$expectedResponse' => ['result' => '{"foo":"bar"}'], + '$expectedResult' => [ + 'foo' => 'bar', + ], + ]; + + yield 'Only id is set' => [ + '$params' => [ + 'id' => 'testId', + ], + '$expectedBody' => '{"id":"testId","template":null,"qualifier":null}', + '$expectedResponse' => ['result' => '{"foo":"bar"}'], + '$expectedResult' => [ + 'foo' => 'bar', + ], + ]; + } +} From 01d28ce0d3d58b61ebc19aeaa4cd27f1001261c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20Jack=C5=ABnas?= Date: Sat, 30 Jan 2021 22:32:51 +0200 Subject: [PATCH 4/4] Implement DbTest --- tests/Unit/DbTest.php | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/Unit/DbTest.php diff --git a/tests/Unit/DbTest.php b/tests/Unit/DbTest.php new file mode 100644 index 0000000..31dedde --- /dev/null +++ b/tests/Unit/DbTest.php @@ -0,0 +1,90 @@ +db = new Db($this->client); + } + + /** + * @dataProvider provideTestInsertOneFail + */ + public function testInsertOneFail( + array $params, + string $expectedException, + string $expectedExceptionMessage + ): void { + $this->expectException($expectedException); + $this->expectExceptionMessage($expectedExceptionMessage); + + $this->db->insertOne($params); + } + + public static function provideTestInsertOneFail(): Generator + { + yield 'Empty params' => [ + '$params' => [], + '$expectedException' => RequestValidationException::class, + '$expectedExceptionMessage' => '"collectionName" is required!', + ]; + + yield 'Missing document' => [ + '$params' => ['collectionName' => 'testCollection'], + '$expectedException' => RequestValidationException::class, + '$expectedExceptionMessage' => '"document" is required!', + ]; + } + + public function testInsertOne(): void + { + $params = [ + 'collectionName' => 'testCollection', + 'document' => 'testDocument', + ]; + + $expectedParams = [ + 'collectionName' => 'testCollection', + 'document' => toJson('testDocument'), + 'bypassDocumentValidation' => false, + 'waitForFileUpload' => false, + 'ignoreTriggers' => false, + ]; + + $result = ['result' => '{}']; + + $expectedResult = []; + + $this->client + ->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'v2/db/testCollection', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ], + 'body' => toJson($expectedParams), + ] + )->willReturn($result); + + $actualResult = $this->db->insertOne($params); + + $this->assertSame($expectedResult, $actualResult); + } +}