|
| 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\FilesManager; |
| 18 | +use Symfony\Flex\Lock; |
| 19 | + |
| 20 | +class FilesManagerTest extends TestCase |
| 21 | +{ |
| 22 | + public function testShouldWrite() |
| 23 | + { |
| 24 | + @mkdir(FLEX_TEST_DIR); |
| 25 | + (new Process(['git', 'init'], FLEX_TEST_DIR))->mustRun(); |
| 26 | + (new Process(['git', 'config', 'user.name', 'Unit test'], FLEX_TEST_DIR))->mustRun(); |
| 27 | + (new Process(['git', 'config', 'user.email', ''], FLEX_TEST_DIR))->mustRun(); |
| 28 | + |
| 29 | + $filePath = FLEX_TEST_DIR.'/a.txt'; |
| 30 | + file_put_contents($filePath, 'a'); |
| 31 | + (new Process(['git', 'add', '-A'], FLEX_TEST_DIR))->mustRun(); |
| 32 | + (new Process(['git', 'commit', '-m', 'setup of original files'], FLEX_TEST_DIR))->mustRun(); |
| 33 | + |
| 34 | + file_put_contents($filePath, 'b'); |
| 35 | + |
| 36 | + $io = $this->getMockBuilder(IOInterface::class)->getMock(); |
| 37 | + $io->method('askConfirmation')->willReturn(true); |
| 38 | + |
| 39 | + $lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock(); |
| 40 | + $lock->method('all')->willReturn([ |
| 41 | + 'symfony/my-package' => [ |
| 42 | + 'files' => [ |
| 43 | + $filePath, |
| 44 | + ], |
| 45 | + ], |
| 46 | + ]); |
| 47 | + |
| 48 | + $filesManager = new FilesManager($io, $lock, FLEX_TEST_DIR); |
| 49 | + |
| 50 | + // We need to set the writtenFiles property to reset the state |
| 51 | + $reflection = new \ReflectionProperty(FilesManager::class, 'writtenFiles'); |
| 52 | + $reflection->setAccessible(true); |
| 53 | + |
| 54 | + $this->assertTrue($filesManager->shouldWriteFile('non-existing-file.txt', false, false)); |
| 55 | + $this->assertFalse($filesManager->shouldWriteFile($filePath, false, false)); |
| 56 | + |
| 57 | + // It allowed to write the file |
| 58 | + $reflection->setValue($filesManager, []); |
| 59 | + $this->assertTrue($filesManager->shouldWriteFile($filePath, true, false)); |
| 60 | + |
| 61 | + // We skip all questions, so we're able to write |
| 62 | + $reflection->setValue($filesManager, []); |
| 63 | + $this->assertTrue($filesManager->shouldWriteFile($filePath, true, true)); |
| 64 | + } |
| 65 | +} |
0 commit comments