<?php

namespace MediaVorus;

use Pimple\Container;

class MediaVorusServiceProviderTest extends TestCase
{
    private function getApplication()
    {
        return new Container();
    }

    public function testInitializationWithFFProbe()
    {
        $app = $this->getApplication();

        $app['exiftool.reader'] = $this->createMock(\PHPExiftool\Reader::class);
        $app['exiftool.writer'] = $this->createMock(\PHPExiftool\Writer::class);
        $app['ffmpeg.ffprobe'] = $this->createMock(\FFMpeg\FFProbe::class);

        $mediaVorusRef = new MediaVorusServiceProvider();
        $mediaVorusRef->register($app);
        $mediaVorusRef->boot($app);

        $this->assertInstanceOf(MediaVorus::class, $app['mediavorus']);
        $this->assertSame($app['exiftool.reader'], $app['mediavorus']->getReader());
        $this->assertSame($app['exiftool.writer'], $app['mediavorus']->getWriter());
        $this->assertSame($app['ffmpeg.ffprobe'], $app['mediavorus']->getFFProbe());
    }

    public function testInitializationWithoutFFProbe()
    {
        $app = $this->getApplication();

        $app['exiftool.reader'] = $this->createMock(\PHPExiftool\Reader::class);
        $app['exiftool.writer'] = $this->createMock(\PHPExiftool\Writer::class);

        $mediaVorusRef = new MediaVorusServiceProvider();
        $mediaVorusRef->register($app);
        $mediaVorusRef->boot($app);

        $this->assertInstanceOf(MediaVorus::class, $app['mediavorus']);
        $this->assertSame($app['exiftool.reader'], $app['mediavorus']->getReader());
        $this->assertSame($app['exiftool.writer'], $app['mediavorus']->getWriter());
        $this->assertNull($app['mediavorus']->getFFProbe());
    }

    public function testInitializationWithNonWorkingFFProbe()
    {
        $app = $this->getApplication();

        $app['exiftool.reader'] = $this->createMock(\PHPExiftool\Reader::class);
        $app['exiftool.writer'] = $this->createMock(\PHPExiftool\Writer::class);
        $app['ffmpeg.ffprobe'] = function () {
            throw new \FFMpeg\Exception\ExecutableNotFoundException('/path/to/nowhere');
        };

        $mediaVorusRef = new MediaVorusServiceProvider();
        $mediaVorusRef->register($app);
        $mediaVorusRef->boot($app);

        $this->assertInstanceOf(MediaVorus::class, $app['mediavorus']);
        $this->assertSame($app['exiftool.reader'], $app['mediavorus']->getReader());
        $this->assertSame($app['exiftool.writer'], $app['mediavorus']->getWriter());
        $this->assertNull($app['mediavorus']->getFFProbe());
    }

    /**
     * @expectedException MediaVorus\Exception\RuntimeException
     * @expectedExceptionMessage MediaVorus Service Provider requires Exiftool Service Provider
     */
    public function testFailOnExiftool()
    {
        $this->expectException(\MediaVorus\Exception\RuntimeException::class);
        $this->expectExceptionMessage('MediaVorus Service Provider requires Exiftool Service Provider');

        $app = $this->getApplication();
        $mediaVorusRef = new MediaVorusServiceProvider();
        $mediaVorusRef->register($app);
        $mediaVorusRef->boot($app);
    }
}