<commit_before><?php

namespace JimLind\TiVo\Tests;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use JimLind\TiVo\XmlDownloader;

/**
 * Test the TiVo\XmlDownloader service.
 */
class XmlDownloaderTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var ClientInterface
     */
    private $guzzle;

    /**
     * @var XmlDownloader
     */
    private $fixture = null;

    /**
     * Setup the PHPUnit test.
     */
    public function setUp()
    {
        $this->guzzle = $this->getMock('\GuzzleHttp\ClientInterface');

        $this->fixture = new XmlDownloader(null, null, $this->guzzle);
    }

    /**
     * Test that request type passed through to Guzzle.
     */
    public function testRequestTypePassThroughOnDownload()
    {
        $spy      = $this->any();
        $response = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $this->guzzle->expects($spy)->method('request')->willReturn($response);

        $this->fixture->download();

        $invocationList = $spy->getInvocations();
        $actual         = $invocationList[0]->parameters[0];
        $this->assertEquals('GET', $actual);
    }

    /**
     * Test that IP gets passed through to Guzzle.
     */
    public function testIPPathPassThroughOnDownload()
    {
        $ip       = rand();
        $expected = 'https://'.$ip.'/TiVoConnect';

        $spy      = $this->any();
        $response = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $this->guzzle->expects($spy)->method('request')->willReturn($response);

        $this->fixture = new XmlDownloader($ip, null, $this->guzzle);
        $this->fixture->download();

        $invocationList = $spy->getInvocations();
        $actual         = $invocationList[0]->parameters[1];
        $this->assertEquals($expected, $actual);
    }

    /**
     * Test that MAK gets passed through to Guzzle.
     */
    public function testMacPassThroughOnDownload()
    {
        $mak      = rand();
        $expected = ['tivo', $mak, 'digest'];

        $spy      = $this->any();
        $response = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $this->guzzle->expects($spy)->method('request')->willReturn($response);

        $this->fixture = new XmlDownloader(null, $mak, $this->guzzle);
        $this->fixture->download();

        $invocationList = $spy->getInvocations();
        $actual         = $invocationList[0]->parameters[2]['auth'];
        $this->assertEquals($expected, $actual);
    }

    /**
     * Test that AnchorOffset increments on successful Guzzle call.
     */
    public function testAnchorOffsetIncrement()
    {
        $firstResponse = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $firstResponse->method('getBody')
            ->will($this->returnValue('<xml><Item /></xml>'));

        $secondResponse = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $secondResponse->method('getBody')
            ->will($this->returnValue('<xml />'));

        $spy = $this->any();
        $this->guzzle->expects($spy)
            ->method('request')
            ->will($this->onConsecutiveCalls($firstResponse, $secondResponse));

        $this->fixture->download();

        $invocationList = $spy->getInvocations();

        $firstAnchor = $invocationList[0]->parameters[2]['query']['AnchorOffset'];
        $this->assertEquals(0, $firstAnchor);
        $secondAnchor = $invocationList[1]->parameters[2]['query']['AnchorOffset'];
        $this->assertEquals(1, $secondAnchor);
    }

    /**
     * Test what happens when Guzzle only throws an exception.
     */
    public function testNowPlayingException()
    {
        $this->guzzle->method('request')
            ->will($this->throwException(new TransferException()));

        $actual = $this->fixture->download();
        $this->assertEquals([], $actual);
    }

    /**
     * Test the NowPlaying string to SimpleXML parsing.
     *
     * @param string[]           $xmlList  Array of XML strings
     * @param SimpleXMLElement[] $expected Array of XML Elements
     *
     * @dataProvider guzzleReturnParsingProvider
     */
    public function testGuzzleReturnParsing($xmlList, $expected)
    {
        foreach ($xmlList as $index => $xmlString) {
            $response = $this->getMock('\Psr\Http\Message\ResponseInterface');
            $response->expects($this->once())
                ->method('getBody')
                ->will($this->returnValue($xmlString));

            $this->guzzle->expects($this->at($index))
                ->method('request')
                ->will($this->returnValue($response));
        }

        $actual = $this->fixture->download();
        $this->assertEquals($expected, $actual);<commit_msg>Fix a lot of test methods for XmlDownloader<commit_after><?php

namespace JimLind\TiVo\Tests;

use GuzzleHttp\ClientInterface;
use JimLind\TiVo\XmlDownloader;

/**
 * Test the TiVo\XmlDownloader service.
 */
class XmlDownloaderTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var ClientInterface
     */
    private $guzzle;

    /**
     * @var XmlDownloader
     */
    private $fixture = null;

    /**
     * Setup the PHPUnit test.
     */
    public function setUp()
    {
        $this->guzzle = $this->getMock('\GuzzleHttp\ClientInterface');

        $this->fixture = new XmlDownloader(null, null, $this->guzzle);
    }

    /**
     * Test that Guzzle uses correct method.
     */
    public function testMethodOnDownload()
    {
        $spy      = $this->any();
        $response = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $this->guzzle->expects($spy)
            ->method('send')
            ->willReturn($response);

        $this->fixture->download();

        $invocationList = $spy->getInvocations();
        $request        = $invocationList[0]->parameters[0];

        $this->assertEquals('GET', $request->getMethod());
    }

    /**
     * Test that Guzzle uses correct URI.
     */
    public function testUriOnDownload()
    {
        $spy      = $this->any();
        $response = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $this->guzzle->expects($spy)
            ->method('send')
            ->willReturn($response);

        $ipAddress = rand();
        $this->fixture = new XmlDownloader($ipAddress, null, $this->guzzle);
        $this->fixture->download();

        $invocationList = $spy->getInvocations();
        $request        = $invocationList[0]->parameters[0];
        $this->assertEquals(
            'https://'.$ipAddress.'/TiVoConnect',
            (string) $request->getUri()
        );
    }

    /**
     * Test that Guzzle uses correct auth options.
     */
    public function testAuthOnDownload()
    {
        $spy      = $this->any();
        $response = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $this->guzzle->expects($spy)
            ->method('send')
            ->willReturn($response);

        $mak      = rand();
        $expected = ['tivo', $mak, 'digest'];
        $this->fixture = new XmlDownloader(null, $mak, $this->guzzle);
        $this->fixture->download();

        $invocationList = $spy->getInvocations();
        $actual         = $invocationList[0]->parameters[1]['auth'];
        $this->assertEquals($expected, $actual);
    }

    /**
     * Test a Guzzle exception with response
     */
    public function testRequestExceptionResponseOnDownload()
    {
        $response = $this->getMock('\Psr\Http\Message\ResponseInterface');

        $exception = $this->getMockBuilder('GuzzleHttp\Exception\ClientException')
            ->disableOriginalConstructor()
            ->getMock();
        $exception->method('hasResponse')->willReturn(true);
        $exception->method('getResponse')->willReturn($response);

        $this->guzzle->method('send')
            ->will($this->throwException($exception));

        $actual = $this->fixture->download();
        // TODO Test Logger
        $this->assertEquals([], $actual);
    }

    /**
     * Test a Guzzle exception with message
     */
    public function testRequestExceptionMessageOnDownload()
    {
        $message = rand();

        $exception = $this->getMockBuilder('GuzzleHttp\Exception\ClientException')
            ->disableOriginalConstructor()
            ->getMock();
        $exception->method('hasResponse')->willReturn(false);
        $exception->method('getMessage')->willReturn($message);

        $this->guzzle->method('send')
            ->will($this->throwException($exception));

        $actual = $this->fixture->download();
        // TODO Test Logger
        $this->assertEquals([], $actual);
    }

    /**
     * Test that AnchorOffset increments on successful Guzzle call.
     */
    public function testAnchorOffsetIncrement()
    {
        $firstResponse = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $firstResponse->method('getBody')
            ->willReturn('<xml><Item /></xml>');
        $firstResponse->method('getStatusCode')
            ->willReturn(200);

        $secondResponse = $this->getMock('\Psr\Http\Message\ResponseInterface');
        $secondResponse->method('getBody')
            ->willReturn('<xml />');
        $secondResponse->method('getStatusCode')
            ->willReturn(200);

        $spy = $this->any();
        $this->guzzle->expects($spy)
            ->method('send')
            ->will($this->onConsecutiveCalls($firstResponse, $secondResponse));

        $this->fixture->download();

        $invocationList = $spy->getInvocations();

        $firstAnchor = $invocationList[0]->parameters[1]['query']['AnchorOffset'];
        $this->assertEquals(0, $firstAnchor);
        $secondAnchor = $invocationList[1]->parameters[1]['query']['AnchorOffset'];
        $this->assertEquals(1, $secondAnchor);
    }

    /**
     * Test the NowPlaying string to SimpleXML parsing.
     *
     * @param string[]           $xmlList  Array of XML strings
     * @param SimpleXMLElement[] $expected Array of XML Elements
     *
     * @dataProvider guzzleReturnParsingProvider
     */
    public function testGuzzleReturnParsing($xmlList, $expected)
    {
        foreach ($xmlList as $index => $xmlString) {
            $response = $this->getMock('\Psr\Http\Message\ResponseInterface');
            $response->expects($this->once())
                ->method('getBody')
                ->will($this->returnValue($xmlString));

            $this->guzzle->expects($this->at($index))
                ->method('send')
                ->will($this->returnValue($response));
        }

        $actual = $this->fixture->download();
        $this->assertEquals($expected, $actual);
{'fraction_non_alphanumeric': 0.16306281007207712, 'fraction_numerical': 0.002808199943836001, 'mean_word_length': 2.450904392764858, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 207, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2965962', 'n_tokens_mistral': 3103, 'n_tokens_neox': 2961, 'n_words': 500}