From c7a604f45c6ef3d3401ab0ef713b37d54a585095 Mon Sep 17 00:00:00 2001 From: Ross Steele Date: Fri, 22 Nov 2019 11:14:24 +0000 Subject: [PATCH 1/3] Update GoogleTranslate class to contain a validateInput method Create validateInput method that currently checks any inputs for null and throw an invalid argument exception. Check all methods that can error out by putting in null strings Create new tests for validating against null string --- src/GoogleTranslate.php | 23 +++++++++++++++++++++++ tests/GoogleTranslateTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 0dd369d..7c3ea0e 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -24,6 +24,8 @@ public function detectLanguage($input): array return $this->detectLanguageBatch($input); } + $this->validateInput($input); + $response = $this ->translateClient ->detectLanguage($input); @@ -37,6 +39,8 @@ public function detectLanguage($input): array public function detectLanguageBatch(array $input): array { + $this->validateInput($input); + $responses = $this ->translateClient ->detectLanguageBatch($input); @@ -54,6 +58,8 @@ public function detectLanguageBatch(array $input): array public function translate($input, $to = null, $format = 'text'): array { + $this->validateInput($input); + $translateTo = $to ?? config('googletranslate.default_target_translation'); $translateTo = $this->sanitizeLanguageCode($translateTo); @@ -76,6 +82,8 @@ public function translate($input, $to = null, $format = 'text'): array public function justTranslate(string $input, $to = null): string { + $this->validateInput($input); + $translateTo = $to ?? config('googletranslate.default_target_translation'); $translateTo = $this->sanitizeLanguageCode($translateTo); @@ -91,6 +99,8 @@ public function translateBatch(array $input, string $translateTo, $format = 'tex { $translateTo = $this->sanitizeLanguageCode($translateTo); + $this->validateInput($input); + $responses = $this ->translateClient ->translateBatch($input, $translateTo, $format); @@ -149,4 +159,17 @@ public function sanitizeLanguageCode(string $languageCode) get the list of valid and supported language codes by running GoogleTranslate::languages()" ); } + + protected function validateInput($input) + { + if(is_array($input) && in_array(null, $input)) { + throw new \InvalidArgumentException('Input string cannot be null'); + } + + if(is_null($input)) { + throw new \InvalidArgumentException('Input string cannot be null'); + } + + return; + } } diff --git a/tests/GoogleTranslateTest.php b/tests/GoogleTranslateTest.php index 8af400e..fed6deb 100644 --- a/tests/GoogleTranslateTest.php +++ b/tests/GoogleTranslateTest.php @@ -202,4 +202,28 @@ public function it_sanitizes_the_language_codes() ); $this->translate->sanitizeLanguageCode('xx'); } + + /** @test */ + public function it_validates_input_against_null_strings() + { + $this->expectException(\InvalidArgumentException::class); + $this->translate->translate(null, 'en'); + $this->translate->justTranslate(null, 'en'); + } + + /** @test */ + public function it_validates_input_against_null_strings_in_a_batch() + { + $this->expectException(\InvalidArgumentException::class); + $this->translate->translateBatch([null, null], 'en'); + } + + /** @test */ + public function it_validates_input_agaisnt_null_strings_when_detecting_a_language() + { + $this->expectException(\InvalidArgumentException::class); + $this->translate->detectLanguage(null); + $this->translate->detectLanguage([null, null]); + $this->translate->detectLanguageBatch([null, null]); + } } From ffe6f1a1f65dca9a0a2720be4b5bef40ae88fbe8 Mon Sep 17 00:00:00 2001 From: Ross Steele Date: Fri, 22 Nov 2019 11:15:07 +0000 Subject: [PATCH 2/3] Add return type to validateInput method --- src/GoogleTranslate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 7c3ea0e..c5f9c88 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -160,7 +160,7 @@ public function sanitizeLanguageCode(string $languageCode) ); } - protected function validateInput($input) + protected function validateInput($input): void { if(is_array($input) && in_array(null, $input)) { throw new \InvalidArgumentException('Input string cannot be null'); From 089f14b6f91e21a818d5661ff634702b0cd224aa Mon Sep 17 00:00:00 2001 From: Ross Steele Date: Fri, 22 Nov 2019 13:14:01 +0000 Subject: [PATCH 3/3] Import InvalidArgumentException --- src/GoogleTranslate.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index c5f9c88..6e79e70 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -3,6 +3,7 @@ namespace JoggApp\GoogleTranslate; use Exception; +use InvalidArgumentException; use JoggApp\GoogleTranslate\Traits\SupportedLanguages; class GoogleTranslate @@ -163,11 +164,11 @@ public function sanitizeLanguageCode(string $languageCode) protected function validateInput($input): void { if(is_array($input) && in_array(null, $input)) { - throw new \InvalidArgumentException('Input string cannot be null'); + throw new InvalidArgumentException('Input string cannot be null'); } if(is_null($input)) { - throw new \InvalidArgumentException('Input string cannot be null'); + throw new InvalidArgumentException('Input string cannot be null'); } return;