<commit_before><?php

use \Mockery as m;

class TwilioTest extends PHPUnit_Framework_TestCase {

    protected $formHeaders = array('Content-Type' => 'application/x-www-form-urlencoded');
    protected $callParams = array('To' => '123', 'From' => '123', 'Url' => 'http://example.com');
    protected $nginxError = array(500, array('Content-Type' => 'text/html'),
                '<html>Nginx 500 error</html>'
            );
    function tearDown() {
        m::close();
    }

    function getClient($http) {
        return new Services_Twilio('AC123', '123', '2010-04-01', $http);
    }

    function createMockHttp($url, $method, $response, $params = null, 
        $status = 200
    ) {
        $http = m::mock(new Services_Twilio_TinyHttp);
        if ($method === 'post') {
            $http->shouldReceive('post')->once()->with(
                "/2010-04-01/Accounts/AC123$url.json", 
                $this->formHeaders, 
                http_build_query($params)
            )->andReturn(array(
                    $status, 
                    array('Content-Type' => 'application/json'),
                    json_encode($response)
                )
            );
        } else {
            $query = empty($params) ? '' : '?' . http_build_query($params);
            $http->shouldReceive($method)->once()->with(
                "/2010-04-01/Accounts/AC123$url.json$query"
            )->andReturn(array(
                    $status, 
                    array('Content-Type' => 'application/json'),
                    json_encode($response)
                )
            );
        }
        return $http;
    }

    /**
     * @dataProvider uriTestProvider
     */
    function testRequestUriConstructedProperly($path, $params, $full_uri, $end_string) {
        $this->assertSame($end_string, Services_Twilio::getRequestUri(
            $path, $params, $full_uri
        ));
    }

    function uriTestProvider() {
        return array(
            array('/2010-04-01/Accounts', array('FriendlyName' => 'hi'), false, 
                '/2010-04-01/Accounts.json?FriendlyName=hi'),
            array('/2010-04-01/Accounts', array(), false, 
                '/2010-04-01/Accounts.json'),
            array('/2010-04-01/Accounts.json', array(), true, 
                '/2010-04-01/Accounts.json'),
            array('/2010-04-01/Accounts.json', array('FriendlyName' => 'hi'), true, 
                '/2010-04-01/Accounts.json'),
            array('/2010-04-01/Accounts', array(
                'FriendlyName' => 'hi', 'foo' => 'bar'
            ), false, '/2010-04-01/Accounts.json?FriendlyName=hi&foo=bar'),
        );
    }

    function testNeedsRefining() {
        $http = $this->createMockHttp('', 'get', array(
                    'sid' => 'AC123',
                    'friendly_name' => '<NAME>',
                ));
        $client = $this->getClient($http);
        $this->assertEquals('AC123', $client->account->sid);
        $this->assertEquals('<NAME>', $client->account->friendly_name);
    }

