<commit_before><?php

namespace Silktide\QueueBall\Sqs\Test;

use Silktide\QueueBall\Exception\QueueException;
use Silktide\QueueBall\Sqs\Queue;
use Aws\Sqs\SqsClient;
use Silktide\QueueBall\Message\QueueMessageFactoryInterface;
use Silktide\QueueBall\Message\QueueMessage;

/**
 *
 */
class QueueTest extends \PHPUnit_Framework_TestCase {

    /**
     * @var \Mockery\Mock|SqsClient
     */
    protected $sqsClient;

    /**
     * @var \Mockery\Mock|QueueMessageFactoryInterface
     */
    protected $messageFactory;

    /**
     * @var \Mockery\Mock|QueueMessage
     */
    protected $queueMessage;

    protected $queueUrl;

    protected $queueId = "queue";

    protected $receiptId = "receipt";

    public function setUp()
    {

        $this->queueUrl = "http://queue.com";
        /** @var \Mockery\Mock $urlReturn */
        $urlReturn = \Mockery::mock("Guzzle\\Service\\Resource\\Model");
        $urlReturn->shouldReceive("get")->with("QueueUrl")->andReturn($this->queueUrl)->getMock();

        $this->sqsClient = \Mockery::mock("Aws\\Sqs\\SqsClient");
        $this->sqsClient->shouldReceive("getQueueUrl")->andReturn($urlReturn);

        $this->queueMessage = \Mockery::mock("Silktide\\QueueBall\\Message\\QueueMessage");
        $this->queueMessage->shouldReceive(
            [
                "getQueueId" => $this->queueId,
                "getReceiptId" => $this->receiptId
            ]
        );

        $this->messageFactory = \Mockery::mock("Silktide\\QueueBall\\Message\\QueueMessageFactoryInterface");
    }

    public function testQueueId()
    {
        // no queueId
        $queue = new Queue($this->sqsClient, $this->messageFactory);

        try {
            $queue->getQueueId();
            $this->fail("Shouldn't be able to get a queue Id that hasn't been set");
        } catch (QueueException $e) {
            $this->assertEquals("No queue ID has been set", $e->getMessage());
        }

        $queue = new Queue($this->sqsClient, $this->messageFactory, $this->queueId);

        $this->assertEquals($this->queueId, $queue->getQueueId());
    }

    /**
     * @dataProvider timeoutProvider
     *
     * @param $timeout
     * @param $expectedTimeout
     */
    public function testCreateQueue($timeout, $expectedTimeout)
    {
        $queue = new Queue($this->sqsClient, $this->messageFactory);

        $expectedArg = [
            "QueueName" => $this->queueId,
            "Attributes" => [
                "VisibilityTimeout" => $expectedTimeout
            ]
        ];

        $this->sqsClient->shouldReceive("createQueue")->atLeast()->times(1)->with($expectedArg);

        $queue->createQueue($this->queueId, $timeout);
        $this->assertEquals($this->queueId, $queue->getQueueId());
    }

    public function timeoutProvider()
    {
        return [
            [ // explicit timeout
                30,
                30
            ],
            [ // default timeout
                null,
                Queue::DEFAULT_MESSAGE_LOCK_TIMEOUT
            ],
            [ // default when timeout is invalid
                "NAN",
                Queue::DEFAULT_MESSAGE_LOCK_TIMEOUT
            ]
        ];
    }

    public function testDeleteQueue()
    {
        $expectedArg = [
            "QueueUrl" => $this->queueUrl
        ];

        $this->sqsClient->shouldReceive("deleteQueue")->twice()->with($expectedArg);


        $queue = new Queue($this->sqsClient, $this->messageFactory);
        try {
            $queue->deleteQueue();
            $this->fail("should not be able to delete a queue without setting or passing a queue ID");
        } catch (QueueException $e) {
            $this->assertEquals("No queue ID has been set", $e->getMessage());
        }

        // test deleting, passing a queueId
        $queue->deleteQueue($this->queueId);
        $this->assertAttributeEquals($this->queueUrl, "queueUrl", $queue);

        // set the QueueId on the queue (check the URL is reset)
        $queue->setQueueId($this->queueId);
        $this->assertAttributeEquals(null, "queueUrl", $queue);

        // delete the queue implicitly
        $queue->deleteQueue();
        $this->assertAttributeEquals($this->queueUrl, "queueUrl", $queue);

    }

