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

namespace GoCardlessPro\Integration;

class CreditorsIntegrationTest extends IntegrationTestBase
{
    public function testResourceModelExists()
    {
        $obj = new \GoCardlessPro\Resources\Creditor(array());
        $this->assertNotNull($obj);
    }
    
    public function testCreditorsCreate()
    {
        $fixture = $this->loadJsonFixture('creditors')->create;
        $this->stub_request($fixture);

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

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

        $this->assertEquals($body->address_line1, $response->address_line1);
        $this->assertEquals($body->address_line2, $response->address_line2);
        $this->assertEquals($body->address_line3, $response->address_line3);
        $this->assertEquals($body->bank_reference_prefix, $response->bank_reference_prefix);
        $this->assertEquals($body->can_create_refunds, $response->can_create_refunds);
        $this->assertEquals($body->city, $response->city);
        $this->assertEquals($body->country_code, $response->country_code);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->creditor_type, $response->creditor_type);
        $this->assertEquals($body->custom_payment_pages_enabled, $response->custom_payment_pages_enabled);
        $this->assertEquals($body->fx_payout_currency, $response->fx_payout_currency);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->logo_url, $response->logo_url);
        $this->assertEquals($body->mandate_imports_enabled, $response->mandate_imports_enabled);
        $this->assertEquals($body->merchant_responsible_for_notifications, $response->merchant_responsible_for_notifications);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->postal_code, $response->postal_code);
        $this->assertEquals($body->region, $response->region);
        $this->assertEquals($body->scheme_identifiers, $response->scheme_identifiers);
        $this->assertEquals($body->verification_status, $response->verification_status);
    

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

    public function testCreditorsCreateWithIdempotencyConflict()
    {
        $fixture = $this->loadJsonFixture('creditors')->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->creditors();
        $response = call_user_func_array(array($service, 'create'), (array)$fixture->url_params);
        $body = $fixture->body->creditors;

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

        $this->assertEquals($body->address_line1, $response->address_line1);
        $this->assertEquals($body->address_line2, $response->address_line2);
        $this->assertEquals($body->address_line3, $response->address_line3);
        $this->assertEquals($body->bank_reference_prefix, $response->bank_reference_prefix);
        $this->assertEquals($body->can_create_refunds, $response->can_create_refunds);
        $this->assertEquals($body->city, $response->city);
        $this->assertEquals($body->country_code, $response->country_code);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->creditor_type, $response->creditor_type);
        $this->assertEquals($body->custom_payment_pages_enabled, $response->custom_payment_pages_enabled);
        $this->assertEquals($body->fx_payout_currency, $response->fx_payout_currency);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->logo_url, $response->logo_url);
        $this->assertEquals($body->mandate_imports_enabled, $response->mandate_imports_enabled);
        $this->assertEquals($body->merchant_responsible_for_notifications, $response->merchant_responsible_for_notifications);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->postal_code, $response->postal_code);
        $this->assertEquals($body->region, $response->region);
        $this->assertEquals($body->scheme_identifiers, $response->scheme_identifiers);
        $this->assertEquals($body->verification_status, $response->verification_status);
        

        $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(), '/creditors/ID123');
    }
    
    public function testCreditorsList()
    {
        $fixture = $this->loadJsonFixture('creditors')->list;
        $this->stub_request($fixture);

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

        $body = $fixture->body->creditors;
    
        $records = $response->records;
        $this->assertInstanceOf('\GoCardlessPro\Core\ListResponse', $response);
        $this->assertInstanceOf('\GoCardlessPro\Resources\Creditor', $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]->address_line1)) {
                $this->assertEquals($body[$num]->address_line1, $record->address_line1);
            }
            
            if (isset($body[$num]->address_line2)) {
                $this->assertEquals($body[$num]->address_line2, $record->address_line2);
            }
            
            if (isset($body[$num]->address_line3)) {
                $this->assertEquals($body[$num]->address_line3, $record->address_line3);
            }
            
            if (isset($body[$num]->bank_reference_prefix)) {
                $this->assertEquals($body[$num]->bank_reference_prefix, $record->bank_reference_prefix);
            }
            
            if (isset($body[$num]->can_create_refunds)) {
                $this->assertEquals($body[$num]->can_create_refunds, $record->can_create_refunds);
            }
            
            if (isset($body[$num]->city)) {
                $this->assertEquals($body[$num]->city, $record->city);
            }
            
            if (isset($body[$num]->country_code)) {
                $this->assertEquals($body[$num]->country_code, $record->country_code);
            }
            
            if (isset($body[$num]->created_at)) {
                $this->assertEquals($body[$num]->created_at, $record->created_at);
            }
            
            if (isset($body[$num]->creditor_type)) {
                $this->assertEquals($body[$num]->creditor_type, $record->creditor_type);
            }
            
            if (isset($body[$num]->custom_payment_pages_enabled)) {
                $this->assertEquals($body[$num]->custom_payment_pages_enabled, $record->custom_payment_pages_enabled);
            }
            
            if (isset($body[$num]->fx_payout_currency)) {
                $this->assertEquals($body[$num]->fx_payout_currency, $record->fx_payout_currency);
            }
            
            if (isset($body[$num]->id)) {
                $this->assertEquals($body[$num]->id, $record->id);
            }
            
            if (isset($body[$num]->links)) {
                $this->assertEquals($body[$num]->links, $record->links);
            }
            
            if (isset($body[$num]->logo_url)) {
                $this->assertEquals($body[$num]->logo_url, $record->logo_url);
            }
            
            if (isset($body[$num]->mandate_imports_enabled)) {
                $this->assertEquals($body[$num]->mandate_imports_enabled, $record->mandate_imports_enabled);
            }
            
            if (isset($body[$num]->merchant_responsible_for_notifications)) {
                $this->assertEquals($body[$num]->merchant_responsible_for_notifications, $record->merchant_responsible_for_notifications);
            }
            
            if (isset($body[$num]->name)) {
                $this->assertEquals($body[$num]->name, $record->name);
            }
            
            if (isset($body[$num]->postal_code)) {
                $this->assertEquals($body[$num]->postal_code, $record->postal_code);
            }
            
            if (isset($body[$num]->region)) {
                $this->assertEquals($body[$num]->region, $record->region);
            }
            
            if (isset($body[$num]->scheme_identifiers)) {
                $this->assertEquals($body[$num]->scheme_identifiers, $record->scheme_identifiers);
            }
            
            if (isset($body[$num]->verification_status)) {
                $this->assertEquals($body[$num]->verification_status, $record->verification_status);
            }
            
        }

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

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

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

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

        $this->assertEquals($body->address_line1, $response->address_line1);
        $this->assertEquals($body->address_line2, $response->address_line2);
        $this->assertEquals($body->address_line3, $response->address_line3);
        $this->assertEquals($body->bank_reference_prefix, $response->bank_reference_prefix);
        $this->assertEquals($body->can_create_refunds, $response->can_create_refunds);
        $this->assertEquals($body->city, $response->city);
        $this->assertEquals($body->country_code, $response->country_code);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->creditor_type, $response->creditor_type);
        $this->assertEquals($body->custom_payment_pages_enabled, $response->custom_payment_pages_enabled);
        $this->assertEquals($body->fx_payout_currency, $response->fx_payout_currency);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->logo_url, $response->logo_url);
        $this->assertEquals($body->mandate_imports_enabled, $response->mandate_imports_enabled);
        $this->assertEquals($body->merchant_responsible_for_notifications, $response->merchant_responsible_for_notifications);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->postal_code, $response->postal_code);
        $this->assertEquals($body->region, $response->region);
        $this->assertEquals($body->scheme_identifiers, $response->scheme_identifiers);
        $this->assertEquals($body->verification_status, $response->verification_status);
    

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

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

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

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

        $this->assertEquals($body->address_line1, $response->address_line1);
        $this->assertEquals($body->address_line2, $response->address_line2);
        $this->assertEquals($body->address_line3, $response->address_line3);
        $this->assertEquals($body->bank_reference_prefix, $response->bank_reference_prefix);
        $this->assertEquals($body->can_create_refunds, $response->can_create_refunds);
        $this->assertEquals($body->city, $response->city);
        $this->assertEquals($body->country_code, $response->country_code);
        $this->assertEquals($body->created_at, $response->created_at);
        $this->assertEquals($body->creditor_type, $response->creditor_type);
        $this->assertEquals($body->custom_payment_pages_enabled, $response->custom_payment_pages_enabled);
        $this->assertEquals($body->fx_payout_currency, $response->fx_payout_currency);
        $this->assertEquals($body->id, $response->id);
        $this->assertEquals($body->links, $response->links);
        $this->assertEquals($body->logo_url, $response->logo_url);
        $this->assertEquals($body->mandate_imports_enabled, $response->mandate_imports_enabled);
        $this->assertEquals($body->merchant_responsible_for_notifications, $response->merchant_responsible_for_notifications);
        $this->assertEquals($body->name, $response->name);
        $this->assertEquals($body->postal_code, $response->postal_code);
        $this->assertEquals($body->region, $response->region);
        $this->assertEquals($body->scheme_identifiers, $response->scheme_identifiers);
        $this->assertEquals($body->verification_status, $response->verification_status);
    

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

    
}
