<commit_before><?php

namespace FA\Tests\Service;

use DateTime;
use FA\Service\ImageService;

class ImageServiceTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var ImageService
     */
    protected $service;

    /**
     * @var ImageDao
     */
    protected $dao;

    /**
     * @var FlickrService
     */
    protected $flickr;

    protected function setUp()
    {
        $this->dao = $this->getMock('FA\Dao\ImageDao', array('find', 'findAll', 'save', 'delete', 'findFirstImage'), array(), '', false);
        $this->flickr = $this->getMock('FA\Service\FlickrService', array('getSizes'), array(), '', false);
        $this->service = new ImageService($this->dao, $this->flickr);
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
        $this->service = null;
    }

    /**
     * @covers FA\Service\ImageService::find
     */
    public function testFind()
    {
        $imageData = array('day' => '1', 'photo_id' => '7606616668');
        $imageSizes = array('sizes' => array('size' => array()));

        $this->dao->expects($this->once())
                ->method('find')
                ->with($imageData['day'])
                ->will($this->returnValue($imageData));

        $this->flickr->expects($this->once())
                ->method('getSizes')
                ->with($imageData['photo_id'])
                ->will($this->returnValue($imageSizes));

        $result = $this->service->find($imageData['day']);

        $this->assertEquals(array_merge($imageData, $imageSizes), $result);
    }

    public function testFindImageDoesNotExist()
    {
        $this->dao->expects($this->once())
                ->method('find')
                ->with('222')
                ->will($this->returnValue(false));

        $this->flickr->expects($this->never())->method('getSizes');

        $this->assertNull($this->service->find('222'));
    }

    /**
     * @covers FA\Service\ImageService::findAll
     */
    public function testFindAll()
    {
        $imageData = array(
            array('day' => '1', 'photo_id' => '11'),
            array('day' => '2', 'photo_id' => '22'),
            array('day' => '3', 'photo_id' => '33')
        );

        $imageSizes = array(
            array('sizes' => array(11)),
            array('sizes' => array(22)),
            array('sizes' => array(33))
        );

        $this->dao->expects($this->once())
                ->method('findAll')
                ->will($this->returnValue($imageData));

        $expected = array();
        
        // The Flickr service should be called as many times as there are
        // data elements returned from the dao
        foreach ($imageData as $index => $image) {
            
            $expected[] = array_merge($image, $imageSizes[$index]);
            
            $this->flickr->expects($this->at($index))
                    ->method('getSizes')
                    ->with($image['photo_id'])
                    ->will($this->returnValue($imageSizes[$index]));
        }
        
        $result = $this->service->findAll();
        
        $this->assertEquals($expected, $result);
    }

    /**
     * @covers FA\Service\ImageService::save
     */
    public function testSave()
    {
        $this->dao->expects($this->once())
                ->method('save')
                ->with(array('day' => 200, 'photo_id' => 999999))
                ->will($this->returnValue(1));
        
        $this->assertEquals(1, $this->service->save(array('day' => 200, 'photo_id' => 999999)));
    }

    /**
     * @covers FA\Service\ImageService::delete
     */
    public function testDelete()
    {
        $this->dao->expects($this->once())
                ->method('delete')
                ->with(200)
                ->will($this->returnValue(1));
        
        $this->assertEquals(1, $this->service->delete(200));
    }

    /**
     * @covers FA\Service\ImageService::getProjectDay
     * @dataProvider getProjectDayDataProvider
     */
    public function testGetProjectDay($testDate, $projectDay)
    {
        $firstImage = array(
            'id' => 1,
            'day' => 1,
            'photo_id' => 7606616668,
            'posted' => '2012-07-29 15:31:56',
        );
        
        $date = new DateTime($testDate);

        $this->dao->expects($this->once())
                ->method('findFirstImage')
                ->will($this->returnValue($firstImage));

        $this->assertEquals($projectDay, $this->service->getProjectDay($date));
    }

    /**
     * @covers FA\Service\ImageService::getProjectDay
     * @dataProvider getProjectDayDataProvider
     */
    public function testGetProjectDayWhenNoImagesExist()
    {
        $this->dao->expects($this->once())
                ->method('findFirstImage')
                ->will($this->returnValue(false));

        $this->assertEquals(1, $this->service->getProjectDay());
    }

    public function getProjectDayDataProvider()
    {
        return array(
            array('2012-07-30', 2),
            array('2012-07-31', 3),
            array('2013-07-28', 365),
        );
    }
}<commit_msg>Adds test<commit_after><?php

namespace FA\Tests\Service;

use DateTime;
use FA\Service\ImageService;

class ImageServiceTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var ImageService
     */
    protected $service;

    /**
     * @var ImageDao
     */
    protected $dao;

    /**
     * @var FlickrService
     */
    protected $flickr;

    protected function setUp()
    {
        $this->dao = $this->getMock('FA\Dao\ImageDao', array('find', 'findAll', 'save', 'delete', 'findFirstImage', 'countImages'), array(), '', false);
        $this->flickr = $this->getMock('FA\Service\FlickrService', array('getSizes'), array(), '', false);
        $this->service = new ImageService($this->dao, $this->flickr);
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
        $this->service = null;
    }

    /**
     * @covers FA\Service\ImageService::find
     */
    public function testFind()
    {
        $imageData = array('day' => '1', 'photo_id' => '7606616668');
        $imageSizes = array('sizes' => array('size' => array()));

        $this->dao->expects($this->once())
                ->method('find')
                ->with($imageData['day'])
                ->will($this->returnValue($imageData));

        $this->flickr->expects($this->once())
                ->method('getSizes')
                ->with($imageData['photo_id'])
                ->will($this->returnValue($imageSizes));

        $result = $this->service->find($imageData['day']);

        $this->assertEquals(array_merge($imageData, $imageSizes), $result);
    }

    public function testFindImageDoesNotExist()
    {
        $this->dao->expects($this->once())
                ->method('find')
                ->with('222')
                ->will($this->returnValue(false));

        $this->flickr->expects($this->never())->method('getSizes');

        $this->assertNull($this->service->find('222'));
    }

    /**
     * @covers FA\Service\ImageService::findAll
     */
    public function testFindAll()
    {
        $imageData = array(
            array('day' => '1', 'photo_id' => '11'),
            array('day' => '2', 'photo_id' => '22'),
            array('day' => '3', 'photo_id' => '33')
        );

        $imageSizes = array(
            array('sizes' => array(11)),
            array('sizes' => array(22)),
            array('sizes' => array(33))
        );

        $this->dao->expects($this->once())
                ->method('findAll')
                ->will($this->returnValue($imageData));

        $expected = array();
        
        // The Flickr service should be called as many times as there are
        // data elements returned from the dao
        foreach ($imageData as $index => $image) {
            
            $expected[] = array_merge($image, $imageSizes[$index]);
            
            $this->flickr->expects($this->at($index))
                    ->method('getSizes')
                    ->with($image['photo_id'])
                    ->will($this->returnValue($imageSizes[$index]));
        }
        
        $result = $this->service->findAll();
        
        $this->assertEquals($expected, $result);
    }

    /**
     * @covers FA\Service\ImageService::save
     */
    public function testSave()
    {
        $this->dao->expects($this->once())
                ->method('save')
                ->with(array('day' => 200, 'photo_id' => 999999))
                ->will($this->returnValue(1));
        
        $this->assertEquals(1, $this->service->save(array('day' => 200, 'photo_id' => 999999)));
    }

    /**
     * @covers FA\Service\ImageService::delete
     */
    public function testDelete()
    {
        $this->dao->expects($this->once())
                ->method('delete')
                ->with(200)
                ->will($this->returnValue(1));
        
        $this->assertEquals(1, $this->service->delete(200));
    }

    /**
     * @covers FA\Service\ImageService::getProjectDay
     * @dataProvider getProjectDayDataProvider
     */
    public function testGetProjectDay($testDate, $projectDay)
    {
        $firstImage = array(
            'id' => 1,
            'day' => 1,
            'photo_id' => 7606616668,
            'posted' => '2012-07-29 15:31:56',
        );
        
        $date = new DateTime($testDate);

        $this->dao->expects($this->once())
                ->method('findFirstImage')
                ->will($this->returnValue($firstImage));

        $this->assertEquals($projectDay, $this->service->getProjectDay($date));
    }

    /**
     * @covers FA\Service\ImageService::getProjectDay
     * @dataProvider getProjectDayDataProvider
     */
    public function testGetProjectDayWhenNoImagesExist()
    {
        $this->dao->expects($this->once())
                ->method('findFirstImage')
                ->will($this->returnValue(false));

        $this->assertEquals(1, $this->service->getProjectDay());
    }

    public function testCountImages()
    {
        $this->dao->expects($this->once())
            ->method('countImages');
        
        $this->service->countImages();
    }

    public function getProjectDayDataProvider()
    {
        return array(
            array('2012-07-30', 2),
            array('2012-07-31', 3),
            array('2013-07-28', 365),
        );
    }
}
{'fraction_non_alphanumeric': 0.17676862450741226, 'fraction_numerical': 0.021767686245074122, 'mean_word_length': 1.9550873301912948, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 239, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2129791', 'n_tokens_mistral': 3286, 'n_tokens_neox': 3072, 'n_words': 504}