From 8278cd5d4c528967fbf2c616370a6bb0a6918c38 Mon Sep 17 00:00:00 2001 From: ondrs Date: Thu, 14 Aug 2014 16:14:53 +0200 Subject: [PATCH] rewrite tests --- .gitignore | 3 +- .travis.yml | 12 +++ license.md => LICENSE.md | 0 readme.md => README.md | 0 composer.json | 42 +++++--- src/Hi.php | 134 ------------------------ src/ondrs/Hi/Hi.php | 172 +++++++++++++++++++++++++++++++ tests/.gitignore | 3 + tests/bootstrap.php | 28 +++++ tests/ondrs/Uploader/HiTest.phpt | 66 ++++++++++++ tests/ondrs/Uploader/data.json | 1 + tests/php.ini-unix | 0 tests/test.bat | 1 + tests/tmp/.gitignore | 2 + 14 files changed, 313 insertions(+), 151 deletions(-) create mode 100644 .travis.yml rename license.md => LICENSE.md (100%) rename readme.md => README.md (100%) delete mode 100644 src/Hi.php create mode 100644 src/ondrs/Hi/Hi.php create mode 100644 tests/.gitignore create mode 100644 tests/bootstrap.php create mode 100644 tests/ondrs/Uploader/HiTest.phpt create mode 100644 tests/ondrs/Uploader/data.json create mode 100644 tests/php.ini-unix create mode 100644 tests/test.bat create mode 100644 tests/tmp/.gitignore diff --git a/.gitignore b/.gitignore index 723ef36..7579f74 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +vendor +composer.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..20dcb18 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: php + +php: + - 5.4 + - 5.5 + +before_script: + - composer self-update + - composer install --no-interaction --prefer-source --dev + - ./vendor/bin/parallel-lint -e php,phpt --exclude vendor . + +script: VERBOSE=true ./vendor/bin/tester -c ./tests/php.ini-unix -j 40 ./tests diff --git a/license.md b/LICENSE.md similarity index 100% rename from license.md rename to LICENSE.md diff --git a/readme.md b/README.md similarity index 100% rename from readme.md rename to README.md diff --git a/composer.json b/composer.json index e2ad63a..28ff9dd 100644 --- a/composer.json +++ b/composer.json @@ -1,19 +1,29 @@ { - "name": "ondrs/hi", - "type": "library", - "description": "Czech names and surnames greeting generator API PHP wrapper", - "keywords": ["czech", "greeting", "generator", "name", "surname", "php", "api", "wrapper"], - "homepage": "http://ondraplsek.cz", - "license": ["BSD-3", "GPL-2.0", "GPL-3.0"], - "authors": [{ - "name": "Ondřej Plšek", - "homepage": "http://ondraplsek.cz", - "role": "Developer" - }], - "require": { - "php": ">=5.4.0" - }, - "autoload": { - "classmap": ["src/"] + "name": "ondrs/hi", + "type": "library", + "description": "Czech names and surnames greeting generator API PHP wrapper", + "keywords": ["czech", "greeting", "generator", "name", "surname", "php", "api", "wrapper"], + "homepage": "http://ondraplsek.cz", + "license": ["MIT"], + "authors": [ + { + "name": "Ondřej Plšek", + "homepage": "http://ondraplsek.cz", + "role": "Developer" } + ], + "require": { + "php": ">=5.4.0", + "kdyby/curl": "2.2.0", + "nette/caching": "2.2.*@dev", + "nette/utils": "2.2.*@dev" + }, + "require-dev": { + "nette/tester": "1.2.*", + "janmarek/mockista": "dev-master", + "jakub-onderka/php-parallel-lint": "0.*" + }, + "autoload": { + "classmap": ["src/"] + } } diff --git a/src/Hi.php b/src/Hi.php deleted file mode 100644 index 12b4b7e..0000000 --- a/src/Hi.php +++ /dev/null @@ -1,134 +0,0 @@ -type = $type; - } - - - /** - * @param null|string $type - */ - public function setType($type) - { - $this->type = $type; - } - - - /** - * @return null|string - */ - public function getType() - { - return $this->type; - } - - - /** - * @param string $name - * @return bool|string - */ - public function mr($name) - { - return $this->to($name, self::GENDER_MALE); - } - - - /** - * @param string $name - * @return bool|string - */ - public function ms($name) - { - return $this->to($name, self::GENDER_FEMALE); - } - - - /** - * @param string $name - * @param null|string $gender - * @return bool|string - */ - public function to($name, $gender = NULL) - { - $url = $this->url . '?name=' . urlencode($name); - - if ($this->type !== NULL) { - $url .= '&type=' . urlencode($this->type); - } - - if ($gender !== NULL) { - $url .= '&gender=' . urlencode($gender); - } - - $json = $this->fetchUrl($url); - - if ($json->success) { - return $json->results[0]; - } else { - return FALSE; - } - - } - - - /** - * @param string $url - * @return \stdClass - * @throws Exception - */ - private function fetchUrl($url) - { - $response = @file_get_contents($url); - - if (!$response) { - throw new Exception("Cannot fetch URL '$url'"); - } - - $json = @json_decode($response); - - if ($json) { - return $json; - } else { - throw new Exception('Malformed JSON'); - } - - - } - -} - - -class Exception extends \Exception -{ - -} diff --git a/src/ondrs/Hi/Hi.php b/src/ondrs/Hi/Hi.php new file mode 100644 index 0000000..2f882eb --- /dev/null +++ b/src/ondrs/Hi/Hi.php @@ -0,0 +1,172 @@ +cache = new Cache($storage); + + if ($curlSender === NULL) { + $this->curlSender = new CurlSender(); + } + } + + + /** + * @param null|string $type + */ + public function setType($type) + { + $this->type = $type; + } + + + /** + * @return null|string + */ + public function getType() + { + return $this->type; + } + + + /** + * @param string $name + * @return bool|string + */ + public function mr($name) + { + return $this->to($name, self::GENDER_MALE); + } + + + /** + * @param string $name + * @return bool|string + */ + public function ms($name) + { + return $this->to($name, self::GENDER_FEMALE); + } + + + /** + * @param string $name + * @param null|string $gender + * @return bool|string + */ + public function to($name, $gender = NULL) + { + $name = Strings::fixEncoding($name); + $name = Strings::trim($name); + $name = Strings::lower($name); + + $url = self::API_URL . '?name=' . urlencode($name); + + if ($this->type !== NULL) { + $url .= '&type=' . urlencode($this->type); + } + + if ($gender !== NULL) { + $url .= '&gender=' . urlencode($gender); + } + + return $this->cache->load($url, function ($dependencies) use ($url) { + + $data = $this->fetchUrl($url); + $json = $this->parseJson($data); + + $result = $json->success ? $json->results[0] : FALSE; + + $this->cache->save($url, $result, $dependencies); + + return $result; + }); + } + + + /** + * @param string $url + * @return \stdClass + * @throws Exception + */ + public function fetchUrl($url) + { + try { + $request = new Request($url); + $response = $this->curlSender->send($request); + + return $response->getResponse(); + } catch (CurlException $e) { + throw new Exception($e->getMessage()); + } + + } + + /** + * @param $data + * @return mixed + * @throws Exception + */ + public function parseJson($data) + { + try { + return Json::decode($data); + } catch (JsonException $e) { + throw new Exception($e->getMessage()); + } + } + +} + + +class Exception extends \Exception +{ + +} diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..9632c82 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,3 @@ +coverage.dat +*.actual +*.expected diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..3e6645d --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,28 @@ +run(isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : NULL); +} diff --git a/tests/ondrs/Uploader/HiTest.phpt b/tests/ondrs/Uploader/HiTest.phpt new file mode 100644 index 0000000..e4467d2 --- /dev/null +++ b/tests/ondrs/Uploader/HiTest.phpt @@ -0,0 +1,66 @@ +curlSender = \Mockista\mock('Kdyby\Curl\CurlSender'); + $this->hi = new \ondrs\Hi\Hi(TEMP_DIR, $this->curlSender); + } + + + function tearDown() + { + $this->curlSender->assertExpectations(); + } + + + function testTo() + { + $response = \Mockista\mock('Kdyby\Curl\Response'); + $response->expects('getResponse') + ->once() + ->andReturn(file_get_contents(__DIR__ . '/data.json')); + + $this->curlSender->expects('send') + ->once() + ->andReturn($response); + + $result1 = $this->hi->to('plšek'); + $result2 = $this->hi->to('plšek'); + + Assert::same('Plšku', $result2->vocativ); + Assert::equal($result1, $result2); + } + + + function testParseJson() + { + $parsed = $this->hi->parseJson(file_get_contents(__DIR__ . '/data.json')); + Assert::type('stdClass', $parsed); + + Assert::exception(function() { + $this->hi->parseJson('!!'); + }, 'ondrs\Hi\Exception'); + } + + + +} + + +run(new HiTest()); diff --git a/tests/ondrs/Uploader/data.json b/tests/ondrs/Uploader/data.json new file mode 100644 index 0000000..7a3bc6e --- /dev/null +++ b/tests/ondrs/Uploader/data.json @@ -0,0 +1 @@ +{"success":true,"count":1,"results":[{"nominativ":"Pl\u0161ek","vocativ":"Pl\u0161ku","type":"surname","gender":"male"}]} \ No newline at end of file diff --git a/tests/php.ini-unix b/tests/php.ini-unix new file mode 100644 index 0000000..e69de29 diff --git a/tests/test.bat b/tests/test.bat new file mode 100644 index 0000000..746ded5 --- /dev/null +++ b/tests/test.bat @@ -0,0 +1 @@ +./../vendor/bin/tester -c c:/xampp/php/php.ini -j 40 . diff --git a/tests/tmp/.gitignore b/tests/tmp/.gitignore new file mode 100644 index 0000000..125e342 --- /dev/null +++ b/tests/tmp/.gitignore @@ -0,0 +1,2 @@ +* +!.*