From 12da3102f9518737679e85e999c1099ed97575e2 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 23 Sep 2014 22:15:20 -0400 Subject: [PATCH] Added cost and tests --- .coveralls.yml | 1 + .travis.yml | 27 +++++++++++++++++++ composer.json | 23 ++++++++++++++++ src/GoogleProfanity.php | 49 ++++++++++++++++++++++++++++++++++ tests/GoogleProfanityTest.php | 50 +++++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 .coveralls.yml create mode 100644 .travis.yml create mode 100644 composer.json create mode 100644 src/GoogleProfanity.php create mode 100644 tests/GoogleProfanityTest.php diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..9160059 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1 @@ +service_name: travis-ci diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6f89428 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,27 @@ + +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + - 5.6 + - hhvm + - hhvm-nightly + +matrix: + allow_failures: + - php: hhvm-nightly + +install: + - composer install + +before_script: + - mkdir -p build/logs + - cd tests + +script: + - phpunit --colors --coverage-clover /home/travis/build/joshtronic/php-googleprofanity/build/logs/clover.xml . + +after_success: + - php ../vendor/bin/coveralls --config ../.coveralls.yml -v diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..489baae --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "joshtronic/php-googleprofanity", + "description": "PHP Wrapper for the unofficial Google Profanity API", + "version": "1.0.0", + "type": "library", + "keywords": ["google", "profanity", "api"], + "homepage": "https://github.com/joshtronic/php-googleprofanity", + "license": "MIT", + "authors": [{ + "name": "Josh Sherman", + "email": "josh@gravityblvd.com", + "homepage": "http://joshtronic.com" + }], + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "satooshi/php-coveralls": "dev-master" + }, + "autoload": { + "psr-4": {"joshtronic\\": "src/"} + } +} diff --git a/src/GoogleProfanity.php b/src/GoogleProfanity.php new file mode 100644 index 0000000..272bf52 --- /dev/null +++ b/src/GoogleProfanity.php @@ -0,0 +1,49 @@ + + * @copyright Copyright 2014, Josh Sherman + * @license http://www.opensource.org/licenses/mit-license.html + * @link https://github.com/joshtronic/php-googleprofanity + */ + +namespace joshtronic; + +/** + * Google Profanity API Interface + */ +class GoogleProfanity +{ + /** + * Check + * + * Checks if a word is considered profanity. + * + * @usage GoogleProfanity::check('fuck'); // returns true + * @param string $word word to check + * @param string $endpoint the endpoint to call (helps testing) + * @return boolean whether or not the word is profanity + */ + public function check($word, $endpoint = 'http://www.wdyl.com/profanity?q=') + { + $response = json_decode(file_get_contents($endpoint . $word), true); + + if ($response == null || !isset($response['response']) + || !in_array($response['response'], array('true', 'false'))) + { + throw new \Exception('Invalid response from API.'); + } + else + { + return $response['response'] == 'true'; + } + } +} + diff --git a/tests/GoogleProfanityTest.php b/tests/GoogleProfanityTest.php new file mode 100644 index 0000000..6ffb9f2 --- /dev/null +++ b/tests/GoogleProfanityTest.php @@ -0,0 +1,50 @@ +profanity = new joshtronic\GoogleProfanity(); + } + + /** + * @dataProvider providerFormatPhoneNumber + */ + public function testCheck($a, $b) + { + $this->assertEquals($b, $this->profanity->check($a)); + } + + public function providerFormatPhoneNumber() + { + return array( + array('alpha', false), + array('beta', false), + array('joshtronic', false), + array('god', false), + array('fck', false), + array('fuck', true), + array('shit', true), + array('cocksucker', true), + array('cuntface', false), // Unsure why not... + ); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid response from API. + */ + public function testInvalidResponse() + { + $file = SITE_PATH . 'null-'; + + file_put_contents($file . 'test', null); + + $this->profanity->check('test', $file); + } +} +