Added cost and tests

This commit is contained in:
Josh Sherman 2014-09-23 22:15:20 -04:00
parent fdf5fee3e2
commit 12da3102f9
5 changed files with 150 additions and 0 deletions

1
.coveralls.yml Normal file
View file

@ -0,0 +1 @@
service_name: travis-ci

27
.travis.yml Normal file
View file

@ -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

23
composer.json Normal file
View file

@ -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/"}
}
}

49
src/GoogleProfanity.php Normal file
View file

@ -0,0 +1,49 @@
<?php
/**
* Google Profanity Wrapper File
*
* PHP version 5
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <josh@gravityblvd.com>
* @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';
}
}
}

View file

@ -0,0 +1,50 @@
<?php
require_once '../src/GoogleProfanity.php';
class GoogleProfanityTest extends PHPUnit_Framework_TestCase
{
private $profanity;
public function setUp()
{
$this->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);
}
}