diff --git a/composer.json b/composer.json index 1be9702..f90c5aa 100644 --- a/composer.json +++ b/composer.json @@ -38,8 +38,7 @@ } }, "scripts": { - "test": "phpunit", - "coverage": "phpunit --coverage-html build/coverage/html" + "test": "phpunit --testdox" }, "extra": { "branch-alias": { diff --git a/src/AbstractNoCaptcha.php b/src/AbstractNoCaptcha.php index 03eded6..87abf2e 100644 --- a/src/AbstractNoCaptcha.php +++ b/src/AbstractNoCaptcha.php @@ -152,6 +152,16 @@ public function setLang($lang) return $this; } + /** + * Get language code. + * + * @return string|null + */ + public function getLang() + { + return $this->lang; + } + /** * Set HTTP Request Client. * @@ -279,7 +289,7 @@ abstract protected function parseResponse($json); */ protected function hasLang() { - return ! empty($this->lang); + return ! empty($this->getLang()); } /** diff --git a/src/Contracts/NoCaptcha.php b/src/Contracts/NoCaptcha.php index a6b8f70..093cded 100644 --- a/src/Contracts/NoCaptcha.php +++ b/src/Contracts/NoCaptcha.php @@ -23,7 +23,7 @@ interface NoCaptcha * * @param \Arcanedev\NoCaptcha\Contracts\Utilities\Request $request * - * @return self + * @return $this */ public function setRequestClient(Request $request); @@ -32,10 +32,17 @@ public function setRequestClient(Request $request); * * @param string $lang * - * @return self + * @return $this */ public function setLang($lang); + /** + * Get language code. + * + * @return string|null + */ + public function getLang(); + /* ----------------------------------------------------------------- | Main Methods | ----------------------------------------------------------------- diff --git a/src/NoCaptchaManager.php b/src/NoCaptchaManager.php index 22a84a0..9c14eb6 100644 --- a/src/NoCaptchaManager.php +++ b/src/NoCaptchaManager.php @@ -84,7 +84,7 @@ protected function buildDriver(string $driver): NoCaptchaContract return $this->container->make($driver, [ 'secret' => $this->config('secret'), 'siteKey' => $this->config('sitekey'), - 'locale' => $this->config('lang') ?: $this->container->getLocale(), + 'lang' => $this->config('lang') ?: $this->container->getLocale(), ]); } diff --git a/src/NoCaptchaV2.php b/src/NoCaptchaV2.php index 6d075e1..4339325 100644 --- a/src/NoCaptchaV2.php +++ b/src/NoCaptchaV2.php @@ -45,7 +45,7 @@ private function getScriptSrc($callbackName = null) $queries = []; if ($this->hasLang()) - Arr::set($queries, 'hl', $this->lang); + Arr::set($queries, 'hl', $this->getLang()); if ($this->hasCallbackName($callbackName)) { Arr::set($queries, 'onload', $callbackName); diff --git a/src/NoCaptchaV3.php b/src/NoCaptchaV3.php index fcc2a5e..338c6b9 100644 --- a/src/NoCaptchaV3.php +++ b/src/NoCaptchaV3.php @@ -6,7 +6,6 @@ use Arcanedev\Html\Elements\Input; use Arcanedev\NoCaptcha\Utilities\ResponseV3; -use Illuminate\Support\Arr; /** * Class NoCaptchaV3 @@ -128,12 +127,12 @@ private function getScriptSrc($callbackName = null) $queries = []; if ($this->hasLang()) - Arr::set($queries, 'hl', $this->lang); + $queries['hl'] = $this->getLang(); - Arr::set($queries, 'render', $this->getSiteKey()); + $queries['render'] = $this->getSiteKey(); if ($this->hasCallbackName($callbackName)) - Arr::set($queries, 'onload', $callbackName); + $queries['onload'] = $callbackName; return $this->getClientUrl() . (count($queries) ? '?' . http_build_query($queries) : ''); } diff --git a/tests/Laravel/NoCaptchaManagerTest.php b/tests/Laravel/NoCaptchaManagerTest.php index c4ef8dc..8dfd8d0 100644 --- a/tests/Laravel/NoCaptchaManagerTest.php +++ b/tests/Laravel/NoCaptchaManagerTest.php @@ -81,15 +81,16 @@ public function it_can_get_default_driver_via_helper(): void /** @test */ public function it_can_get_driver_by_given_version(): void { - static::assertInstanceOf( - NoCaptchaV3::class, - $this->manager->version('v3') - ); + $versions = [ + 'v3' => NoCaptchaV3::class, + 'v2' => NoCaptchaV2::class, + ]; - static::assertInstanceOf( - NoCaptchaV2::class, - $this->manager->version('v2') - ); + foreach ($versions as $version => $class) { + $captcha = $this->manager->version(); + + static::assertInstanceOf(NoCaptchaV3::class, $captcha); + } } /** @test */ @@ -100,4 +101,22 @@ public function it_must_throw_exception_on_unsupported_version(): void $this->manager->version('v1'); } + + /** @test */ + public function it_can_set_lang_from_locale() + { + $versions = [ + 'v3' => NoCaptchaV3::class, + 'v2' => NoCaptchaV2::class, + ]; + + $this->app->setLocale('fr'); + + foreach ($versions as $version => $class) { + $captcha = $this->manager->version($version); + + static::assertInstanceOf($class, $captcha); + static::assertSame('fr', $captcha->getLang()); + } + } }