    public function testSendMessage()
    {
        $queue = new Queue($this->sqsClient, $this->messageFactory, $this->queueId);

        $message = "message";
        $expectedArg = [
            "QueueUrl" => $this->queueUrl,
            "MessageBody" => json_encode($message)
        ];
        $this->sqsClient->shouldReceive("sendMessage")->with($expectedArg)->once();
        $queue->sendMessage($message, $this->queueId);
    }

    public function testReceiveMessage()
    {
        $messageArray = [1, 2, 3];

        /** @var \Mockery\Mock $message */
        $message = \Mockery::mock("Guzzle\\Common\\Collection");
        $message->shouldReceive("toArray")->andReturn($messageArray)->getMock();
        $this->sqsClient->shouldReceive("receiveMessage")->with(["QueueUrl" => $this->queueUrl, "WaitTimeSeconds" => 0])->andReturn($message);
        $this->messageFactory->shouldReceive("createMessage")->with($messageArray, $this->queueId)->andReturn(true);

        $queue = new Queue($this->sqsClient, $this->messageFactory, $this->queueId);

        $this->assertTrue($queue->receiveMessage());
    }

    public function testCompleteMessage()
    {
        $expectedArg = [
            "QueueUrl" => $this->queueUrl,
            "ReceiptHandle" => $this->receiptId
        ];
        $this->sqsClient->shouldReceive("deleteMessage")->with($expectedArg)->once();

        $queue = new Queue($this->sqsClient, $this->messageFactory);
        $queue->completeMessage($this->queueMessage);
    }

