2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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-05-10 21:29:24 +00:00
|
|
|
[ 'nextSequenceValue' => 1, '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',
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'nextSequenceValue' => null, 'insert' => true, 'insertId' => 2 ],
|
2014-07-18 03:58:21 +00:00
|
|
|
2
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'unsuccessful insert',
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'nextSequenceValue' => null, 'insert' => false, 'insertId' => 2 ],
|
2014-07-18 03:58:21 +00:00
|
|
|
false
|
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 );
|
|
|
|
$this->assertInstanceOf( 'EchoEvent', $res );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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() {
|
|
|
|
$event = $this->getMockBuilder( 'EchoEvent' )
|
|
|
|
->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 ) {
|
|
|
|
$dbFactory = $this->getMockBuilder( 'MWEchoDbFactory' )
|
|
|
|
->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of DatabaseMysql ( DatabaseBase )
|
|
|
|
*/
|
|
|
|
protected function mockDb( array $dbResult ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult += [
|
2014-07-18 03:58:21 +00:00
|
|
|
'nextSequenceValue' => '',
|
|
|
|
'insert' => '',
|
|
|
|
'insertId' => '',
|
|
|
|
'select' => '',
|
|
|
|
'selectRow' => ''
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
$db = $this->getMockBuilder( 'DatabaseMysql' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$db->expects( $this->any() )
|
|
|
|
->method( 'nextSequenceValue' )
|
|
|
|
->will( $this->returnValue( $dbResult['nextSequenceValue'] ) );
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|