test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / maintenance / fetchTextTest.php
blobe8df199e778632181cc58e38d3647b9909d7fea0
1 <?php
3 require_once __DIR__ . "/../../../maintenance/fetchText.php";
5 /**
6 * Mock for the input/output of FetchText
8 * FetchText internally tries to access stdin and stdout. We mock those aspects
9 * for testing.
11 class SemiMockedFetchText extends FetchText {
13 /**
14 * @var String|null Text to pass as stdin
16 private $mockStdinText = null;
18 /**
19 * @var bool Whether or not a text for stdin has been provided
21 private $mockSetUp = false;
23 /**
24 * @var Array Invocation counters for the mocked aspects
26 private $mockInvocations = array( 'getStdin' => 0 );
29 /**
30 * Data for the fake stdin
32 * @param $stdin String The string to be used instead of stdin
34 function mockStdin( $stdin ) {
35 $this->mockStdinText = $stdin;
36 $this->mockSetUp = true;
39 /**
40 * Gets invocation counters for mocked methods.
42 * @return Array An array, whose keys are function names. The corresponding values
43 * denote the number of times the function has been invoked.
45 function mockGetInvocations() {
46 return $this->mockInvocations;
49 // -----------------------------------------------------------------
50 // Mocked functions from FetchText follow.
52 function getStdin( $len = null ) {
53 $this->mockInvocations['getStdin']++;
54 if ( $len !== null ) {
55 throw new PHPUnit_Framework_ExpectationFailedException(
56 "Tried to get stdin with non null parameter" );
59 if ( !$this->mockSetUp ) {
60 throw new PHPUnit_Framework_ExpectationFailedException(
61 "Tried to get stdin before setting up rerouting" );
64 return fopen( 'data://text/plain,' . $this->mockStdinText, 'r' );
68 /**
69 * TestCase for FetchText
71 * @group Database
72 * @group Dump
74 class FetchTextTest extends MediaWikiTestCase {
76 // We add 5 Revisions for this test. Their corresponding text id's
77 // are stored in the following 5 variables.
78 private $textId1;
79 private $textId2;
80 private $textId3;
81 private $textId4;
82 private $textId5;
85 /**
86 * @var Exception|null As the current MediaWikiTestCase::run is not
87 * robust enough to recover from thrown exceptions directly, we cannot
88 * throw frow within addDBData, although it would be appropriate. Hence,
89 * we catch the exception and store it until we are in setUp and may
90 * finally rethrow the exception without crashing the test suite.
92 private $exceptionFromAddDBData;
94 /**
95 * @var FetchText the (mocked) FetchText that is to test
97 private $fetchText;
99 /**
100 * Adds a revision to a page, while returning the resuting text's id
102 * @param $page WikiPage The page to add the revision to
103 * @param $text String The revisions text
104 * @param $text String The revisions summare
106 * @throws MWExcepion
108 private function addRevision( $page, $text, $summary ) {
109 $status = $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), $summary );
110 if ( $status->isGood() ) {
111 $value = $status->getValue();
112 $revision = $value['revision'];
113 $id = $revision->getTextId();
114 if ( $id > 0 ) {
115 return $id;
118 throw new MWException( "Could not determine text id" );
122 function addDBData() {
123 $this->tablesUsed[] = 'page';
124 $this->tablesUsed[] = 'revision';
125 $this->tablesUsed[] = 'text';
127 $wikitextNamespace = $this->getDefaultWikitextNS();
129 try {
130 $title = Title::newFromText( 'FetchTextTestPage1', $wikitextNamespace );
131 $page = WikiPage::factory( $title );
132 $this->textId1 = $this->addRevision( $page, "FetchTextTestPage1Text1", "FetchTextTestPage1Summary1" );
134 $title = Title::newFromText( 'FetchTextTestPage2', $wikitextNamespace );
135 $page = WikiPage::factory( $title );
136 $this->textId2 = $this->addRevision( $page, "FetchTextTestPage2Text1", "FetchTextTestPage2Summary1" );
137 $this->textId3 = $this->addRevision( $page, "FetchTextTestPage2Text2", "FetchTextTestPage2Summary2" );
138 $this->textId4 = $this->addRevision( $page, "FetchTextTestPage2Text3", "FetchTextTestPage2Summary3" );
139 $this->textId5 = $this->addRevision( $page, "FetchTextTestPage2Text4 some additional Text ", "FetchTextTestPage2Summary4 extra " );
140 } catch ( Exception $e ) {
141 // We'd love to pass $e directly. However, ... see
142 // documentation of exceptionFromAddDBData
143 $this->exceptionFromAddDBData = $e;
148 protected function setUp() {
149 parent::setUp();
151 // Check if any Exception is stored for rethrowing from addDBData
152 if ( $this->exceptionFromAddDBData !== null ) {
153 throw $this->exceptionFromAddDBData;
156 $this->fetchText = new SemiMockedFetchText();
161 * Helper to relate FetchText's input and output
163 private function assertFilter( $input, $expectedOutput ) {
164 $this->fetchText->mockStdin( $input );
165 $this->fetchText->execute();
166 $invocations = $this->fetchText->mockGetInvocations();
167 $this->assertEquals( 1, $invocations['getStdin'],
168 "getStdin invocation counter" );
169 $this->expectOutputString( $expectedOutput );
173 // Instead of the following functions, a data provider would be great.
174 // However, as data providers are evaluated /before/ addDBData, a data
175 // provider would not know the required ids.
177 function testExistingSimple() {
178 $this->assertFilter( $this->textId2,
179 $this->textId2 . "\n23\nFetchTextTestPage2Text1" );
182 function testExistingSimpleWithNewline() {
183 $this->assertFilter( $this->textId2 . "\n",
184 $this->textId2 . "\n23\nFetchTextTestPage2Text1" );
187 function testExistingSeveral() {
188 $this->assertFilter( "$this->textId1\n$this->textId5\n"
189 . "$this->textId3\n$this->textId3",
190 implode( "", array(
191 $this->textId1 . "\n23\nFetchTextTestPage1Text1",
192 $this->textId5 . "\n44\nFetchTextTestPage2Text4 "
193 . "some additional Text",
194 $this->textId3 . "\n23\nFetchTextTestPage2Text2",
195 $this->textId3 . "\n23\nFetchTextTestPage2Text2"
196 ) ) );
199 function testEmpty() {
200 $this->assertFilter( "", null );
203 function testNonExisting() {
204 $this->assertFilter( $this->textId5 + 10, ( $this->textId5 + 10 ) . "\n-1\n" );
207 function testNegativeInteger() {
208 $this->assertFilter( "-42", "-42\n-1\n" );
211 function testFloatingPointNumberExisting() {
212 // float -> int -> revision
213 $this->assertFilter( $this->textId3 + 0.14159,
214 $this->textId3 . "\n23\nFetchTextTestPage2Text2" );
217 function testFloatingPointNumberNonExisting() {
218 $this->assertFilter( $this->textId5 + 3.14159,
219 ( $this->textId5 + 3 ) . "\n-1\n" );
222 function testCharacters() {
223 $this->assertFilter( "abc", "0\n-1\n" );
226 function testMix() {
227 $this->assertFilter( "ab\n" . $this->textId4 . ".5cd\n\nefg\n" . $this->textId2
228 . "\n" . $this->textId3,
229 implode( "", array(
230 "0\n-1\n",
231 $this->textId4 . "\n23\nFetchTextTestPage2Text3",
232 "0\n-1\n",
233 "0\n-1\n",
234 $this->textId2 . "\n23\nFetchTextTestPage2Text1",
235 $this->textId3 . "\n23\nFetchTextTestPage2Text2"
236 ) ) );