<?php
//
// WARNING: Do not edit by hand, this file was generated by Crank:
// https://github.com/gocardless/crank
//

namespace GoCardlessPro\Integration;

class SubscriptionsIntegrationTest extends IntegrationTestBase
{
    public function testResourceModelExists()
    {
        $obj = new \GoCardlessPro\Resources\Subscription(array());
        $this->assertNotNull($obj);
    }
    
    public function testSubscriptionsCreate()
    {
        $fixture = $this->loadJsonFixture('subscriptions')->create;
        $this->stub_request($fixture);

        $service = $this->client->subscriptions();
        $response = call_user_func_array(array($service, 'create'), (array)$fixture->url_params);

        $body = $fixture->body->subscriptions;
    
        $this->assertInstanceOf('\GoCardlessPro\Resources\Subscription', $response);

        $this->assertEquals($body->amount, $response->amount);
        $this->assertEquals($body->app_fee, $response->app_fee);
        $this->assertEquals($body->count, $response->count);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->currency, $response->currency);
        $this->assertEquals($body->day_of_month, $response->day_of_month);
        $this->assertEquals($body->earliest_charge_date_after_resume, $response->earliest_charge_date_after_resume);
        $this->assertEquals($body->end_date, $response->end_date);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->interval, $response->interval);
        $this->assertEquals($body->interval_unit, $response->interval_unit);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->metadata, $response->metadata);
        $this->assertEquals($body->month, $response->month);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->parent_plan_paused, $response->parent_plan_paused);
        $this->assertEquals($body->payment_reference, $response->payment_reference);
        $this->assertEquals($body->retry_if_possible, $response->retry_if_possible);
        $this->assertEquals($body->start_date, $response->start_date);
        $this->assertEquals($body->status, $response->status);
        $this->assertEquals($body->upcoming_payments, $response->upcoming_payments);
    

        $expectedPathRegex = $this->extract_resource_fixture_path_regex($fixture);
        $dispatchedRequest = $this->history[0]['request'];
        $this->assertMatchesRegularExpression($expectedPathRegex, $dispatchedRequest->getUri()->getPath());
    }

    public function testSubscriptionsCreateWithIdempotencyConflict()
    {
        $fixture = $this->loadJsonFixture('subscriptions')->create;

        $idempotencyConflictResponseFixture = $this->loadFixture('idempotent_creation_conflict_invalid_state_error');

        // The POST request responds with a 409 to our original POST, due to an idempotency conflict
        $this->mock->append(new \GuzzleHttp\Psr7\Response(409, [], $idempotencyConflictResponseFixture));

        // The client makes a second request to fetch the resource that was already
        // created using our idempotency key. It responds with the created resource,
        // which looks just like the response for a successful POST request.
        $this->mock->append(new \GuzzleHttp\Psr7\Response(200, [], json_encode($fixture->body)));

        $service = $this->client->subscriptions();
        $response = call_user_func_array(array($service, 'create'), (array)$fixture->url_params);
        $body = $fixture->body->subscriptions;

        $this->assertInstanceOf('\GoCardlessPro\Resources\Subscription', $response);

        $this->assertEquals($body->amount, $response->amount);
        $this->assertEquals($body->app_fee, $response->app_fee);
        $this->assertEquals($body->count, $response->count);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->currency, $response->currency);
        $this->assertEquals($body->day_of_month, $response->day_of_month);
        $this->assertEquals($body->earliest_charge_date_after_resume, $response->earliest_charge_date_after_resume);
        $this->assertEquals($body->end_date, $response->end_date);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->interval, $response->interval);
        $this->assertEquals($body->interval_unit, $response->interval_unit);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->metadata, $response->metadata);
        $this->assertEquals($body->month, $response->month);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->parent_plan_paused, $response->parent_plan_paused);
        $this->assertEquals($body->payment_reference, $response->payment_reference);
        $this->assertEquals($body->retry_if_possible, $response->retry_if_possible);
        $this->assertEquals($body->start_date, $response->start_date);
        $this->assertEquals($body->status, $response->status);
        $this->assertEquals($body->upcoming_payments, $response->upcoming_payments);
        

        $expectedPathRegex = $this->extract_resource_fixture_path_regex($fixture);
        $conflictRequest = $this->history[0]['request'];
        $this->assertMatchesRegularExpression($expectedPathRegex, $conflictRequest->getUri()->getPath());
        $getRequest = $this->history[1]['request'];
        $this->assertEquals($getRequest->getUri()->getPath(), '/subscriptions/ID123');
    }
    
    public function testSubscriptionsList()
    {
        $fixture = $this->loadJsonFixture('subscriptions')->list;
        $this->stub_request($fixture);

        $service = $this->client->subscriptions();
        $response = call_user_func_array(array($service, 'list'), (array)$fixture->url_params);

        $body = $fixture->body->subscriptions;
    
        $records = $response->records;
        $this->assertInstanceOf('\GoCardlessPro\Core\ListResponse', $response);
        $this->assertInstanceOf('\GoCardlessPro\Resources\Subscription', $records[0]);
        if (!is_null($fixture->body) && property_exists($fixture->body, 'meta') && !is_null($fixture->body->meta)) {
            $this->assertEquals($fixture->body->meta->cursors->before, $response->before);
            $this->assertEquals($fixture->body->meta->cursors->after, $response->after);
        }
    

    
        foreach (range(0, count($body) - 1) as $num) {
            $record = $records[$num];
            
            if (isset($body[$num]->amount)) {
                $this->assertEquals($body[$num]->amount, $record->amount);
            }
            
            if (isset($body[$num]->app_fee)) {
                $this->assertEquals($body[$num]->app_fee, $record->app_fee);
            }
            
            if (isset($body[$num]->count)) {
                $this->assertEquals($body[$num]->count, $record->count);
            }
            
            if (isset($body[$num]->created_at)) {
                $this->assertEquals($body[$num]->created_at, $record->created_at);
            }
            
            if (isset($body[$num]->currency)) {
                $this->assertEquals($body[$num]->currency, $record->currency);
            }
            
            if (isset($body[$num]->day_of_month)) {
                $this->assertEquals($body[$num]->day_of_month, $record->day_of_month);
            }
            
            if (isset($body[$num]->earliest_charge_date_after_resume)) {
                $this->assertEquals($body[$num]->earliest_charge_date_after_resume, $record->earliest_charge_date_after_resume);
            }
            
            if (isset($body[$num]->end_date)) {
                $this->assertEquals($body[$num]->end_date, $record->end_date);
            }
            
            if (isset($body[$num]->id)) {
                $this->assertEquals($body[$num]->id, $record->id);
            }
            
            if (isset($body[$num]->interval)) {
                $this->assertEquals($body[$num]->interval, $record->interval);
            }
            
            if (isset($body[$num]->interval_unit)) {
                $this->assertEquals($body[$num]->interval_unit, $record->interval_unit);
            }
            
            if (isset($body[$num]->links)) {
                $this->assertEquals($body[$num]->links, $record->links);
            }
            
            if (isset($body[$num]->metadata)) {
                $this->assertEquals($body[$num]->metadata, $record->metadata);
            }
            
            if (isset($body[$num]->month)) {
                $this->assertEquals($body[$num]->month, $record->month);
            }
            
            if (isset($body[$num]->name)) {
                $this->assertEquals($body[$num]->name, $record->name);
            }
            
            if (isset($body[$num]->parent_plan_paused)) {
                $this->assertEquals($body[$num]->parent_plan_paused, $record->parent_plan_paused);
            }
            
            if (isset($body[$num]->payment_reference)) {
                $this->assertEquals($body[$num]->payment_reference, $record->payment_reference);
            }
            
            if (isset($body[$num]->retry_if_possible)) {
                $this->assertEquals($body[$num]->retry_if_possible, $record->retry_if_possible);
            }
            
            if (isset($body[$num]->start_date)) {
                $this->assertEquals($body[$num]->start_date, $record->start_date);
            }
            
            if (isset($body[$num]->status)) {
                $this->assertEquals($body[$num]->status, $record->status);
            }
            
            if (isset($body[$num]->upcoming_payments)) {
                $this->assertEquals($body[$num]->upcoming_payments, $record->upcoming_payments);
            }
            
        }

        $expectedPathRegex = $this->extract_resource_fixture_path_regex($fixture);
        $dispatchedRequest = $this->history[0]['request'];
        $this->assertMatchesRegularExpression($expectedPathRegex, $dispatchedRequest->getUri()->getPath());
    }

    
    public function testSubscriptionsGet()
    {
        $fixture = $this->loadJsonFixture('subscriptions')->get;
        $this->stub_request($fixture);

        $service = $this->client->subscriptions();
        $response = call_user_func_array(array($service, 'get'), (array)$fixture->url_params);

        $body = $fixture->body->subscriptions;
    
        $this->assertInstanceOf('\GoCardlessPro\Resources\Subscription', $response);

        $this->assertEquals($body->amount, $response->amount);
        $this->assertEquals($body->app_fee, $response->app_fee);
        $this->assertEquals($body->count, $response->count);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->currency, $response->currency);
        $this->assertEquals($body->day_of_month, $response->day_of_month);
        $this->assertEquals($body->earliest_charge_date_after_resume, $response->earliest_charge_date_after_resume);
        $this->assertEquals($body->end_date, $response->end_date);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->interval, $response->interval);
        $this->assertEquals($body->interval_unit, $response->interval_unit);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->metadata, $response->metadata);
        $this->assertEquals($body->month, $response->month);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->parent_plan_paused, $response->parent_plan_paused);
        $this->assertEquals($body->payment_reference, $response->payment_reference);
        $this->assertEquals($body->retry_if_possible, $response->retry_if_possible);
        $this->assertEquals($body->start_date, $response->start_date);
        $this->assertEquals($body->status, $response->status);
        $this->assertEquals($body->upcoming_payments, $response->upcoming_payments);
    

        $expectedPathRegex = $this->extract_resource_fixture_path_regex($fixture);
        $dispatchedRequest = $this->history[0]['request'];
        $this->assertMatchesRegularExpression($expectedPathRegex, $dispatchedRequest->getUri()->getPath());
    }

    
    public function testSubscriptionsUpdate()
    {
        $fixture = $this->loadJsonFixture('subscriptions')->update;
        $this->stub_request($fixture);

        $service = $this->client->subscriptions();
        $response = call_user_func_array(array($service, 'update'), (array)$fixture->url_params);

        $body = $fixture->body->subscriptions;
    
        $this->assertInstanceOf('\GoCardlessPro\Resources\Subscription', $response);

        $this->assertEquals($body->amount, $response->amount);
        $this->assertEquals($body->app_fee, $response->app_fee);
        $this->assertEquals($body->count, $response->count);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->currency, $response->currency);
        $this->assertEquals($body->day_of_month, $response->day_of_month);
        $this->assertEquals($body->earliest_charge_date_after_resume, $response->earliest_charge_date_after_resume);
        $this->assertEquals($body->end_date, $response->end_date);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->interval, $response->interval);
        $this->assertEquals($body->interval_unit, $response->interval_unit);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->metadata, $response->metadata);
        $this->assertEquals($body->month, $response->month);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->parent_plan_paused, $response->parent_plan_paused);
        $this->assertEquals($body->payment_reference, $response->payment_reference);
        $this->assertEquals($body->retry_if_possible, $response->retry_if_possible);
        $this->assertEquals($body->start_date, $response->start_date);
        $this->assertEquals($body->status, $response->status);
        $this->assertEquals($body->upcoming_payments, $response->upcoming_payments);
    

        $expectedPathRegex = $this->extract_resource_fixture_path_regex($fixture);
        $dispatchedRequest = $this->history[0]['request'];
        $this->assertMatchesRegularExpression($expectedPathRegex, $dispatchedRequest->getUri()->getPath());
    }

    
    public function testSubscriptionsPause()
    {
        $fixture = $this->loadJsonFixture('subscriptions')->pause;
        $this->stub_request($fixture);

        $service = $this->client->subscriptions();
        $response = call_user_func_array(array($service, 'pause'), (array)$fixture->url_params);

        $body = $fixture->body->subscriptions;
    
        $this->assertInstanceOf('\GoCardlessPro\Resources\Subscription', $response);

        $this->assertEquals($body->amount, $response->amount);
        $this->assertEquals($body->app_fee, $response->app_fee);
        $this->assertEquals($body->count, $response->count);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->currency, $response->currency);
        $this->assertEquals($body->day_of_month, $response->day_of_month);
        $this->assertEquals($body->earliest_charge_date_after_resume, $response->earliest_charge_date_after_resume);
        $this->assertEquals($body->end_date, $response->end_date);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->interval, $response->interval);
        $this->assertEquals($body->interval_unit, $response->interval_unit);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->metadata, $response->metadata);
        $this->assertEquals($body->month, $response->month);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->parent_plan_paused, $response->parent_plan_paused);
        $this->assertEquals($body->payment_reference, $response->payment_reference);
        $this->assertEquals($body->retry_if_possible, $response->retry_if_possible);
        $this->assertEquals($body->start_date, $response->start_date);
        $this->assertEquals($body->status, $response->status);
        $this->assertEquals($body->upcoming_payments, $response->upcoming_payments);
    

        $expectedPathRegex = $this->extract_resource_fixture_path_regex($fixture);
        $dispatchedRequest = $this->history[0]['request'];
        $this->assertMatchesRegularExpression($expectedPathRegex, $dispatchedRequest->getUri()->getPath());
    }

    
    public function testSubscriptionsResume()
    {
        $fixture = $this->loadJsonFixture('subscriptions')->resume;
        $this->stub_request($fixture);

        $service = $this->client->subscriptions();
        $response = call_user_func_array(array($service, 'resume'), (array)$fixture->url_params);

        $body = $fixture->body->subscriptions;
    
        $this->assertInstanceOf('\GoCardlessPro\Resources\Subscription', $response);

        $this->assertEquals($body->amount, $response->amount);
        $this->assertEquals($body->app_fee, $response->app_fee);
        $this->assertEquals($body->count, $response->count);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->currency, $response->currency);
        $this->assertEquals($body->day_of_month, $response->day_of_month);
        $this->assertEquals($body->earliest_charge_date_after_resume, $response->earliest_charge_date_after_resume);
        $this->assertEquals($body->end_date, $response->end_date);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->interval, $response->interval);
        $this->assertEquals($body->interval_unit, $response->interval_unit);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->metadata, $response->metadata);
        $this->assertEquals($body->month, $response->month);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->parent_plan_paused, $response->parent_plan_paused);
        $this->assertEquals($body->payment_reference, $response->payment_reference);
        $this->assertEquals($body->retry_if_possible, $response->retry_if_possible);
        $this->assertEquals($body->start_date, $response->start_date);
        $this->assertEquals($body->status, $response->status);
        $this->assertEquals($body->upcoming_payments, $response->upcoming_payments);
    

        $expectedPathRegex = $this->extract_resource_fixture_path_regex($fixture);
        $dispatchedRequest = $this->history[0]['request'];
        $this->assertMatchesRegularExpression($expectedPathRegex, $dispatchedRequest->getUri()->getPath());
    }

    
    public function testSubscriptionsCancel()
    {
        $fixture = $this->loadJsonFixture('subscriptions')->cancel;
        $this->stub_request($fixture);

        $service = $this->client->subscriptions();
        $response = call_user_func_array(array($service, 'cancel'), (array)$fixture->url_params);

        $body = $fixture->body->subscriptions;
    
        $this->assertInstanceOf('\GoCardlessPro\Resources\Subscription', $response);

        $this->assertEquals($body->amount, $response->amount);
        $this->assertEquals($body->app_fee, $response->app_fee);
        $this->assertEquals($body->count, $response->count);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->currency, $response->currency);
        $this->assertEquals($body->day_of_month, $response->day_of_month);
        $this->assertEquals($body->earliest_charge_date_after_resume, $response->earliest_charge_date_after_resume);
        $this->assertEquals($body->end_date, $response->end_date);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->interval, $response->interval);
        $this->assertEquals($body->interval_unit, $response->interval_unit);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->metadata, $response->metadata);
        $this->assertEquals($body->month, $response->month);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->parent_plan_paused, $response->parent_plan_paused);
        $this->assertEquals($body->payment_reference, $response->payment_reference);
        $this->assertEquals($body->retry_if_possible, $response->retry_if_possible);
        $this->assertEquals($body->start_date, $response->start_date);
        $this->assertEquals($body->status, $response->status);
        $this->assertEquals($body->upcoming_payments, $response->upcoming_payments);
    

        $expectedPathRegex = $this->extract_resource_fixture_path_regex($fixture);
        $dispatchedRequest = $this->history[0]['request'];
        $this->assertMatchesRegularExpression($expectedPathRegex, $dispatchedRequest->getUri()->getPath());
    }

    
}
