Skip to content

Commit 782d524

Browse files
committed
Add phpunit workflow
1 parent c6c962d commit 782d524

File tree

15 files changed

+178
-134
lines changed

15 files changed

+178
-134
lines changed

.github/workflows/ci.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,36 @@ jobs:
4545

4646
- name: Run tests
4747
run: npm test
48+
49+
phpunit:
50+
runs-on: ubuntu-latest
51+
steps:
52+
53+
- name: Setup PHP
54+
uses: shivammathur/setup-php@v2
55+
with:
56+
php-version: 8.1
57+
tools: composer:2
58+
59+
- name: Clone MediaWiki
60+
run: git clone --depth 1 -b REL1_43 https://gerrit.wikimedia.org/r/mediawiki/core.git mediawiki
61+
62+
- name: Install dependencies
63+
working-directory: mediawiki
64+
run: composer update
65+
66+
- name: Composer require
67+
working-directory: mediawiki
68+
run: |
69+
REPO="${{ github.event.repository.name }}"
70+
PACKAGE="${REPO#mwstake-}"
71+
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
72+
composer require "mwstake/${PACKAGE}:dev-${BRANCH}" --prefer-source
73+
74+
- name: Create LocalSettings.php
75+
working-directory: mediawiki
76+
run: echo '<?php $wgServer = "";' > LocalSettings.php
77+
78+
- name: Run tests
79+
working-directory: mediawiki
80+
run: composer phpunit:unit -- --colors=always "extensions/${{ github.event.repository.name }}/tests/phpunit/unit" --exclude-group Broken,ParserFuzz,Stub

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"autoload": {
2020
"psr-4": {
2121
"MWStake\\MediaWiki\\Component\\DataStore\\": "src/",
22-
"MWStake\\MediaWiki\\Component\\DataStore\\Tests\\": "tests/phpunit/"
22+
"MWStake\\MediaWiki\\Component\\DataStore\\Tests\\Unit\\": "tests/phpunit/unit/",
23+
"MWStake\\MediaWiki\\Component\\DataStore\\Tests\\Integration\\": "tests/phpunit/integration/"
2324
},
2425
"files": [
2526
"bootstrap.php"

src/Filter/Boolean.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function doesMatch( $dataSet ) {
3939
}
4040

4141
/**
42-
* Converts specific strings to boolean values
42+
* Normalise a value to boolean
4343
* Returns null if the value is invalid
4444
*
4545
* @param mixed $value The value to normalise
@@ -50,12 +50,22 @@ private function normaliseBoolean( $value ) {
5050
return $value;
5151
}
5252

53+
if ( is_int( $value ) ) {
54+
if ( $value === 1 ) {
55+
return true;
56+
}
57+
if ( $value === 0 ) {
58+
return false;
59+
}
60+
return null;
61+
}
62+
5363
if ( is_string( $value ) ) {
5464
$lower = strtolower( $value );
55-
if ( in_array( $lower, [ 'true', 'yes' ], true ) ) {
65+
if ( in_array( $lower, [ 'true', 'yes', '1' ] ) ) {
5666
return true;
5767
}
58-
if ( in_array( $lower, [ 'false', 'no' ], true ) ) {
68+
if ( in_array( $lower, [ 'false', 'no', '0' ] ) ) {
5969
return false;
6070
}
6171
}

tests/phpunit/Filter/BooleanTest.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

tests/phpunit/Filter/TemplateTitleTest.php renamed to tests/phpunit/integration/Filter/TemplateTitleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace MWStake\MediaWiki\Component\DataStore\Tests\Filter;
3+
namespace MWStake\MediaWiki\Component\DataStore\Tests\Integration\Filter;
44

5+
use MediaWikiIntegrationTestCase;
56
use MWStake\MediaWiki\Component\DataStore\Filter;
67
use MWStake\MediaWiki\Component\DataStore\Record;
7-
use PHPUnit\Framework\TestCase;
88

9-
class TemplateTitleTest extends TestCase {
9+
class TemplateTitleTest extends MediaWikiIntegrationTestCase {
1010
/**
1111
* @covers \MWStake\MediaWiki\Component\DataStore\Filter\TemplateTitle::matches
1212
*/

tests/phpunit/Filter/TitleTest.php renamed to tests/phpunit/integration/Filter/TitleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace MWStake\MediaWiki\Component\DataStore\Tests\Filter;
3+
namespace MWStake\MediaWiki\Component\DataStore\Tests\Integration\Filter;
44

5+
use MediaWikiIntegrationTestCase;
56
use MWStake\MediaWiki\Component\DataStore\Filter;
67
use MWStake\MediaWiki\Component\DataStore\Record;
7-
use PHPUnit\Framework\TestCase;
88

9-
class TitleTest extends TestCase {
9+
class TitleTest extends MediaWikiIntegrationTestCase {
1010
/**
1111
* @covers \MWStake\MediaWiki\Component\DataStore\Filter\Title::matches
1212
*/
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace MWStake\MediaWiki\Component\DataStore\Tests\Unit\Filter;
4+
5+
use MediaWikiUnitTestCase;
6+
use MWStake\MediaWiki\Component\DataStore\Filter\Boolean;
7+
use MWStake\MediaWiki\Component\DataStore\Record;
8+
9+
class BooleanTest extends MediaWikiUnitTestCase {
10+
/**
11+
* @covers \MWStake\MediaWiki\Component\DataStore\Filter\Boolean::matches
12+
*/
13+
public function testPositive() {
14+
$filter = new Boolean( [
15+
'field' => 'field1',
16+
'comparison' => 'eq',
17+
'value' => true
18+
] );
19+
20+
$result = $filter->matches( new Record( (object)[
21+
'field1' => true,
22+
'field2' => false
23+
] ) );
24+
25+
$this->assertTrue( $result );
26+
}
27+
28+
/**
29+
* @covers MWStake\MediaWiki\Component\DataStore\Filter\Boolean::matches
30+
*/
31+
public function testNegative() {
32+
$filter = new Boolean( [
33+
'field' => 'field1',
34+
'comparison' => 'eq',
35+
'value' => false
36+
] );
37+
38+
$result = $filter->matches( new Record( (object)[
39+
'field1' => true,
40+
'field2' => false
41+
] ) );
42+
43+
$this->assertFalse( $result );
44+
}
45+
46+
/**
47+
* @dataProvider provideMatchesValues
48+
* @param bool $expectation
49+
* @param string $comparison
50+
* @param mixed $fieldValue
51+
* @param mixed $filterValue
52+
* @covers \MWStake\MediaWiki\Component\DataStore\Filter\Boolean::matches
53+
*/
54+
public function testMatches( $expectation, $comparison, $fieldValue, $filterValue ) {
55+
$filter = new Boolean( [
56+
Boolean::KEY_FIELD => 'field_A',
57+
Boolean::KEY_VALUE => $filterValue,
58+
Boolean::KEY_COMPARISON => $comparison
59+
] );
60+
61+
$dataSet = new Record( (object)[
62+
'field_A' => $fieldValue
63+
] );
64+
65+
if ( $expectation ) {
66+
$this->assertTrue( $filter->matches( $dataSet ), 'Filter should apply' );
67+
} else {
68+
$this->assertFalse( $filter->matches( $dataSet ), 'Filter should not apply' );
69+
}
70+
}
71+
72+
public function provideMatchesValues() {
73+
return [
74+
[ true, Boolean::COMPARISON_EQUALS, true, true ],
75+
[ true, Boolean::COMPARISON_EQUALS, 1, true ],
76+
[ true, Boolean::COMPARISON_EQUALS, '1', true ],
77+
[ true, Boolean::COMPARISON_EQUALS, false, false ],
78+
[ true, Boolean::COMPARISON_EQUALS, 0, false ],
79+
[ true, Boolean::COMPARISON_EQUALS, '0', false ],
80+
[ true, Boolean::COMPARISON_NOT_EQUALS, true, false ],
81+
[ true, Boolean::COMPARISON_NOT_EQUALS, 1, false ],
82+
[ true, Boolean::COMPARISON_NOT_EQUALS, '1', false ],
83+
[ true, Boolean::COMPARISON_NOT_EQUALS, false, true ],
84+
[ true, Boolean::COMPARISON_NOT_EQUALS, 0, true ],
85+
[ true, Boolean::COMPARISON_NOT_EQUALS, '0', true ],
86+
[ false, Boolean::COMPARISON_EQUALS, true, false ],
87+
[ false, Boolean::COMPARISON_EQUALS, 1, false ],
88+
[ false, Boolean::COMPARISON_EQUALS, '1', false ],
89+
[ false, Boolean::COMPARISON_EQUALS, false, true ],
90+
[ false, Boolean::COMPARISON_EQUALS, 0, true ],
91+
[ false, Boolean::COMPARISON_EQUALS, '0', true ],
92+
[ false, Boolean::COMPARISON_NOT_EQUALS, true, true ],
93+
[ false, Boolean::COMPARISON_NOT_EQUALS, 1, true ],
94+
[ false, Boolean::COMPARISON_NOT_EQUALS, '1', true ],
95+
[ false, Boolean::COMPARISON_NOT_EQUALS, false, false ],
96+
[ false, Boolean::COMPARISON_NOT_EQUALS, 0, false ],
97+
[ false, Boolean::COMPARISON_NOT_EQUALS, '0', false ]
98+
];
99+
}
100+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace MWStake\MediaWiki\Component\DataStore\Tests\Filter;
3+
namespace MWStake\MediaWiki\Component\DataStore\Tests\Unit\Filter;
44

5+
use MediaWikiUnitTestCase;
56
use MWStake\MediaWiki\Component\DataStore\Filter;
67
use MWStake\MediaWiki\Component\DataStore\Record;
7-
use PHPUnit\Framework\TestCase;
88

9-
class DateTest extends TestCase {
9+
class DateTest extends MediaWikiUnitTestCase {
1010
/**
1111
* @covers \MWStake\MediaWiki\Component\DataStore\Filter\Date::matches
1212
*/

tests/phpunit/Filter/ListValueTest.php renamed to tests/phpunit/unit/Filter/ListValueTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace MWStake\MediaWiki\Component\DataStore\Tests\Filter;
3+
namespace MWStake\MediaWiki\Component\DataStore\Tests\Unit\Filter;
44

5+
use MediaWikiUnitTestCase;
56
use MWStake\MediaWiki\Component\DataStore\Filter;
67
use MWStake\MediaWiki\Component\DataStore\Record;
7-
use PHPUnit\Framework\TestCase;
88

9-
class ListValueTest extends TestCase {
9+
class ListValueTest extends MediaWikiUnitTestCase {
1010
/**
1111
* @covers \MWStake\MediaWiki\Component\DataStore\Filter\ListValue::matches
1212
*/

tests/phpunit/Filter/NumericValueTest.php renamed to tests/phpunit/unit/Filter/NumericValueTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace MWStake\MediaWiki\Component\DataStore\Tests\Filter;
3+
namespace MWStake\MediaWiki\Component\DataStore\Tests\Unit\Filter;
44

5+
use MediaWikiUnitTestCase;
56
use MWStake\MediaWiki\Component\DataStore\Filter;
67
use MWStake\MediaWiki\Component\DataStore\Record;
7-
use PHPUnit\Framework\TestCase;
88

9-
class NumericValueTest extends TestCase {
9+
class NumericValueTest extends MediaWikiUnitTestCase {
1010
/**
1111
* @covers \MWStake\MediaWiki\Component\DataStore\Filter\NumericValue::matches
1212
*/

0 commit comments

Comments
 (0)