1
0
Fork 0

Clean up useless mocks

pull/10547/head
Jordi Boggiano 2022-02-18 13:05:12 +01:00
parent a16ed3d0ed
commit 137ebddc25
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 29 additions and 77 deletions

View File

@ -13,6 +13,7 @@
namespace Composer\Test\Installer; namespace Composer\Test\Installer;
use Composer\Installer\LibraryInstaller; use Composer\Installer\LibraryInstaller;
use Composer\Repository\InstalledArrayRepository;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Test\TestCase; use Composer\Test\TestCase;
use Composer\Composer; use Composer\Composer;
@ -121,16 +122,21 @@ class LibraryInstallerTest extends TestCase
public function testIsInstalled(): void public function testIsInstalled(): void
{ {
$library = new LibraryInstaller($this->io, $this->composer); $library = new LibraryInstaller($this->io, $this->composer);
$package = $this->createPackageMock(); $package = $this->getPackage('test/pkg', '1.0.0');
$this->repository $repository = new InstalledArrayRepository();
->expects($this->exactly(2)) $this->assertFalse($library->isInstalled($repository, $package));
->method('hasPackage')
->with($package)
->will($this->onConsecutiveCalls(true, false));
$this->assertTrue($library->isInstalled($this->repository, $package)); // package being in repo is not enough to be installed
$this->assertFalse($library->isInstalled($this->repository, $package)); $repository->addPackage($package);
$this->assertFalse($library->isInstalled($repository, $package));
// package being in repo and vendor/pkg/foo dir present means it is seen as installed
$this->ensureDirectoryExistsAndClear($this->vendorDir.'/'.$package->getPrettyName());
$this->assertTrue($library->isInstalled($repository, $package));
$repository->removePackage($package);
$this->assertFalse($library->isInstalled($repository, $package));
} }
/** /**
@ -140,12 +146,7 @@ class LibraryInstallerTest extends TestCase
public function testInstall(): void public function testInstall(): void
{ {
$library = new LibraryInstaller($this->io, $this->composer); $library = new LibraryInstaller($this->io, $this->composer);
$package = $this->createPackageMock(); $package = $this->getPackage('some/package', '1.0.0');
$package
->expects($this->any())
->method('getPrettyName')
->will($this->returnValue('some/package'));
$this->dm $this->dm
->expects($this->once()) ->expects($this->once())
@ -173,30 +174,13 @@ class LibraryInstallerTest extends TestCase
$filesystem $filesystem
->expects($this->once()) ->expects($this->once())
->method('rename') ->method('rename')
->with($this->vendorDir.'/package1/oldtarget', $this->vendorDir.'/package1/newtarget'); ->with($this->vendorDir.'/vendor/package1/oldtarget', $this->vendorDir.'/vendor/package1/newtarget');
$initial = $this->createPackageMock(); $initial = $this->getPackage('vendor/package1', '1.0.0');
$target = $this->createPackageMock(); $target = $this->getPackage('vendor/package1', '2.0.0');
$initial $initial->setTargetDir('oldtarget');
->expects($this->any()) $target->setTargetDir('newtarget');
->method('getPrettyName')
->will($this->returnValue('package1'));
$initial
->expects($this->any())
->method('getTargetDir')
->will($this->returnValue('oldtarget'));
$target
->expects($this->any())
->method('getPrettyName')
->will($this->returnValue('package1'));
$target
->expects($this->any())
->method('getTargetDir')
->will($this->returnValue('newtarget'));
$this->repository $this->repository
->expects($this->exactly(3)) ->expects($this->exactly(3))
@ -206,7 +190,7 @@ class LibraryInstallerTest extends TestCase
$this->dm $this->dm
->expects($this->once()) ->expects($this->once())
->method('update') ->method('update')
->with($initial, $target, $this->vendorDir.'/package1/newtarget'); ->with($initial, $target, $this->vendorDir.'/vendor/package1/newtarget');
$this->repository $this->repository
->expects($this->once()) ->expects($this->once())
@ -231,16 +215,7 @@ class LibraryInstallerTest extends TestCase
public function testUninstall(): void public function testUninstall(): void
{ {
$library = new LibraryInstaller($this->io, $this->composer); $library = new LibraryInstaller($this->io, $this->composer);
$package = $this->createPackageMock(); $package = $this->getPackage('vendor/pkg', '1.0.0');
$package
->expects($this->any())
->method('getPrettyName')
->will($this->returnValue('pkg'));
$package
->expects($this->any())
->method('getName')
->will($this->returnValue('pkg'));
$this->repository $this->repository
->expects($this->exactly(2)) ->expects($this->exactly(2))
@ -251,7 +226,7 @@ class LibraryInstallerTest extends TestCase
$this->dm $this->dm
->expects($this->once()) ->expects($this->once())
->method('remove') ->method('remove')
->with($package, $this->vendorDir.'/pkg'); ->with($package, $this->vendorDir.'/vendor/pkg');
$this->repository $this->repository
->expects($this->once()) ->expects($this->once())
@ -265,32 +240,19 @@ class LibraryInstallerTest extends TestCase
$library->uninstall($this->repository, $package); $library->uninstall($this->repository, $package);
} }
public function testGetInstallPath(): void public function testGetInstallPathWithoutTargetDir(): void
{ {
$library = new LibraryInstaller($this->io, $this->composer); $library = new LibraryInstaller($this->io, $this->composer);
$package = $this->createPackageMock(); $package = $this->getPackage('Vendor/Pkg', '1.0.0');
$package $this->assertEquals($this->vendorDir.'/'.$package->getPrettyName(), $library->getInstallPath($package));
->expects($this->once())
->method('getTargetDir')
->will($this->returnValue(null));
$this->assertEquals($this->vendorDir.'/'.$package->getName(), $library->getInstallPath($package));
} }
public function testGetInstallPathWithTargetDir(): void public function testGetInstallPathWithTargetDir(): void
{ {
$library = new LibraryInstaller($this->io, $this->composer); $library = new LibraryInstaller($this->io, $this->composer);
$package = $this->createPackageMock(); $package = $this->getPackage('Foo/Bar', '1.0.0');
$package->setTargetDir('Some/Namespace');
$package
->expects($this->once())
->method('getTargetDir')
->will($this->returnValue('Some/Namespace'));
$package
->expects($this->any())
->method('getPrettyName')
->will($this->returnValue('foo/bar'));
$this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package)); $this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package));
} }
@ -306,7 +268,7 @@ class LibraryInstallerTest extends TestCase
->getMock(); ->getMock();
$library = new LibraryInstaller($this->io, $this->composer, 'library', null, $binaryInstallerMock); $library = new LibraryInstaller($this->io, $this->composer, 'library', null, $binaryInstallerMock);
$package = $this->createPackageMock(); $package = $this->getPackage('foo/bar', '1.0.0');
$binaryInstallerMock $binaryInstallerMock
->expects($this->never()) ->expects($this->never())
@ -320,14 +282,4 @@ class LibraryInstallerTest extends TestCase
$library->ensureBinariesPresence($package); $library->ensureBinariesPresence($package);
} }
/**
* @return \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected function createPackageMock()
{
return $this->getMockBuilder('Composer\Package\Package')
->setConstructorArgs(array(md5((string) mt_rand()), '1.0.0.0', '1.0.0'))
->getMock();
}
} }