Skip to content

Commit

Permalink
Merge pull request #2 from adam-paterson/feature/tone-analyzer
Browse files Browse the repository at this point in the history
Initial Tone Analyzer service
  • Loading branch information
adam-paterson committed Feb 24, 2017
2 parents 86f0dd3 + 54b590b commit f71342e
Show file tree
Hide file tree
Showing 27 changed files with 887 additions and 8 deletions.
1 change: 1 addition & 0 deletions .subsplit
Submodule .subsplit added at abfb97
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -46,7 +46,7 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio

[ico-version]: https://img.shields.io/packagist/v/adam-paterson/ibm-watson-sdk.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/adam-paterson/ibm-watson-sdk/master.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/adam-paterson/ibm-watson-sdk/develop.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/adam-paterson/ibm-watson-sdk.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/adam-paterson/ibm-watson-sdk.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/adam-paterson/ibm-watson-sdk.svg?style=flat-square
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -27,9 +27,11 @@
"symfony/http-foundation": "^3.2"
},
"replace": {
"adam-paterson/watson-common": "self.version"
"adam-paterson/watson-common": "self.version",
"adam-paterson/watson-tone-analyzer": "self.version"
},
"require-dev": {
"guzzle/plugin-mock": "^3.9",
"mockery/mockery": "^0.9.8",
"phpunit/phpunit": "~4.0||~5.0",
"squizlabs/php_codesniffer": "^2.3"
Expand Down
23 changes: 23 additions & 0 deletions src/Common/.scrutinizer.yml
@@ -0,0 +1,23 @@
filter:
excluded_paths: [Tests/*]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true

tools:
external_code_coverage:
timeout: 600
runs: 3
1 change: 1 addition & 0 deletions src/Common/.styleci.yml
@@ -0,0 +1 @@
preset: psr2
36 changes: 36 additions & 0 deletions src/Common/.travis.yml
@@ -0,0 +1,36 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- hhvm

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false

## Cache composer
cache:
directories:
- $HOME/.composer/cache

matrix:
include:
- php: 5.6
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'

before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist

script:
- vendor/bin/phpcs --standard=psr2 --ignore=*/Tests/* src/
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- |
if [[ "$TRAVIS_PHP_VERSION" != 'hhvm' && "$TRAVIS_PHP_VERSION" != '7.0' ]]; then
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
fi
18 changes: 16 additions & 2 deletions src/Common/Message/AbstractRequest.php
Expand Up @@ -98,11 +98,12 @@ public function getParameters()
* Get a parameter
*
* @param string $key The parameter key
* @param mixed $default Default value
* @return mixed
*/
public function getParameter($key)
public function getParameter($key, $default = null)
{
return $this->parameters->get($key);
return $this->parameters->get($key, $default);
}

/**
Expand Down Expand Up @@ -149,6 +150,19 @@ public function setPassword($value)
return $this->setParameter('password', $value);
}

/**
* Get request data
*
* @return array
*/
public function getData()
{
return [
'username' => $this->getUsername(),
'password' => $this->getPassword(),
];
}

/**
* Get the associated Response
*
Expand Down
12 changes: 8 additions & 4 deletions src/Common/Tests/Message/AbstractRequestTest.php
Expand Up @@ -31,6 +31,14 @@ public function testInitializeWithParameters()
$this->assertSame('01234', $this->request->getPassword());
}

public function testGetData()
{
$data = ['username' => 'adam', 'password' => '01234'];
$this->request->initialize($data);

$this->assertSame($data, $this->request->getData());
}

/**
* @expectedException \IBM\Watson\Common\Exception\RuntimeException
* @expectedExceptionMessage Request cannot be modified after it has been sent!
Expand Down Expand Up @@ -77,10 +85,6 @@ public function testGetResponseAfterRequestSent()

class AbstractRequestTest_MockAbstractRequest extends AbstractRequest
{
public function getData()
{
}

public function sendData($data)
{
$this->response = m::mock('\IBM\Watson\Common\Message\AbstractResponse');
Expand Down
76 changes: 76 additions & 0 deletions src/Common/Tests/TestCase.php
@@ -0,0 +1,76 @@
<?php


namespace IBM\Watson\Common\Tests;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;

/**
* Class TestCase
* @package IBM\Watson\Common\Tests
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* @var \GuzzleHttp\Client
*/
private $httpClient;

/**
* @param string $path Mock response file
* @param int $code Response code
* @return Response
*/
public function getMockHttpResponse($path, $code = 200)
{
if ($path instanceof Response) {
return $path;
}

$ref = new \ReflectionObject($this);
$dir = dirname($ref->getFileName());

// if mock file doesn't exist, check parent directory
if (!file_exists($dir . '/Mock/' . $path) && file_exists($dir . '/../Mock/' . $path)) {
return new Response($code, [], file_get_contents($dir . '/../Mock/' . $path));
}

return new Response($code, [], file_get_contents($dir . '/Mock/' . $path));
}

/**
* Get a mock HTTP client with history and mock responses
*
* @param mixed $container Responses container
* @param $responses
* @return Client
*/
public function getMockHttpClientWithHistoryAndResponses(&$container, $responses)
{
$mock = new MockHandler($responses);

$stack = HandlerStack::create($mock);
$history = Middleware::history($container);
$stack->push($history);

return new Client(['handler' => $stack]);
}

/**
* Get HTTP client
*
* @return Client
*/
public function getHttpClient()
{
if (null === $this->httpClient) {
$this->httpClient = new Client;
}

return $this->httpClient;
}
}
32 changes: 32 additions & 0 deletions src/Common/phpunit.xml.dist
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="IBM Watson Common Test Suite">
<directory>/Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<exclude>
<directory>s/Tests</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
23 changes: 23 additions & 0 deletions src/ToneAnalyzer/.scrutinizer.yml
@@ -0,0 +1,23 @@
filter:
excluded_paths: [Tests/*]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true

tools:
external_code_coverage:
timeout: 600
runs: 3
1 change: 1 addition & 0 deletions src/ToneAnalyzer/.styleci.yml
@@ -0,0 +1 @@
preset: psr2
36 changes: 36 additions & 0 deletions src/ToneAnalyzer/.travis.yml
@@ -0,0 +1,36 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- hhvm

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false

## Cache composer
cache:
directories:
- $HOME/.composer/cache

matrix:
include:
- php: 5.6
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'

before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist

script:
- vendor/bin/phpcs --standard=psr2 --ignore=*/Tests/* src/
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- |
if [[ "$TRAVIS_PHP_VERSION" != 'hhvm' && "$TRAVIS_PHP_VERSION" != '7.0' ]]; then
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
fi
21 changes: 21 additions & 0 deletions src/ToneAnalyzer/LICENSE.md
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2017 Adam Paterson <hello@adampaterson.co.uk>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.

0 comments on commit f71342e

Please sign in to comment.