jsonImportContentsFileDirReader = $this->getMockBuilder( JsonImportContentsFileDirReader::class ) ->disableOriginalConstructor() ->getMock(); $this->contentIterator = $this->getMockBuilder( ContentIterator::class ) ->disableOriginalConstructor() ->getMock(); $this->contentCreator = $this->getMockBuilder( '\SMW\Importer\ContentCreator' ) ->disableOriginalConstructor() ->getMock(); $this->testEnvironment = new TestEnvironment(); $this->spyMessageReporter = $this->testEnvironment->getUtilityFactory()->newSpyMessageReporter(); } public function testCanConstruct() { $this->assertInstanceOf( '\SMW\Importer\Importer', new Importer( $this->contentIterator, $this->contentCreator ) ); } public function testDisabled() { $instance = new Importer( new JsonContentIterator( $this->jsonImportContentsFileDirReader ), $this->contentCreator ); $instance->setMessageReporter( $this->spyMessageReporter ); $instance->isEnabled( false ); $instance->runImport(); $this->assertContains( 'Import support was not enabled (or skipped), stopping the task', $this->spyMessageReporter->getMessagesAsString() ); } public function testRunImport() { $importContents = new ImportContents(); $importContents->setName( 'Foo' ); $importContents->setVersion( 1 ); $page = $this->getMockBuilder( '\WikiPage' ) ->disableOriginalConstructor() ->getMock(); $this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() ) ->method( 'getContentList' ) ->willReturn( [ 'Foo' => [ $importContents ] ] ); $this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() ) ->method( 'getErrors' ) ->willReturn( [] ); $this->contentCreator->expects( $this->atLeastOnce() ) ->method( 'create' ); $instance = new Importer( new JsonContentIterator( $this->jsonImportContentsFileDirReader ), $this->contentCreator ); $instance->setMessageReporter( $this->spyMessageReporter ); $instance->setReqVersion( 1 ); $instance->runImport(); } public function testRunImportWithError() { $importContents = new ImportContents(); $importContents->addError( 'Bar' ); $importContents->setVersion( 1 ); $this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() ) ->method( 'getContentList' ) ->willReturn( [ 'Foo' => [ $importContents ] ] ); $this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() ) ->method( 'getErrors' ) ->willReturn( [ 'Error' ] ); $this->contentCreator->expects( $this->never() ) ->method( 'create' ); $instance = new Importer( new JsonContentIterator( $this->jsonImportContentsFileDirReader ), $this->contentCreator ); $instance->setMessageReporter( $this->spyMessageReporter ); $instance->setReqVersion( 1 ); $instance->runImport(); } public function testRunImportWithErrorDuringCreation() { $importContents = new ImportContents(); $importContents->setVersion( 1 ); $this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() ) ->method( 'getContentList' ) ->willReturn( [ 'Foo' => [ $importContents ] ] ); $this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() ) ->method( 'getErrors' ) ->willReturn( [] ); $this->contentCreator->expects( $this->once() ) ->method( 'create' ) ->with( $this->callback( static function ( $importContents ) { $importContents->addError( 'BarError from create' ); $importContents->addError( [ 'Foo1', 'Foo2' ] ); return true; } ) ); $instance = new Importer( new JsonContentIterator( $this->jsonImportContentsFileDirReader ), $this->contentCreator ); $instance->setMessageReporter( $this->spyMessageReporter ); $instance->setReqVersion( 1 ); $instance->runImport(); $this->assertContains( 'BarError from create', $this->spyMessageReporter->getMessagesAsString() ); } }