<commit_before><?php
/**
 * @copyright 2014-2015 Basis Technology Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
namespace rosette\api;

// It is better to use phpunit --bootstrap ./vendor/autoload.php than to play with
// the pathing.
require_once __DIR__ . '/../../../vendor/autoload.php';

/**
 * Class ApiTest
 * @package rosette\api
 */
class ApiTest extends \PHPUnit_Framework_TestCase
{
    private $userKey = null;
    private static $requestDir = __DIR__ . '/../../../../mock-data/request/';
    private static $responseDir = __DIR__ . '/../../../../mock-data/response/';

    /**
     * Find the correct response file from the mock-data directory
     * Used to replace the retryingRequest function for mocking
     * @param $filename
     * @return mixed|string
     */
    private function getMockedResponse($filename)
    {
        $response = file_get_contents(self::$responseDir . $filename . '.json');
        $response = json_decode($response, true);
        return $response;
    }

    /**
     * Replace the getResponseCode method in the API class for mocking purposes
     * @param $filename
     * @return int
     */
    private function getStatusCode($filename)
    {
        return intval(file_get_contents(self::$responseDir . $filename . '.status'));
    }

    /**
     * Mock the api so that everything still works except the retryingRequest and getResponseCode methods are overridden
     * setResponseCode and checkVersion are stubbed so they just return null
     * @param $userKey
     * @return mixed
     */
    private function setUpApi($userKey)
    {
        $api = $this->getMockBuilder('rosette\api\Api')
                    ->setConstructorArgs([$userKey])
                    ->setMethods(['retryingRequest', 'setResponseCode', 'getResponseCode', 'checkVersion'])
                    ->getMock();
        $api->expects($this->any())
            ->method('retryingRequest')
            ->will($this->returnValue($this->getMockedResponse($userKey)));
        $api->expects($this->any())
            ->method('getResponseCode')
            ->will($this->returnValue($this->getStatusCode($userKey)));
        return $api;
    }

    public function testInfo()
    {
        $expected = $this->getMockedResponse('info');
        $this->userKey = 'info';
        $api = $this->setUpApi($this->userKey);
        $result = $api->info();
        $this->assertSame($expected, $result);
    }

    public function testPing()
    {
        $expected = $this->getMockedResponse('ping');
        $this->userKey = 'ping';
        $api = $this->setUpApi($this->userKey);
        $result = $api->ping();
        $this->assertSame($expected, $result);
    }

    /**
     * Get the file body for a request given a partial file name
     * @param $filename
     * @return mixed
     */
    private function getRequest($filename)
    {
        $request = file_get_contents(self::$requestDir . $filename . '.json');
        return json_decode($request, true);
    }

    /**
     * Return an array of arrays to be passed to testLanguages
     * Each sub array is of the form [file name (after request/ and before .json), endpoint]
     * @return array
     */
    public function findFiles()
    {
        $pattern = "/.*\/request\/([\w\d]*-[\w\d]*-(.*))\.json/";
        $files = [];
        foreach (glob(self::$requestDir . "*.json") as $filename) {
            preg_match($pattern, $filename, $output_array);
            $files[] = [$output_array[1], $output_array[2]];
        }
        return $files;
    }
<commit_msg>RCB-45: Linux php is less tolerant than Mac's<commit_after><?php
/**
 * @copyright 2014-2015 Basis Technology Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
namespace rosette\api;

// It is better to use phpunit --bootstrap ./vendor/autoload.php than to play with
// the pathing.
require_once __DIR__ . '/../../../vendor/autoload.php';

/**
 * Class ApiTest
 * @package rosette\api
 */
class ApiTest extends \PHPUnit_Framework_TestCase
{
    private $userKey = null;
    private static $mockDir = '/../../../../mock-data';
    private static $requestDir;
    private static $responseDir;

    /**
     * setup mock data paths
     */
    public static function setupBeforeClass()
    {
        self::$requestDir = __DIR__ . self::$mockDir . '/request/';
        self::$responseDir = __DIR__ . self::$mockDir . '/response/';
    }

    /**
     * Find the correct response file from the mock-data directory
     * Used to replace the retryingRequest function for mocking
     * @param $filename
     * @return mixed|string
     */
    private function getMockedResponse($filename)
    {
        $response = file_get_contents(self::$responseDir . $filename . '.json');
        $response = json_decode($response, true);
        return $response;
    }

    /**
     * Replace the getResponseCode method in the API class for mocking purposes
     * @param $filename
     * @return int
     */
    private function getStatusCode($filename)
    {
        return intval(file_get_contents(self::$responseDir . $filename . '.status'));
    }

    /**
     * Mock the api so that everything still works except the retryingRequest and getResponseCode methods are overridden
     * setResponseCode and checkVersion are stubbed so they just return null
     * @param $userKey
     * @return mixed
     */
    private function setUpApi($userKey)
    {
        $api = $this->getMockBuilder('rosette\api\Api')
                    ->setConstructorArgs([$userKey])
                    ->setMethods(['retryingRequest', 'setResponseCode', 'getResponseCode', 'checkVersion'])
                    ->getMock();
        $api->expects($this->any())
            ->method('retryingRequest')
            ->will($this->returnValue($this->getMockedResponse($userKey)));
        $api->expects($this->any())
            ->method('getResponseCode')
            ->will($this->returnValue($this->getStatusCode($userKey)));
        return $api;
    }

    public function testInfo()
    {
        $expected = $this->getMockedResponse('info');
        $this->userKey = 'info';
        $api = $this->setUpApi($this->userKey);
        $result = $api->info();
        $this->assertSame($expected, $result);
    }

    public function testPing()
    {
        $expected = $this->getMockedResponse('ping');
        $this->userKey = 'ping';
        $api = $this->setUpApi($this->userKey);
        $result = $api->ping();
        $this->assertSame($expected, $result);
    }

    /**
     * Get the file body for a request given a partial file name
     * @param $filename
     * @return mixed
     */
    private function getRequest($filename)
    {
        $request = file_get_contents(self::$requestDir . $filename . '.json');
        return json_decode($request, true);
    }

    /**
     * Return an array of arrays to be passed to testLanguages
     * Each sub array is of the form [file name (after request/ and before .json), endpoint]
     * @return array
     */
    public function findFiles()
    {
        // can't use global $requestDir due to a dataProvider issue where it's called before
        // everything else, include static functions:
        //    https://github.com/sebastianbergmann/phpunit/issues/1206
        // so workaround until that improvement is implemented
        $requestDir = __DIR__ . self::$mockDir . '/request/';

        $pattern = "/.*\/request\/([\w\d]*-[\w\d]*-(.*))\.json/";
        $files = [];
        foreach (glob($requestDir . "*.json") as $filename) {
            preg_match($pattern, $filename, $output_array);
            $files[] = [$output_array[1], $output_array[2]];
        }
        return $files;
    }

{'fraction_non_alphanumeric': 0.1345646437994723, 'fraction_numerical': 0.003900424457955719, 'mean_word_length': 2.535279805352798, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4312092', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2384, 'n_words': 707}