2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
|
|
|
* @covers EchoEventMapper
|
|
|
|
*/
|
2014-07-18 03:58:21 +00:00
|
|
|
class EchoEventMapperTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
public function provideDataTestInsert() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'successful insert with next sequence = 1',
|
2017-09-06 16:47:22 +00:00
|
|
|
[ 'insert' => true, 'insertId' => 1 ],
|
2014-07-18 03:58:21 +00:00
|
|
|
1
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'successful insert with insert id = 2',
|
2017-09-06 16:47:22 +00:00
|
|
|
[ 'insert' => true, 'insertId' => 2 ],
|
2014-07-18 03:58:21 +00:00
|
|
|
2
|
2018-10-26 21:36:39 +00:00
|
|
|
]
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
|
|
|
'selectRow' => (object)[
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_id' => 1,
|
|
|
|
'event_type' => 'test',
|
|
|
|
'event_variant' => '',
|
|
|
|
'event_extra' => '',
|
|
|
|
'event_page_id' => '',
|
|
|
|
'event_agent_id' => '',
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_agent_ip' => '',
|
|
|
|
'event_deleted' => 0,
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
|
|
|
]
|
2014-07-18 03:58:21 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$res = $eventMapper->fetchById( 1 );
|
2019-02-19 10:35:54 +00:00
|
|
|
$this->assertInstanceOf( EchoEvent::class, $res );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException MWException
|
|
|
|
*/
|
|
|
|
public function testUnsuccessfulFetchById() {
|
|
|
|
$eventMapper = new EchoEventMapper(
|
|
|
|
$this->mockMWEchoDbFactory(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'selectRow' => false
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
2014-07-18 03:58:21 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$res = $eventMapper->fetchById( 1 );
|
|
|
|
$this->assertInstanceOf( 'EchoEvent', $res );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of EchoEvent
|
|
|
|
*/
|
|
|
|
protected function mockEchoEvent() {
|
2019-02-19 10:35:54 +00:00
|
|
|
$event = $this->getMockBuilder( EchoEvent::class )
|
2014-07-18 03:58:21 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'toDbArray' )
|
2016-12-05 18:51:07 +00:00
|
|
|
->will( $this->returnValue( [] ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
protected function mockMWEchoDbFactory( $dbResult ) {
|
2019-02-19 10:35:54 +00:00
|
|
|
$dbFactory = $this->getMockBuilder( MWEchoDbFactory::class )
|
2014-07-18 03:58:21 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$dbFactory->expects( $this->any() )
|
|
|
|
->method( 'getEchoDb' )
|
|
|
|
->will( $this->returnValue( $this->mockDb( $dbResult ) ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $dbFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-16 18:58:52 +00:00
|
|
|
* Returns a mock database object
|
|
|
|
* @return \Wikimedia\Rdbms\IDatabase
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
protected function mockDb( array $dbResult ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult += [
|
2014-07-18 03:58:21 +00:00
|
|
|
'insert' => '',
|
|
|
|
'insertId' => '',
|
|
|
|
'select' => '',
|
|
|
|
'selectRow' => ''
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2019-02-19 10:35:54 +00:00
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
2014-07-18 03:58:21 +00:00
|
|
|
->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'] ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|