<commit_before><commit_msg>:white_check_mark: Add TransferTest<commit_after><?php

namespace Harp\Transfer\Test\Model;

use Harp\Transfer\Test\Repo;
use Harp\Transfer\Test\AbstractTestCase;
use SebastianBergmann\Money\Money;
use SebastianBergmann\Money\Currency;
use Guzzle\Http\Client as HttpClient;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Omnipay\Dummy\Message\AuthorizeRequest;
use Omnipay\Dummy\Message\Response;

use DateTime;

/**
 * @coversDefaultClass Harp\Transfer\Model\AbstractTransfer
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2014, Clippings Ltd.
 * @license   http://spdx.org/licenses/BSD-3-Clause
 */
class AbstractTransferTest extends AbstractTestCase
{
    /**
     * @covers ::getCompletedAt
     * @covers ::setCompletedAt
     */
    public function testCompletedAt()
    {
        $basket = new Basket(['completedAt' => '2014-01-01 00:00:00']);

        $this->assertEquals(new DateTime('2014-01-01 00:00:00'), $basket->getCompletedAt());

        $basket = new Basket();

        $this->assertNull($basket->getCompletedAt());

        $basket->setCompletedAt(new DateTime('2014-02-01 00:00:00'));
        $this->assertEquals(new DateTime('2014-02-01 00:00:00'), $basket->getCompletedAt());
        $this->assertEquals('2014-02-01 00:00:00', $basket->completedAt);
    }

    /**
     * @covers ::isSent
     */
    public function testIsSent()
    {
        $basket = new Basket();

        $this->assertFalse($basket->isSent());

        $basket = new Basket(['response' => 'O:8:"stdClass":0:{}']);

        $this->assertTrue($basket->isSent());
    }

    /**
     * @covers ::getRequestParameters
     */
    public function testGetRequestParameters()
    {
        $basket = new Basket(['currency' => 'BGN', 'id' => 20]);
        $basket
            ->getItems()
                ->add(new ProductItem(['id' => 1, 'price' => 1000, 'isFrozen' => true, 'quantity' => 2]))
                ->add(new ProductItem(['id' => 2, 'price' => 2000, 'isFrozen' => true, 'quantity' => 3]));

        $expected = [
            'items' => [
                [
                    'name' => 1,
                    'description' => 'Item',
                    'price' => 10,
                    'quantity' => 2,
                ],
                [
                    'name' => 2,
                    'description' => 'Item',
                    'price' => 20,
                    'quantity' => 3,
                ]
            ],
            'amount' => 80,
            'currency' => 'BGN',
            'transactionReference' => 20,
            'cart' => ['test'],
        ];

        $params = $basket->getRequestParameters(['cart' => ['test']]);

        $this->assertEquals($expected, $params);
    }

    /**
     * @covers ::sendRequest
     */
    public function testSendRequest()
    {
        $request = $this->getMock(
            'Omnipay\Dummy\Message\AuthorizeRequest',
            ['send'],
            [new HttpClient(), new HttpRequest()]
        );

        $response = $this->getMock(
            'Omnipay\Dummy\Message\Response',
            ['isRedirect', 'isSuccessful'],
            [$request, []]
        );

        $request
            ->expects($this->exactly(3))
            ->method('send')
            ->will($this->returnValue($response));

        $response
            ->expects($this->exactly(3))
            ->method('isRedirect')
            ->will($this->onConsecutiveCalls(true, false, false));

        $response
            ->expects($this->exactly(2))
            ->method('isSuccessful')
            ->will($this->onConsecutiveCalls(false, true));

        $current = new DateTime();

        $basket = new Basket(['currency' => 'BGN', 'id' => 20]);

        $basket->sendRequest($request);
        $this->assertSame($response, $basket->response);
        $this->assertFalse($basket->isSuccessful);

        $this->assertTrue($current >= $basket->completedAt);

        $basket = new Basket(['currency' => 'BGN', 'id' => 20]);

        $basket->sendRequest($request);
        $this->assertSame($response, $basket->response);
        $this->assertFalse($basket->isSuccessful);

        $this->assertTrue($current >= $basket->completedAt);

        $basket = new Basket(['currency' => 'BGN', 'id' => 20]);

        $basket->sendRequest($request);
        $this->assertSame($response, $basket->response);
        $this->assertTrue($basket->isSuccessful);

        $this->assertTrue($current >= $basket->completedAt);
    }

    /**
     * @covers ::execute
     */
    public function testExecute()
    {
        $basket = $this->getMock(
            __NAMESPACE__.'\Basket',
            ['getRequestParameters', 'sendRequest', 'freeze']
        );

        $gateway = $this->getMock(
            'Omnipay\Dummy\Gateway',
            ['purchase']
        );

        $request = new AuthorizeRequest(new HttpClient(), new HttpRequest());
        $response = new Response($request, []);

        $basket
            ->expects($this->once())
            ->method('freeze')
            ->will($this->returnSelf());

        $basket
            ->expects($this->once())
            ->method('getRequestParameters')
            ->with($this->equalTo(['test' => 'test2']))
            ->will($this->returnValue(['test2' => 'test3']));

        $basket
            ->expects($this->once())
            ->method('sendRequest')
            ->with($this->identicalTo($request))
            ->will($this->returnValue($response));

        $gateway
            ->expects($this->once())
            ->method('purchase')
            ->with($this->equalTo(['test2' => 'test3']))
            ->will($this->returnValue($request));

        $basket->execute($gateway, 'purchase', ['test' => 'test2']);

        $this->assertTrue($basket->isSaved());
    }
}
{'fraction_non_alphanumeric': 0.18056749785038692, 'fraction_numerical': 0.020292347377472056, 'mean_word_length': 2.09197235513025, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 132, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3941068', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1686, 'n_words': 272}