    function testAccessSidAvoidsNetworkCall() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->never();
        $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
        $client->account->sid;
    }

    function testOnlyOneClientCreated() {
        $client = new Services_Twilio('AC123', '456');
        $client->account->client->sid = 'CL456';
        $this->assertSame('CL456', $client->account->sandbox->client->sid);
    }

    function testNullVersionReturnsNewest() {
        $client = new Services_Twilio('AC123', '123', null);
        $this->assertEquals('2010-04-01', $client->getVersion());
        $client = new Services_Twilio('AC123', '123', 'v1');
        $this->assertEquals('2010-04-01', $client->getVersion());
        $client = new Services_Twilio('AC123', '123', '2010-04-01');
        $this->assertEquals('2010-04-01', $client->getVersion());
        $client = new Services_Twilio('AC123', '123', '2008-08-01');
        $this->assertEquals('2008-08-01', $client->getVersion());
    }

    function testObjectLoadsOnlyOnce() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->once()
            ->with('/2010-04-01/Accounts/AC123.json')
            ->andReturn(array(200, array('Content-Type' => 'application/json'),
                json_encode(array(
                    'sid' => 'AC123',
                    'friendly_name' => '<NAME>',
                    'status' => 'active',
                ))
            ));
        $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
        $client->account->friendly_name;
        $client->account->friendly_name;
        $client->account->status;
    }

    function testSubresourceLoad() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->once()
            ->with('/2010-04-01/Accounts/AC123/Calls/CA123.json')
            ->andReturn(array(200, array('Content-Type' => 'application/json'),
                json_encode(array('status' => 'Completed'))
            ));

        $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
        $this->assertEquals(
            'Completed',
            $client->account->calls->get('CA123')->status
        );
    }

    function testSubresourceSubresource() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->once()
            ->with('/2010-04-01/Accounts/AC123/Calls/CA123/Notifications/NO123.json')
            ->andReturn(array(200, array('Content-Type' => 'application/json'),
                json_encode(array('message_text' => 'Foo'))
            ));

        $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
        $notifs = $client->account->calls->get('CA123')->notifications;
        $this->assertEquals('Foo', $notifs->get('NO123')->message_text);
    }

    function testGetIteratorUsesFilters() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $qs = '?Page=0&PageSize=10&StartTime%3E=2009-07-06';
        $http->shouldReceive('get')->once()
            ->with('/2010-04-01/Accounts/AC123/Calls.json' . $qs)
            ->andReturn(array(200, array('Content-Type' => 'application/json'),
                json_encode(array(
                    'total' => 1,
                    'calls' => array(array('status' => 'Completed', 'sid' => 'CA123'))
                ))
            ));
        $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
        $iterator = $client->account->calls->getIterator(
            0, 10, array('StartTime>' => '2009-07-06'));
        foreach ($iterator as $call) {
            $this->assertEquals('Completed', $call->status);
            break;
        }
    }

    function testListResource() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->once()
            ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=0&PageSize=10')
            ->andReturn(array(200, array('Content-Type' => 'application/json'),
                json_encode(array(
                    'total' => 1,
                    'calls' => array(array('status' => 'completed', 'sid' => 'CA123'))
                ))
            ));
        $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
        $page = $client->account->calls->getPage(0, 10);
        $call = current($page->getItems());
        $this->assertEquals('completed', $call->status);
        $this->assertEquals(1, $page->total);
    }

    function testInstanceResourceUriConstructionFromList() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->once()
            ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=0&PageSize=10')
            ->andReturn(array(200, array('Content-Type' => 'application/json'),
                json_encode(array(<commit_msg>refactor twilio tests<commit_after><?php

use \Mockery as m;

class TwilioTest extends PHPUnit_Framework_TestCase {

    protected $formHeaders = array('Content-Type' => 'application/x-www-form-urlencoded');
    protected $callParams = array('To' => '123', 'From' => '123', 'Url' => 'http://example.com');
    protected $nginxError = array(500, array('Content-Type' => 'text/html'),
                '<html>Nginx 500 error</html>'
            );
    function tearDown() {
        m::close();
    }

    function getClient($http) {
        return new Services_Twilio('AC123', '123', '2010-04-01', $http);
    }

    function createMockHttp($url, $method, $response, $params = null, 
        $status = 200
    ) {
        $http = m::mock(new Services_Twilio_TinyHttp);
        if ($method === 'post') {
            $http->shouldReceive('post')->once()->with(
                "/2010-04-01/Accounts/AC123$url.json", 
                $this->formHeaders, 
                http_build_query($params)
            )->andReturn(array(
                    $status, 
                    array('Content-Type' => 'application/json'),
                    json_encode($response)
                )
            );
        } else {
            $query = empty($params) ? '' : '?' . http_build_query($params);
            $http->shouldReceive($method)->once()->with(
                "/2010-04-01/Accounts/AC123$url.json$query"
            )->andReturn(array(
                    $status, 
                    array('Content-Type' => 'application/json'),
                    json_encode($response)
                )
            );
        }
        return $http;
    }

    /**
     * @dataProvider uriTestProvider
     */
    function testRequestUriConstructedProperly($path, $params, $full_uri, $end_string) {
        $this->assertSame($end_string, Services_Twilio::getRequestUri(
            $path, $params, $full_uri
        ));
    }

    function uriTestProvider() {
        return array(
            array('/2010-04-01/Accounts', array('FriendlyName' => 'hi'), false, 
                '/2010-04-01/Accounts.json?FriendlyName=hi'),
            array('/2010-04-01/Accounts', array(), false, 
                '/2010-04-01/Accounts.json'),
            array('/2010-04-01/Accounts.json', array(), true, 
                '/2010-04-01/Accounts.json'),
            array('/2010-04-01/Accounts.json', array('FriendlyName' => 'hi'), true, 
                '/2010-04-01/Accounts.json'),
            array('/2010-04-01/Accounts', array(
                'FriendlyName' => 'hi', 'foo' => 'bar'
            ), false, '/2010-04-01/Accounts.json?FriendlyName=hi&foo=bar'),
        );
    }

    function testNeedsRefining() {
        $http = $this->createMockHttp('', 'get', array(
                    'sid' => 'AC123',
                    'friendly_name' => '<NAME>',
                )
            );
        $client = $this->getClient($http);
        $this->assertEquals('AC123', $client->account->sid);
        $this->assertEquals('<NAME>', $client->account->friendly_name);
    }

    function testAccessSidAvoidsNetworkCall() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->never();
        $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
        $client->account->sid;
    }

    function testOnlyOneClientCreated() {
        $client = new Services_Twilio('AC123', '456');
        $client->account->client->sid = 'CL456';
        $this->assertSame('CL456', $client->account->sandbox->client->sid);
    }

    function testNullVersionReturnsNewest() {
        $client = new Services_Twilio('AC123', '123', null);
        $this->assertEquals('2010-04-01', $client->getVersion());
        $client = new Services_Twilio('AC123', '123', 'v1');
        $this->assertEquals('2010-04-01', $client->getVersion());
        $client = new Services_Twilio('AC123', '123', '2010-04-01');
        $this->assertEquals('2010-04-01', $client->getVersion());
        $client = new Services_Twilio('AC123', '123', '2008-08-01');
        $this->assertEquals('2008-08-01', $client->getVersion());
    }

    function testObjectLoadsOnlyOnce() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http = $this->createMockHttp('', 'get', array(
                    'sid' => 'AC123',
                    'friendly_name' => '<NAME>',
                    'status' => 'active',
                ));
        $client = $this->getClient($http);
        $client->account->friendly_name;
        $client->account->friendly_name;
        $client->account->status;
    }

    function testSubresourceLoad() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http = $this->createMockHttp('/Calls/CA123', 'get', 
            array('status' => 'Completed')
        );
        $client = $this->getClient($http);
        $this->assertEquals(
            'Completed',
            $client->account->calls->get('CA123')->status
        );
    }

    function testSubresourceSubresource() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http = $this->createMockHttp('/Calls/CA123/Notifications/NO123', 'get', 
            array('message_text' => 'Foo')
        );

        $client = $this->getClient($http);
        $notifs = $client->account->calls->get('CA123')->notifications;
        $this->assertEquals('Foo', $notifs->get('NO123')->message_text);
    }

    function testGetIteratorUsesFilters() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $params = array(
            'Page' => '0',
            'PageSize' => '10',
            'StartTime>' => '2012-07-06',
        );
        $response = array(
            'total' => 1,
            'calls' => array(array('status' => 'Completed', 'sid' => 'CA123'))
        );
        $http = $this->createMockHttp('/Calls', 'get', $response, $params);
        $client = $this->getClient($http);

        $iterator = $client->account->calls->getIterator(
            0, 10, array('StartTime>' => '2012-07-06'));
        foreach ($iterator as $call) {
            $this->assertEquals('Completed', $call->status);
            break;
        }
    }

    function testListResource() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->once()
            ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=0&PageSize=10')
            ->andReturn(array(200, array('Content-Type' => 'application/json'),
                json_encode(array(
                    'total' => 1,
                    'calls' => array(array('status' => 'completed', 'sid' => 'CA123'))
                ))
            ));
        $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
        $page = $client->account->calls->getPage(0, 10);
        $call = current($page->getItems());
        $this->assertEquals('completed', $call->status);
        $this->assertEquals(1, $page->total);
    }

    function testInstanceResourceUriConstructionFromList() {
        $http = m::mock(new Services_Twilio_TinyHttp);
        $http->shouldReceive('get')->once()
            ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=0&PageSize=10')
            ->andReturn(array(200, array('Content-Type' => 'application/json'),
                json_encode(array(
{'fraction_non_alphanumeric': 0.179386051403405, 'fraction_numerical': 0.05199500427266154, 'mean_word_length': 2.1557768097905, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 259, 'https://': 0, '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': '5246758', 'n_tokens_mistral': 5096, 'n_tokens_neox': 4462, 'n_words': 665}