<commit_before><?php

/*
 * This file is part of the Symfony package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Tests\Component\HttpFoundation\File;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;

class FileTest extends \PHPUnit_Framework_TestCase
{
    protected $file;

    protected function setUp()
    {
        $this->file = new File(__DIR__.'/Fixtures/test.gif');
    }

    public function testGetPathReturnsAbsolutePath()
    {
        $this->assertEquals(__DIR__.'/Fixtures/test.gif', $this->file->getPath());
    }

    public function test__toString()
    {
        $this->assertEquals(__DIR__.'/Fixtures/test.gif', (string) $this->file);
    }

    public function testGetNameReturnsNameWithExtension()
    {
        $this->assertEquals('test.gif', $this->file->getName());
    }

    public function testGetExtensionReturnsEmptyString()
    {
        $file = new File(__DIR__.'/Fixtures/test');
        $this->assertEquals('', $file->getExtension());
    }

    public function testGetExtensionReturnsExtensionWithDot()
    {
        $this->assertEquals('.gif', $this->file->getExtension());
    }

    public function testGetDirectoryReturnsDirectoryName()
    {
        $this->assertEquals(__DIR__.'/Fixtures', $this->file->getDirectory());
    }

    public function testGetMimeTypeUsesMimeTypeGuessers()
    {
        $guesser = $this->createMockGuesser($this->file->getPath(), 'image/gif');

        MimeTypeGuesser::getInstance()->register($guesser);

        $this->assertEquals('image/gif', $this->file->getMimeType());
    }

    public function testGuessExtensionWithoutGuesser()
    {
        $file = new File(__DIR__.'/Fixtures/directory/.empty');

        $this->assertEquals(null, $file->guessExtension());
    }

    public function testGuessExtensionIsBasedOnMimeType()
    {
        $file = new File(__DIR__.'/Fixtures/test');
        $guesser = $this->createMockGuesser($file->getPath(), 'image/gif');

        MimeTypeGuesser::getInstance()->register($guesser);

        $this->assertEquals('.gif', $file->guessExtension());
    }

    public function testConstructWhenFileNotExists()
    {
        $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');

        new File(__DIR__.'/Fixtures/not_here');
    }

    public function testSizeReturnsFileSize()
    {
        $this->assertEquals(filesize($this->file->getPath()), $this->file->getSize());
    }

    public function testSizeFailing()
    {
        $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');

        $dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
        $path = $dir.DIRECTORY_SEPARATOR.'test.copy.gif';
        @unlink($path);
        copy(__DIR__.'/Fixtures/test.gif', $path);

        $file = new File($path);
        @unlink($path);
        $file->getSize();
    }

    public function testMove()
    {
        $path = __DIR__.'/Fixtures/test.copy.gif';
        $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
        $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.copy.gif';
        @unlink($path);
        @unlink($targetPath);
        copy(__DIR__.'/Fixtures/test.gif', $path);

        $file = new File($path);
        $file->move($targetDir);

        $this->assertTrue(file_exists($targetPath));
        $this->assertFalse(file_exists($path));
        $this->assertEquals($targetPath, $file->getPath());

        @unlink($path);
        @unlink($targetPath);
    }

    public function testMoveWithNewName()
    {
        $path = __DIR__.'/Fixtures/test.copy.gif';
        $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
        $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.newname.gif';
        @unlink($path);
        @unlink($targetPath);
        copy(__DIR__.'/Fixtures/test.gif', $path);

        $file = new File($path);
        $file->move($targetDir, 'test.newname.gif');

        $this->assertTrue(file_exists($targetPath));
        $this->assertFalse(file_exists($path));
        $this->assertEquals($targetPath, $file->getPath());

        @unlink($path);
        @unlink($targetPath);
    }

    public function testMoveFailing()
    {
        $path = __DIR__.'/Fixtures/test.copy.gif';
        $targetPath = '/thisfolderwontexist';
        @unlink($path);
        @unlink($targetPath);
        copy(__DIR__.'/Fixtures/test.gif', $path);

        $file = new File($path);

        $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
        $file->move($targetPath);

        $this->assertFileExists($path);
        $this->assertFileNotExists($path.$targetPath.'test.gif');
        $this->assertEquals($path, $file->getPath());

        @unlink($path);
        @unlink($targetPath);
    }

    protected function createMockGuesser($path, $mimeType)
    {
        $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
        $guesser->expects($this->once())
                        ->method('guess')
                        ->with($this->equalTo($path))
                        ->will($this->returnValue($mimeType));

        return $guesser;
    }
}<commit_msg>[HttpFoundation] Fix FileException checking in FileTest<commit_after><?php

/*
 * This file is part of the Symfony package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Tests\Component\HttpFoundation\File;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\Exception\FileException;

class FileTest extends \PHPUnit_Framework_TestCase
{
    protected $file;

    protected function setUp()
    {
        $this->file = new File(__DIR__.'/Fixtures/test.gif');
    }

    public function testGetPathReturnsAbsolutePath()
    {
        $this->assertEquals(__DIR__.'/Fixtures/test.gif', $this->file->getPath());
    }

    public function test__toString()
    {
        $this->assertEquals(__DIR__.'/Fixtures/test.gif', (string) $this->file);
    }

    public function testGetNameReturnsNameWithExtension()
    {
        $this->assertEquals('test.gif', $this->file->getName());
    }

    public function testGetExtensionReturnsEmptyString()
    {
        $file = new File(__DIR__.'/Fixtures/test');
        $this->assertEquals('', $file->getExtension());
    }

    public function testGetExtensionReturnsExtensionWithDot()
    {
        $this->assertEquals('.gif', $this->file->getExtension());
    }

    public function testGetDirectoryReturnsDirectoryName()
    {
        $this->assertEquals(__DIR__.'/Fixtures', $this->file->getDirectory());
    }

    public function testGetMimeTypeUsesMimeTypeGuessers()
    {
        $guesser = $this->createMockGuesser($this->file->getPath(), 'image/gif');

        MimeTypeGuesser::getInstance()->register($guesser);

        $this->assertEquals('image/gif', $this->file->getMimeType());
    }

    public function testGuessExtensionWithoutGuesser()
    {
        $file = new File(__DIR__.'/Fixtures/directory/.empty');

        $this->assertEquals(null, $file->guessExtension());
    }

    public function testGuessExtensionIsBasedOnMimeType()
    {
        $file = new File(__DIR__.'/Fixtures/test');
        $guesser = $this->createMockGuesser($file->getPath(), 'image/gif');

        MimeTypeGuesser::getInstance()->register($guesser);

        $this->assertEquals('.gif', $file->guessExtension());
    }

    public function testConstructWhenFileNotExists()
    {
        $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');

        new File(__DIR__.'/Fixtures/not_here');
    }

    public function testSizeReturnsFileSize()
    {
        $this->assertEquals(filesize($this->file->getPath()), $this->file->getSize());
    }

    public function testSizeFailing()
    {
        $dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
        $path = $dir.DIRECTORY_SEPARATOR.'test.copy.gif';
        @unlink($path);
        copy(__DIR__.'/Fixtures/test.gif', $path);

        $file = new File($path);
        @unlink($path);

        try {
            $file->getSize();
            $this->fail('File::getSize should throw an exception.');
        } catch (FileException $e) {
        }
    }

    public function testMove()
    {
        $path = __DIR__.'/Fixtures/test.copy.gif';
        $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
        $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.copy.gif';
        @unlink($path);
        @unlink($targetPath);
        copy(__DIR__.'/Fixtures/test.gif', $path);

        $file = new File($path);
        $file->move($targetDir);

        $this->assertTrue(file_exists($targetPath));
        $this->assertFalse(file_exists($path));
        $this->assertEquals($targetPath, $file->getPath());

        @unlink($path);
        @unlink($targetPath);
    }

    public function testMoveWithNewName()
    {
        $path = __DIR__.'/Fixtures/test.copy.gif';
        $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
        $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.newname.gif';
        @unlink($path);
        @unlink($targetPath);
        copy(__DIR__.'/Fixtures/test.gif', $path);

        $file = new File($path);
        $file->move($targetDir, 'test.newname.gif');

        $this->assertTrue(file_exists($targetPath));
        $this->assertFalse(file_exists($path));
        $this->assertEquals($targetPath, $file->getPath());

        @unlink($path);
        @unlink($targetPath);
    }

    public function testMoveFailing()
    {
        $path = __DIR__.'/Fixtures/test.copy.gif';
        $targetPath = '/thisfolderwontexist';
        @unlink($path);
        @unlink($targetPath);
        copy(__DIR__.'/Fixtures/test.gif', $path);

        $file = new File($path);

        try {
            $file->move($targetPath);
            $this->fail('File::move should throw an exception.');
        } catch (FileException $e) {
        }

        $this->assertFileExists($path);
        $this->assertFileNotExists($path.$targetPath.'test.gif');
        $this->assertEquals($path, $file->getPath());

        @unlink($path);
        @unlink($targetPath);
    }

    protected function createMockGuesser($path, $mimeType)
    {
        $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
        $guesser->expects($this->once())
                        ->method('guess')
                        ->with($this->equalTo($path))
                        ->will($this->returnValue($mimeType));

        return $guesser;
    }
}
{'fraction_non_alphanumeric': 0.15497757847533633, 'fraction_numerical': 0.0, 'mean_word_length': 2.9444640962150688, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 135, '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': '4152874', 'n_tokens_mistral': 3313, 'n_tokens_neox': 3268, 'n_words': 463}