| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Notifications\DbFactory; |
| 6 |
use MediaWiki\Extension\Notifications\Mapper\TargetPageMapper; |
| 7 |
use MediaWiki\Extension\Notifications\Model\TargetPage; |
| 8 |
use MediaWikiUnitTestCase; |
| 9 |
use Wikimedia\Rdbms\FakeResultWrapper; |
| 10 |
use Wikimedia\Rdbms\IDatabase; |
| 11 |
use Wikimedia\Rdbms\InsertQueryBuilder; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class TargetPageMapperTest extends MediaWikiUnitTestCase { |
| 17 |
|
| 18 |
public static function provideDataTestInsert() { |
| 19 |
return [ |
| 20 |
[ |
| 21 |
'successful insert with next sequence = 1', |
| 22 |
[ 'insert' => true, 'insertId' => 2 ], |
| 23 |
1 |
| 24 |
], |
| 25 |
[ |
| 26 |
'successful insert with insert id = 2', |
| 27 |
[ 'insert' => true, 'insertId' => 2 ], |
| 28 |
2 |
| 29 |
], |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function testInsert( $message, $dbResult, $result ) { |
| 37 |
$target = $this->mockTargetPage(); |
| 38 |
$targetMapper = new TargetPageMapper( $this->mockDbFactory( $dbResult ) ); |
| 39 |
$this->assertEquals( $result, $targetMapper->insert( $target ), $message ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected function mockTargetPage() { |
| 47 |
$target = $this->createMock( TargetPage::class ); |
| 48 |
$target->method( 'toDbArray' ) |
| 49 |
->willReturn( [] ); |
| 50 |
$target->method( 'getPageId' ) |
| 51 |
->willReturn( 2 ); |
| 52 |
$target->method( 'getEventId' ) |
| 53 |
->willReturn( 3 ); |
| 54 |
|
| 55 |
return $target; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected function mockDbFactory( $dbResult ) { |
| 64 |
$dbFactory = $this->createMock( DbFactory::class ); |
| 65 |
$dbFactory->method( 'getEchoDb' ) |
| 66 |
->willReturn( $this->mockDb( $dbResult ) ); |
| 67 |
|
| 68 |
return $dbFactory; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
protected function mockDb( array $dbResult ) { |
| 77 |
$dbResult += [ |
| 78 |
'insert' => '', |
| 79 |
'insertId' => '', |
| 80 |
'select' => [], |
| 81 |
'delete' => '' |
| 82 |
]; |
| 83 |
$db = $this->createMock( IDatabase::class ); |
| 84 |
$db->method( 'insert' ) |
| 85 |
->willReturn( $dbResult['insert'] ); |
| 86 |
$db->method( 'insertId' ) |
| 87 |
->willReturn( $dbResult['insertId'] ); |
| 88 |
$db->method( 'select' ) |
| 89 |
->willReturn( new FakeResultWrapper( $dbResult['select'] ) ); |
| 90 |
$db->method( 'delete' ) |
| 91 |
->willReturn( $dbResult['delete'] ); |
| 92 |
$db->method( 'newInsertQueryBuilder' ) |
| 93 |
->willReturnCallback( static function () use ( $db ) { |
| 94 |
return new InsertQueryBuilder( $db ); |
| 95 |
} ); |
| 96 |
|
| 97 |
return $db; |
| 98 |
} |
| 99 |
|
| 100 |
} |