<?php

namespace KupShop\OrderingBundle\Tests;

use Query\Operator;

class EmailBounceTest extends \DatabaseTestCase
{
    public function testHandleBounce()
    {
        $this->updateOrderDateCreated(4);

        $client = $this->createClient();
        $client->request('POST', '/_wpj/bounce/', [], [], [], $this->getEmail());
        $response = $client->getResponse();

        $this->assertEquals(200, $response->getStatusCode());
        $this->assertTrue($this->checkOrderFlagExist(4));
        $this->assertEquals($this->getEmail(), $response->getContent());
    }

    private function checkOrderFlagExist($orderId)
    {
        $order = sqlQueryBuilder()->select('id')
            ->from('orders')
            ->where(Operator::equals(['id' => $orderId]))
            ->andWhere('FIND_IN_SET("NE", flags)>0')
            ->execute()->fetch();

        if ($order) {
            return true;
        }

        return false;
    }

    private function updateOrderDateCreated($orderId)
    {
        sqlQuery('UPDATE orders SET date_created = NOW() WHERE id = :id', ['id' => $orderId]);
    }

    private function getEmail()
    {
        return file_get_contents(__DIR__.'/EmailBounceTest.txt');
    }

    protected function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }
}
