From 93dfcdb2fa34917fccd0aec9a2df6693b1911c10 Mon Sep 17 00:00:00 2001 From: Scott Lin Date: Fri, 1 Jun 2018 15:05:39 +0800 Subject: [PATCH 1/4] Update package requirement to 7.1 --- .travis.yml | 1 - composer.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c4e9b1a..ca6d046 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 7.0 - 7.1 - hhvm diff --git a/composer.json b/composer.json index 4cd65f6..1b9fdb7 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "homepage": "https://github.com/MAJA-Lin" }], "require": { - "php": ">=7.0", + "php": ">=7.1", "symfony/http-foundation": "^4.1" }, "require-dev": { From cf102f50dae65795eccb7256c3315ce917e463e0 Mon Sep 17 00:00:00 2001 From: Scott Lin Date: Mon, 10 Sep 2018 14:39:04 +0800 Subject: [PATCH 2/4] Modify typo in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 00436dd..b91ea00 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ composer require majalin\http-exception ```php use MajaLin\HttpException\Exception; -use Symfony\Component\HttpFoundation\Response;; +use Symfony\Component\HttpFoundation\Response; ``` From cc55d339917056ad94a2312f87b7f2bda75108a3 Mon Sep 17 00:00:00 2001 From: Scott Lin Date: Mon, 10 Sep 2018 15:08:49 +0800 Subject: [PATCH 3/4] Add method to easy access error code --- src/Exception.php | 10 ++++++++++ tests/ExceptionTest.php | 3 +++ 2 files changed, 13 insertions(+) diff --git a/src/Exception.php b/src/Exception.php index dbf17f7..283710a 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -55,6 +55,16 @@ public function getHttpCode() return $this->httpCode; } + /** + * An alternative way to get exception error code + * + * @return integer + */ + public function getErrorCode(): int + { + return $this->getCode(); + } + /** * Initialized error message and code if their is no input or the type is * not \Exception::__construct() supported diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 0561511..5c85bef 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -34,6 +34,7 @@ public function testExceptionConstructor() $this->assertEquals(Response::HTTP_BAD_REQUEST, $e->getHttpCode()); $this->assertEquals($errorInfo['message'], $e->getMessage()); $this->assertEquals($errorInfo['errorCode'], $e->getCode()); + $this->assertEquals($errorInfo['errorCode'], $e->getErrorCode()); } /** @@ -53,6 +54,7 @@ public function testExceptionConstructingWithoutHttpCode() $this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $e->getHttpCode()); $this->assertEquals($errorInfo['message'], $e->getMessage()); $this->assertEquals($errorInfo['errorCode'], $e->getCode()); + $this->assertEquals($errorInfo['errorCode'], $e->getErrorCode()); } /** @@ -156,5 +158,6 @@ public function testInheritanceOfException() $this->assertEquals(TestException::FIRST_ERROR['httpCode'], $e->getHttpCode()); $this->assertEquals(TestException::FIRST_ERROR['message'], $e->getMessage()); $this->assertEquals(TestException::FIRST_ERROR['errorCode'], $e->getCode()); + $this->assertEquals(TestException::FIRST_ERROR['errorCode'], $e->getErrorCode()); } } From 2ea8dd15eae5eee1ffe96bff1310f8fd9757c0af Mon Sep 17 00:00:00 2001 From: Scott Lin Date: Mon, 10 Sep 2018 15:23:19 +0800 Subject: [PATCH 4/4] Now can add addtional data into the exception --- src/Exception.php | 47 ++++++++++++++++++++++++++++++ tests/ExceptionTest.php | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/src/Exception.php b/src/Exception.php index 283710a..99de26e 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -11,6 +11,11 @@ class Exception extends \Exception */ protected $httpCode = null; + /** + * @var array Additional data you want to put + */ + protected $additionalData = []; + /** * */ @@ -65,6 +70,48 @@ public function getErrorCode(): int return $this->getCode(); } + /** + * Set additionalData in exception + * + * @param array $additionalData + * + * @return HttpException\Exception + */ + public function setAdditionalData(array $additionalData) + { + $this->additionalData = $additionalData; + return $this; + } + + /** + * Get additional data + * + * @return array + */ + public function getAdditionalData(): array + { + return $this->additionalData; + } + + /** + * Append data into the additional data with key + * + * @param mixed $additionalData + * @param mixed $key = null + * + * @return HttpException\Exception + */ + public function appendAdditionalData($additionalData, $key = null) + { + if (isset($key)) { + $this->additionalData[$key] = $additionalData; + } else { + $this->additionalData[] = $additionalData; + } + + return $this; + } + /** * Initialized error message and code if their is no input or the type is * not \Exception::__construct() supported diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 5c85bef..c5d234d 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -160,4 +160,67 @@ public function testInheritanceOfException() $this->assertEquals(TestException::FIRST_ERROR['errorCode'], $e->getCode()); $this->assertEquals(TestException::FIRST_ERROR['errorCode'], $e->getErrorCode()); } + + /** + * @return void + */ + public function testGettingDefaultAdditionalData() + { + $e = new TestException(TestException::FIRST_ERROR); + + $this->assertEquals([], $e->getAdditionalData()); + } + + /** + * @return void + */ + public function testGetterAndSetterOfAdditionalData() + { + $testingData = [__FUNCTION__]; + + $e = new TestException(TestException::FIRST_ERROR); + $e->setAdditionalData($testingData); + + $this->assertEquals($testingData, $e->getAdditionalData()); + } + + /** + * @return void + */ + public function testAppendAdditionalData() + { + $testingData = [ + 'a' => __FUNCTION__ + ]; + + $e = new TestException(TestException::FIRST_ERROR); + $e->setAdditionalData($testingData); + $this->assertEquals($testingData, $e->getAdditionalData()); + + $testingData['b'] = __CLASS__; + $e->appendAdditionalData(__CLASS__, 'b'); + $this->assertEquals($testingData, $e->getAdditionalData()); + } + + /** + * Test append additional data without key + * + * @return void + */ + public function testAppendAdditionalDataWithoutKey() + { + $testingData = [__FUNCTION__]; + + $e = new TestException(TestException::FIRST_ERROR); + $e->setAdditionalData($testingData); + $this->assertEquals($testingData, $e->getAdditionalData()); + + $testingData[] = __CLASS__; + $e->appendAdditionalData(__CLASS__); + $this->assertEquals($testingData, $e->getAdditionalData()); + + $testingData[] = __NAMESPACE__; + $e->appendAdditionalData(__NAMESPACE__); + $this->assertEquals($testingData, $e->getAdditionalData()); + } }