<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\Services\Printing\TurnOverPrintingService;

class TurnOverPrintingServiceTest extends TestCase
{
    use DatabaseTransactions;

    public function testInitialization()
    {
        $printer = Mockery::mock('App\Services\Printing\PrinterProvider');
        $printer->shouldReceive('printLine');

        $repository = Mockery::mock('App\Repositories\Guest\SaleRepository');

        $service = new TurnOverPrintingService($printer, $repository);
    }

    public function testBasicPrinting()
    {
        $acc = factory(App\Models\Guest\Booking::class, 'Pre booked')->create();
        $acc = App\Models\Guest\Booking::with('bookingType')->find($acc->id);
        $msSales = factory(App\Models\Guest\Sale::class, 10)->make()->each(function($u) use ($acc) {
            $u->booking()->associate($acc);
            $u->is_morning_shift = true;
            $u->save();
        });

        $nsSales = factory(App\Models\Guest\Sale::class, 10)->make()->each(function($u) use ($acc) {
            $u->booking()->associate($acc);
            $u->is_morning_shift = false;
            $u->save();
        });

        $ms = $msSales->sum('total_in_default_currency');
        $ns = $nsSales->sum('total_in_default_currency');
        $total = $ms + $ns;
        $sales = $msSales->merge($nsSales);

        $printer = Mockery::mock('App\Services\Printing\PrinterProviderInterface');
        $printer->shouldReceive('printLine')->with('Garden Of Eden')->times(3);
        $printer->shouldReceive('printLine')->with('Day Turnover')->once();
        $printer->shouldReceive('printLine')->with('Morning shift')->once();
        $printer->shouldReceive('printLine')->with('Night shift')->once();
        $printer->shouldReceive('printLine')->with(\Carbon\Carbon::now('Asia/Manila')->format("l dS \of F Y h:i:s A"))->times(3);

        $printer->shouldReceive('printWithPrice')->with('Company', 0)->times(3);
        $printer->shouldReceive('printWithPrice')->with('Walkin Guest', 0)->times(3);

        // Morning shift, night shift and total
        $printer->shouldReceive('printWithPrice')->with('Pre booked', $ms)->once();
        $printer->shouldReceive('printWithPrice')->with('Pre booked', $ns)->once();
        $printer->shouldReceive('printWithPrice')->with('Pre booked', $total)->once();

        $printer->shouldReceive('printWithPrice')->with('VIP', 0)->times(3);
        $printer->shouldReceive('printWithPrice')->with('Walkin', 0)->times(3);

        // Morning shift, night shift and total
        $printer->shouldReceive('printWithPrice')->with('Total', $ms)->once();
        $printer->shouldReceive('printWithPrice')->with('Total', $ns)->once();
        $printer->shouldReceive('printWithPrice')->with('Total', $total)->once();

        $printer->shouldReceive('horizontalRuler')->times(6);

        $printer->shouldReceive('feed')->times(1 * 3);
        $printer->shouldReceive('cut')->times(1 * 3);

        $printer->shouldReceive('selectPrintMode')->zeroOrMoreTimes();

        $repository = Mockery::mock('App\Repositories\Guest\SaleRepository');
        $repository->shouldReceive('getTodaysPubSales')->once()->andReturn($sales);

        $service = new TurnOverPrintingService($printer, $repository);
        $service->print();
    }
}