|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Flex\Tests; |
| 13 | + |
| 14 | +use Composer\IO\IOInterface; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Process\Process; |
| 17 | +use Symfony\Flex\Options; |
| 18 | + |
| 19 | +class OptionsTest extends TestCase |
| 20 | +{ |
| 21 | + public function testShouldWrite() |
| 22 | + { |
| 23 | + @mkdir(FLEX_TEST_DIR); |
| 24 | + (new Process(['git', 'init'], FLEX_TEST_DIR))->mustRun(); |
| 25 | + (new Process(['git', 'config', 'user.name', 'Unit test'], FLEX_TEST_DIR))->mustRun(); |
| 26 | + (new Process(['git', 'config', 'user.email', ''], FLEX_TEST_DIR))->mustRun(); |
| 27 | + |
| 28 | + $filePath = FLEX_TEST_DIR.'/a.txt'; |
| 29 | + file_put_contents($filePath, 'a'); |
| 30 | + (new Process(['git', 'add', '-A'], FLEX_TEST_DIR))->mustRun(); |
| 31 | + (new Process(['git', 'commit', '-m', 'setup of original files'], FLEX_TEST_DIR))->mustRun(); |
| 32 | + |
| 33 | + file_put_contents($filePath, 'b'); |
| 34 | + |
| 35 | + $this->assertTrue((new Options([], null))->shouldWriteFile('non-existing-file.txt', false, false)); |
| 36 | + $this->assertFalse((new Options([], null))->shouldWriteFile($filePath, false, false)); |
| 37 | + |
| 38 | + // We don't have an IO, so we don't write the file |
| 39 | + $this->assertFalse((new Options([], null))->shouldWriteFile($filePath, true, false)); |
| 40 | + |
| 41 | + // We have an IO, and it allowed to write the file |
| 42 | + $io = $this->createMock(IOInterface::class); |
| 43 | + $io->expects($this->once())->method('askConfirmation')->willReturn(true); |
| 44 | + $this->assertTrue((new Options([], $io))->shouldWriteFile($filePath, true, false)); |
| 45 | + |
| 46 | + // We skip all questions, so we're able to write |
| 47 | + $this->assertTrue((new Options([], null))->shouldWriteFile($filePath, true, true)); |
| 48 | + } |
| 49 | +} |
0 commit comments