Skip to content

Commit

Permalink
rewrite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrs committed Aug 14, 2014
1 parent 0bd02e1 commit 8278cd5
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 151 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
vendor
composer.lock
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
42 changes: 26 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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/"]
}
}
134 changes: 0 additions & 134 deletions src/Hi.php

This file was deleted.

172 changes: 172 additions & 0 deletions src/ondrs/Hi/Hi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: ondrs
* Date: 13.10.13
* Time: 18:53
* To change this template use File | Settings | File Templates.
*/

namespace ondrs\Hi;


use Kdyby\Curl\CurlException;
use Kdyby\Curl\CurlSender;
use Kdyby\Curl\Request;
use Nette\Caching\Cache;
use Nette\Caching\Storages\FileStorage;
use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use Nette\Utils\Strings;

class Hi
{

/** @var \Nette\Caching\Cache */
private $cache;

/** @var CurlSender */
private $curlSender;

/** @var null|string */
private $type;

const
API_URL = 'http://hi.ondraplsek.cz',
TYPE_NAME = 'name',
TYPE_SURNAME = 'surname',

GENDER_MALE = 'male',
GENDER_FEMALE = 'female';


/**
* @param $cacheDir
* @param CurlSender|NULL $curlSender
*/
public function __construct($cacheDir, CurlSender $curlSender = NULL)
{
FileSystem::createDir($cacheDir);

$storage = new FileStorage($cacheDir);
$this->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
{

}
3 changes: 3 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage.dat
*.actual
*.expected
Loading

0 comments on commit 8278cd5

Please sign in to comment.