spyMessageReporter = TestEnvironment::getUtilityFactory()->newSpyMessageReporter(); $this->setupFile = $this->getMockBuilder( SetupFile::class ) ->disableOriginalConstructor() ->getMock(); } public function testCanConstruct() { $connection = $this->getMockBuilder( '\Wikimedia\Rdbms\Database' ) ->disableOriginalConstructor() ->getMockForAbstractClass(); $this->assertInstanceOf( VersionExaminer::class, new VersionExaminer( $connection ) ); } public function testRequirements() { $connection = $this->getMockBuilder( '\Wikimedia\Rdbms\Database' ) ->disableOriginalConstructor() ->setMethods( [ 'getServerInfo' ] ) ->getMockForAbstractClass(); $connection->expects( $this->atLeastOnce() ) ->method( 'getServerInfo' ) ->willReturn( 1 ); $connection->expects( $this->atLeastOnce() ) ->method( 'getType' ) ->willReturn( 'foo' ); $instance = new VersionExaminer( $connection ); $instance->setMessageReporter( $this->spyMessageReporter ); $instance->setSetupFile( $this->setupFile ); $requirements = $instance->defineDatabaseRequirements( [ 'foo' => 2 ] ); $this->assertArrayHasKey( 'type', $requirements ); $this->assertArrayHasKey( 'latest_version', $requirements ); $this->assertArrayHasKey( 'minimum_version', $requirements ); } public function testRequirements_InvalidDefined() { $connection = $this->getMockBuilder( '\Wikimedia\Rdbms\Database' ) ->disableOriginalConstructor() ->setMethods( [ 'getServerInfo' ] ) ->getMockForAbstractClass(); $connection->expects( $this->atLeastOnce() ) ->method( 'getType' ) ->willReturn( 'foo' ); $instance = new VersionExaminer( $connection ); $this->expectException( '\RuntimeException' ); $requirements = $instance->defineDatabaseRequirements( [ 'foobar' => 2 ] ); } public function testMeetsVersionMinRequirement() { $connection = $this->getMockBuilder( '\Wikimedia\Rdbms\Database' ) ->disableOriginalConstructor() ->setMethods( [ 'getServerInfo' ] ) ->getMockForAbstractClass(); $connection->expects( $this->atLeastOnce() ) ->method( 'getServerInfo' ) ->willReturn( 1 ); $connection->expects( $this->atLeastOnce() ) ->method( 'getType' ) ->willReturn( 'foo' ); $this->setupFile->expects( $this->once() ) ->method( 'remove' ); $instance = new VersionExaminer( $connection ); $instance->setMessageReporter( $this->spyMessageReporter ); $instance->setSetupFile( $this->setupFile ); $this->assertTrue( $instance->meetsVersionMinRequirement( [ 'foo' => 1 ] ) ); } public function testMeetsVersionMinRequirement_FailsMinimumRequirement() { $connection = $this->getMockBuilder( '\Wikimedia\Rdbms\Database' ) ->disableOriginalConstructor() ->setMethods( [ 'getServerInfo' ] ) ->getMockForAbstractClass(); $connection->expects( $this->atLeastOnce() ) ->method( 'getServerInfo' ) ->willReturn( 1 ); $connection->expects( $this->atLeastOnce() ) ->method( 'getType' ) ->willReturn( 'foo' ); $this->setupFile->expects( $this->once() ) ->method( 'set' ); $this->setupFile->expects( $this->once() ) ->method( 'finalize' ); $instance = new VersionExaminer( $connection ); $instance->setMessageReporter( $this->spyMessageReporter ); $instance->setSetupFile( $this->setupFile ); $this->assertFalse( $instance->meetsVersionMinRequirement( [ 'foo' => 2 ] ) ); $this->assertContains( "The `foo` database version of 1 doesn't meet the minimum requirement of 2", $this->spyMessageReporter->getMessagesAsString() ); } }