|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright MediaCT. All rights reserved. |
| 4 | + * https://www.mediact.nl |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Mediact\Composer\Tests; |
| 8 | + |
| 9 | +use Composer\IO\IOInterface; |
| 10 | +use Mediact\FileMapping\FileMappingInterface; |
| 11 | +use Mediact\FileMapping\FileMappingReaderInterface; |
| 12 | +use org\bovigo\vfs\vfsStream; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use PHPUnit_Framework_MockObject_MockObject; |
| 15 | +use Mediact\Composer\FileInstaller; |
| 16 | + |
| 17 | +/** |
| 18 | + * @coversDefaultClass \MediaCT\Composer\FileInstaller |
| 19 | + */ |
| 20 | +class FileInstallerTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @return void |
| 24 | + * |
| 25 | + * @covers ::__construct |
| 26 | + */ |
| 27 | + public function testConstructor() |
| 28 | + { |
| 29 | + /** @noinspection PhpParamsInspection */ |
| 30 | + $this->assertInstanceOf( |
| 31 | + FileInstaller::class, |
| 32 | + new FileInstaller( |
| 33 | + $this->createMock(FileMappingReaderInterface::class) |
| 34 | + ) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return void |
| 40 | + * @covers ::installFile |
| 41 | + */ |
| 42 | + public function testInstallFile() |
| 43 | + { |
| 44 | + /** @var FileMappingReaderInterface $reader */ |
| 45 | + $reader = $this->createMock(FileMappingReaderInterface::class); |
| 46 | + $installer = new FileInstaller($reader); |
| 47 | + |
| 48 | + $fs = vfsStream::setup( |
| 49 | + sha1(__METHOD__), |
| 50 | + null, |
| 51 | + [ |
| 52 | + 'source' => [ |
| 53 | + 'foo.php' => 'Foo' |
| 54 | + ], |
| 55 | + 'destination' => [] |
| 56 | + ] |
| 57 | + ); |
| 58 | + |
| 59 | + /** @var FileMappingInterface|PHPUnit_Framework_MockObject_MockObject $mapping */ |
| 60 | + $mapping = $this->createMock(FileMappingInterface::class); |
| 61 | + $mapping |
| 62 | + ->expects($this->once()) |
| 63 | + ->method('getSource') |
| 64 | + ->willReturn( |
| 65 | + $fs->getChild('source/foo.php')->url() |
| 66 | + ); |
| 67 | + |
| 68 | + $mapping |
| 69 | + ->expects($this->once()) |
| 70 | + ->method('getDestination') |
| 71 | + ->willReturn( |
| 72 | + $fs->getChild('destination')->url() . '/foo.php' |
| 73 | + ); |
| 74 | + |
| 75 | + $installer->installFile($mapping); |
| 76 | + |
| 77 | + $this->assertStringEqualsFile( |
| 78 | + $fs->getChild('destination/foo.php')->url(), |
| 79 | + 'Foo' |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return void |
| 85 | + * @covers ::install |
| 86 | + */ |
| 87 | + public function testInstall() |
| 88 | + { |
| 89 | + /** @var FileMappingReaderInterface|PHPUnit_Framework_MockObject_MockObject $reader */ |
| 90 | + $reader = $this->createMock(FileMappingReaderInterface::class); |
| 91 | + $installer = new FileInstaller($reader); |
| 92 | + |
| 93 | + $fs = vfsStream::setup( |
| 94 | + sha1(__METHOD__), |
| 95 | + null, |
| 96 | + [ |
| 97 | + 'source' => [ |
| 98 | + 'foo.php' => 'Foo' |
| 99 | + ], |
| 100 | + 'destination' => [] |
| 101 | + ] |
| 102 | + ); |
| 103 | + |
| 104 | + /** @var FileMappingInterface|PHPUnit_Framework_MockObject_MockObject $mapping */ |
| 105 | + $mapping = $this->createMock(FileMappingInterface::class); |
| 106 | + $mapping |
| 107 | + ->expects($this->once()) |
| 108 | + ->method('getSource') |
| 109 | + ->willReturn( |
| 110 | + $fs->getChild('source/foo.php')->url() |
| 111 | + ); |
| 112 | + |
| 113 | + $mapping |
| 114 | + ->expects($this->exactly(3)) |
| 115 | + ->method('getDestination') |
| 116 | + ->willReturn( |
| 117 | + $fs->getChild('destination')->url() . '/foo.php' |
| 118 | + ); |
| 119 | + |
| 120 | + $mapping |
| 121 | + ->expects($this->once()) |
| 122 | + ->method('getRelativeDestination') |
| 123 | + ->willReturn('foo.php'); |
| 124 | + |
| 125 | + $reader |
| 126 | + ->expects($this->exactly(4)) |
| 127 | + ->method('valid') |
| 128 | + ->willReturnOnConsecutiveCalls(true, false, true, false); |
| 129 | + |
| 130 | + $reader |
| 131 | + ->expects($this->exactly(2)) |
| 132 | + ->method('current') |
| 133 | + ->willReturn($mapping); |
| 134 | + |
| 135 | + /** @var IOInterface|PHPUnit_Framework_MockObject_MockObject $io */ |
| 136 | + $io = $this->createMock(IOInterface::class); |
| 137 | + $io |
| 138 | + ->expects($this->once()) |
| 139 | + ->method('write') |
| 140 | + ->with($this->isType('string')); |
| 141 | + |
| 142 | + $installer->install($io); |
| 143 | + |
| 144 | + $this->assertStringEqualsFile( |
| 145 | + $fs->getChild('destination/foo.php')->url(), |
| 146 | + 'Foo' |
| 147 | + ); |
| 148 | + |
| 149 | + $installer->install($io); |
| 150 | + } |
| 151 | +} |
0 commit comments