    public function testReturnMessage()
    {<commit_msg>Fixed test for the new createQueue method signature - message lock timeout is now sent in the options array<commit_after><?php

namespace Silktide\QueueBall\Sqs\Test;

use Silktide\QueueBall\Exception\QueueException;
use Silktide\QueueBall\Sqs\Queue;
use Aws\Sqs\SqsClient;
use Silktide\QueueBall\Message\QueueMessageFactoryInterface;
use Silktide\QueueBall\Message\QueueMessage;

/**
 *
 */
class QueueTest extends \PHPUnit_Framework_TestCase {

    /**
     * @var \Mockery\Mock|SqsClient
     */
    protected $sqsClient;

    /**
     * @var \Mockery\Mock|QueueMessageFactoryInterface
     */
    protected $messageFactory;

    /**
     * @var \Mockery\Mock|QueueMessage
     */
    protected $queueMessage;

    protected $queueUrl;

    protected $queueId = "queue";

    protected $receiptId = "receipt";

    public function setUp()
    {

        $this->queueUrl = "http://queue.com";
        /** @var \Mockery\Mock $urlReturn */
        $urlReturn = \Mockery::mock("Guzzle\\Service\\Resource\\Model");
        $urlReturn->shouldReceive("get")->with("QueueUrl")->andReturn($this->queueUrl)->getMock();

        $this->sqsClient = \Mockery::mock("Aws\\Sqs\\SqsClient");
        $this->sqsClient->shouldReceive("getQueueUrl")->andReturn($urlReturn);

        $this->queueMessage = \Mockery::mock("Silktide\\QueueBall\\Message\\QueueMessage");
        $this->queueMessage->shouldReceive(
            [
                "getQueueId" => $this->queueId,
                "getReceiptId" => $this->receiptId
            ]
        );

        $this->messageFactory = \Mockery::mock("Silktide\\QueueBall\\Message\\QueueMessageFactoryInterface");
    }

    public function testQueueId()
    {
        // no queueId
        $queue = new Queue($this->sqsClient, $this->messageFactory);

        try {
            $queue->getQueueId();
            $this->fail("Shouldn't be able to get a queue Id that hasn't been set");
        } catch (QueueException $e) {
            $this->assertEquals("No queue ID has been set", $e->getMessage());
        }

        $queue = new Queue($this->sqsClient, $this->messageFactory, $this->queueId);

        $this->assertEquals($this->queueId, $queue->getQueueId());
    }

    /**
     * @dataProvider timeoutProvider
     *
     * @param $timeout
     * @param $expectedTimeout
     */
    public function testCreateQueue($timeout, $expectedTimeout)
    {
        $queue = new Queue($this->sqsClient, $this->messageFactory);

        $expectedArg = [
            "QueueName" => $this->queueId,
            "Attributes" => [
                "VisibilityTimeout" => $expectedTimeout
            ]
        ];

        $this->sqsClient->shouldReceive("createQueue")->atLeast()->times(1)->with($expectedArg);

        $queue->createQueue($this->queueId, ["messageLockTimeout" => $timeout]);
        $this->assertEquals($this->queueId, $queue->getQueueId());
    }

    public function timeoutProvider()
    {
        return [
            [ // explicit timeout
                30,
                30
            ],
            [ // default timeout
                null,
                Queue::DEFAULT_MESSAGE_LOCK_TIMEOUT
            ],
            [ // default when timeout is invalid
                "NAN",
                Queue::DEFAULT_MESSAGE_LOCK_TIMEOUT
            ]
        ];
    }

    public function testDeleteQueue()
    {
        $expectedArg = [
            "QueueUrl" => $this->queueUrl
        ];

        $this->sqsClient->shouldReceive("deleteQueue")->twice()->with($expectedArg);


        $queue = new Queue($this->sqsClient, $this->messageFactory);
        try {
            $queue->deleteQueue();
            $this->fail("should not be able to delete a queue without setting or passing a queue ID");
        } catch (QueueException $e) {
            $this->assertEquals("No queue ID has been set", $e->getMessage());
        }

        // test deleting, passing a queueId
        $queue->deleteQueue($this->queueId);
        $this->assertAttributeEquals($this->queueUrl, "queueUrl", $queue);

        // set the QueueId on the queue (check the URL is reset)
        $queue->setQueueId($this->queueId);
        $this->assertAttributeEquals(null, "queueUrl", $queue);

        // delete the queue implicitly
        $queue->deleteQueue();
        $this->assertAttributeEquals($this->queueUrl, "queueUrl", $queue);

    }

    public function testSendMessage()
    {
        $queue = new Queue($this->sqsClient, $this->messageFactory, $this->queueId);

        $message = "message";
        $expectedArg = [
            "QueueUrl" => $this->queueUrl,
            "MessageBody" => json_encode($message)
        ];
        $this->sqsClient->shouldReceive("sendMessage")->with($expectedArg)->once();
        $queue->sendMessage($message, $this->queueId);
    }

    public function testReceiveMessage()
    {
        $messageArray = [1, 2, 3];

        /** @var \Mockery\Mock $message */
        $message = \Mockery::mock("Guzzle\\Common\\Collection");
        $message->shouldReceive("toArray")->andReturn($messageArray)->getMock();
        $this->sqsClient->shouldReceive("receiveMessage")->with(["QueueUrl" => $this->queueUrl, "WaitTimeSeconds" => 20])->andReturn($message);
        $this->messageFactory->shouldReceive("createMessage")->with($messageArray, $this->queueId)->andReturn(true);

        $queue = new Queue($this->sqsClient, $this->messageFactory, $this->queueId);

        $this->assertTrue($queue->receiveMessage());
    }

    public function testCompleteMessage()
    {
        $expectedArg = [
            "QueueUrl" => $this->queueUrl,
            "ReceiptHandle" => $this->receiptId
        ];
        $this->sqsClient->shouldReceive("deleteMessage")->with($expectedArg)->once();

        $queue = new Queue($this->sqsClient, $this->messageFactory);
        $queue->completeMessage($this->queueMessage);
    }

    public function testReturnMessage()
    {
{'fraction_non_alphanumeric': 0.15294719781625862, 'fraction_numerical': 0.0016207455429497568, 'mean_word_length': 2.4240654205607477, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 228, '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': '6674773', 'n_tokens_mistral': 3411, 'n_tokens_neox': 3255, 'n_words': 566}