mediawiki-extensions-Echo/tests/phpunit/mapper/EventMapperTest.php
Thiemo Kreuz 5941ef476c Replace @expectedException with $this->expectException()
The @expectedException annotation got deprecated in PHPUnit 7.5, and
removed in PHPUnit 8.0. This was done because the annotation does have
two disadvantages:
* The class name is encoded in string, where it is not easy to find for
  all IDEs and tools.
* it did not allow to say exactly *when* the exception is expected.

Change-Id: I96862a18874f36355e817accd64d8703c1965c86
2019-02-19 20:54:15 +00:00

127 lines
2.9 KiB
PHP

<?php
/**
* @covers EchoEventMapper
*/
class EchoEventMapperTest extends MediaWikiTestCase {
public function provideDataTestInsert() {
return [
[
'successful insert with next sequence = 1',
[ 'insert' => true, 'insertId' => 1 ],
1
],
[
'successful insert with insert id = 2',
[ 'insert' => true, 'insertId' => 2 ],
2
]
];
}
/**
* @dataProvider provideDataTestInsert
*/
public function testInsert( $message, $dbResult, $result ) {
$event = $this->mockEchoEvent();
$eventMapper = new EchoEventMapper( $this->mockMWEchoDbFactory( $dbResult ) );
$this->assertEquals( $result, $eventMapper->insert( $event ), $message );
}
/**
* Successful fetchById()
*/
public function testSuccessfulFetchById() {
$eventMapper = new EchoEventMapper(
$this->mockMWEchoDbFactory(
[
'selectRow' => (object)[
'event_id' => 1,
'event_type' => 'test',
'event_variant' => '',
'event_extra' => '',
'event_page_id' => '',
'event_agent_id' => '',
'event_agent_ip' => '',
'event_deleted' => 0,
]
]
)
);
$res = $eventMapper->fetchById( 1 );
$this->assertInstanceOf( EchoEvent::class, $res );
}
public function testUnsuccessfulFetchById() {
$eventMapper = new EchoEventMapper(
$this->mockMWEchoDbFactory(
[
'selectRow' => false
]
)
);
$this->expectException( MWException::class );
$eventMapper->fetchById( 1 );
}
/**
* Mock object of EchoEvent
*/
protected function mockEchoEvent() {
$event = $this->getMockBuilder( EchoEvent::class )
->disableOriginalConstructor()
->getMock();
$event->expects( $this->any() )
->method( 'toDbArray' )
->will( $this->returnValue( [] ) );
return $event;
}
/**
* Mock object of MWEchoDbFactory
*/
protected function mockMWEchoDbFactory( $dbResult ) {
$dbFactory = $this->getMockBuilder( MWEchoDbFactory::class )
->disableOriginalConstructor()
->getMock();
$dbFactory->expects( $this->any() )
->method( 'getEchoDb' )
->will( $this->returnValue( $this->mockDb( $dbResult ) ) );
return $dbFactory;
}
/**
* Returns a mock database object
* @return \Wikimedia\Rdbms\IDatabase
*/
protected function mockDb( array $dbResult ) {
$dbResult += [
'insert' => '',
'insertId' => '',
'select' => '',
'selectRow' => ''
];
$db = $this->getMockBuilder( DatabaseMysqli::class )
->disableOriginalConstructor()
->getMock();
$db->expects( $this->any() )
->method( 'insert' )
->will( $this->returnValue( $dbResult['insert'] ) );
$db->expects( $this->any() )
->method( 'insertId' )
->will( $this->returnValue( $dbResult['insertId'] ) );
$db->expects( $this->any() )
->method( 'select' )
->will( $this->returnValue( $dbResult['select'] ) );
$db->expects( $this->any() )
->method( 'selectRow' )
->will( $this->returnValue( $dbResult['selectRow'] ) );
return $db;
}
}