<?php

namespace Slack\Test;

use Slack\Client;
use Slack\Notifier;

use Slack\Message\Message;

/**
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-02-23 at 15:45:46.
 */
class NotifierTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

    /**
     * @covers Slack\Notifier::notify
     */
    public function testNotify()
    {
        $client = $this->getMockBuilder('\Slack\Client')
            ->disableOriginalConstructor()
            ->getMock(array('post'));

        $request = $this->getMockBuilder('\Guzzle\Http\Message\Request')
            ->disableOriginalConstructor()
            ->getMock(array('send'));

        $message = new Message('Hello world');

        $message->setChannel('#test')
            ->setIconEmoji(':ghost:')
            ->setUsername('slack-php');

        $attachment = new \Slack\Message\MessageAttachment();
        $field = new \Slack\Message\MessageField();
        $field
            ->setTitle('foo')
            ->setValue('bar')
            ->setShort(false);

        $attachment->addField($field);
        $message->addAttachment($attachment);


        $expectedDatas = json_encode(
            array(
                'text'       => $message->getText(),
                'channel'    => $message->getChannel(),
                'username'   => 'slack-php',
                'icon_emoji' => ':ghost:',
                'attachments' => array(
                    array(
                        'fields' => array(
                            array(
                                'title' => 'foo',
                                'value' => 'bar',
                                'short' => false
                            )
                        )
                    )
                )
            )
        );

        $client->expects($this->once())
            ->method('post')
            ->with(
                $this->equalTo('/services/hooks/incoming-webhook'),
                $this->anything(),
                $this->equalTo($expectedDatas),
                $this->anything()
            )
            ->will($this->returnValue($request));

        $notifier = new Notifier($client);

        $notifier->notify($message);
    }
}
