summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/Importer/ImporterTest.php
blob: 0db5f2ff16c6821e8c01e3e54b5cdf978a400cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php

namespace SMW\Tests\Importer;

use SMW\Importer\ContentIterator;
use SMW\Importer\ImportContents;
use SMW\Importer\Importer;
use SMW\Importer\JsoncontentIterator;
use SMW\Importer\JsonImportContentsFileDirReader;
use SMW\Tests\TestEnvironment;

/**
 * @covers \SMW\Importer\Importer
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class ImporterTest extends \PHPUnit_Framework_TestCase {

	private $spyMessageReporter;
	private $testEnvironment;
	private $contentIterator;
	private $jsonImportContentsFileDirReader;
	private $contentCreator;
	private $messageReporter;

	protected function setUp() {
		parent::setUp();

		$this->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->messageReporter = $this->getMockBuilder( '\Onoi\MessageReporter\MessageReporter' )
			->disableOriginalConstructor()
			->getMock();

		$this->testEnvironment = new TestEnvironment();
	}

	public function testCanConstruct() {

		$this->assertInstanceOf(
			'\SMW\Importer\Importer',
			new Importer( $this->contentIterator, $this->contentCreator )
		);
	}

	public function testDisabled() {

		$spyMessageReporter = $this->testEnvironment->getUtilityFactory()->newSpyMessageReporter();

		$instance = new Importer(
			new JsoncontentIterator( $this->jsonImportContentsFileDirReader ),
			$this->contentCreator
		);

		$instance->setMessageReporter( $spyMessageReporter );
		$instance->isEnabled( false );

		$instance->doImport();

		$this->assertContains(
			'Skipping the import process',
			$spyMessageReporter->getMessagesAsString()
		);
	}

	public function testDoImport() {

		$importContents = new ImportContents();

		$importContents->setName( 'Foo' );
		$importContents->setVersion( 1 );

		$page = $this->getMockBuilder( '\WikiPage' )
			->disableOriginalConstructor()
			->getMock();

		$this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() )
			->method( 'getContentList' )
			->will( $this->returnValue( [ 'Foo' => [ $importContents ] ] ) );

		$this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() )
			->method( 'getErrors' )
			->will( $this->returnValue( [] ) );

		$this->contentCreator->expects( $this->atLeastOnce() )
			->method( 'create' );

		$instance = new Importer(
			new JsoncontentIterator( $this->jsonImportContentsFileDirReader ),
			$this->contentCreator
		);

		$instance->setMessageReporter( $this->messageReporter );
		$instance->setReqVersion( 1 );

		$instance->doImport();
	}

	public function testDoImportWithError() {

		$importContents = new ImportContents();

		$importContents->addError( 'Bar' );
		$importContents->setVersion( 1 );

		$this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() )
			->method( 'getContentList' )
			->will( $this->returnValue( [ 'Foo' => [ $importContents ] ] ) );

		$this->jsonImportContentsFileDirReader->expects( $this->atLeastOnce() )
			->method( 'getErrors' )
			->will( $this->returnValue( [ 'Error' ] ) );

		$this->contentCreator->expects( $this->never() )
			->method( 'create' );

		$instance = new Importer(
			new JsoncontentIterator( $this->jsonImportContentsFileDirReader ),
			$this->contentCreator
		);

		$instance->setMessageReporter( $this->messageReporter );
		$instance->setReqVersion( 1 );

		$instance->doImport();
	}

}