<?php
error_reporting(E_ALL & ~E_STRICT);
class SqsTest extends PHPUnit_Framework_TestCase {

    protected function setUp() {
        $json = file_get_contents(dirname(__file__) . '/config/sqs.json');
        $config = json_decode($json);
        $this->_buster = new \Buster($config, true);
        $this->_buster->createQueue();
    }

    public function tearDown() {
        $this->_buster->deleteQueue();
    }

     /**
     * @expectedException Exception
     */
    public function testConfigurationMustBePresent() {
        $buster = new \Buster();
    }

     /**
     * @expectedException Exception
     */
    public function testConfigurationMustHaveOptions() {
        $options = new \stdClass();
        $buster = new \Buster($options);
    }

     /**
     * @expectedException Exception
     */
    public function testVendorCannotBeAString() {
        $this->_buster->send('1', array('test'));
    }

    /**
     * @expectedException Exception
     */
    public function testVendorCannotBeAnArray() {
        $this->_buster->send(array(1), array('test'));
    }

    /**
     * @expectedException Exception
     */
    public function testVendorCannotBeAnObject() {
        $vendor = new \stdClass();
        $this->_buster->send($vendor, array('test'));
    }

    /**
     * @expectedException Exception
     */
    public function testTagsCannotBeAnObject() {
        $tags = new \stdClass();
        $this->_buster->send(1, $tags);
    }

     /**
     * @expectedException Exception
     */
    public function testTagsCannotBeAnInteger() {
        $this->_buster->send(1, 1);
    }

     /**
     * @expectedException Exception
     */
    public function testTagsCannotBeAnString() {
        $tags = new \stdClass();
        $this->_buster->send(1, 'tag');
    }

    public function testClearReturnsAGuzzleModelObject() {
        $response = $this->_buster->send(1, array('tag_1', 'tag_2'));
        $this->assertEquals('Guzzle\Service\Resource\Model', get_class($response));
    }

    public function testClearReturnsAGuzzleModelWithMD5MessageBody() {
        $response = $this->_buster->send(1, array('tag_1', 'tag_2'));
        $this->assertNotNull($response->get('MD5OfMessageBody'));
    }

    public function testClearReturnsAGuzzleModelWithMessageId() {
        $response = $this->_buster->send(1, array('tag_1', 'tag_2'));
        $this->assertNotNull($response->get('MessageId'));
    }

    public function testMessageBodyIsWellFormed() {
        $response = $this->_buster->send(1, array('tag_1', 'tag_2'));
        $this->assertEquals($response->get('MD5OfMessageBody'), md5('{"vendor":1,"tags":["tag_1","tag_2"]}'));
    }

    public function testClearAccountingMessageBodyIsWellFormed() {
        $response = $this->_buster->clearAccounting(1);
        $this->assertEquals($response->get('MD5OfMessageBody'), md5('{"vendor":1,"tags":["salesbreakdown\/index","revenuehistory\/index","statement\/index"]}'));
    }

    public function testClearAnalyticsMessageBodyIsWellFormed() {
        $response = $this->_buster->clearAnalytics(1);
        $this->assertEquals($response->get('MD5OfMessageBody'), md5('{"vendor":1,"tags":["analytics\/index"]}'));
    }

}