2014-08-07 19:10:37 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\AbstractMapper;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2022-11-02 20:47:04 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Mapper\AbstractMapper
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2022-11-02 20:47:04 +00:00
|
|
|
class AbstractMapperTest extends MediaWikiUnitTestCase {
|
2014-08-07 19:10:37 +00:00
|
|
|
|
2019-02-19 10:40:12 +00:00
|
|
|
/**
|
2022-11-02 20:47:04 +00:00
|
|
|
* @return array [ 'mapper' => AbstractMapper, 'property' => ReflectionProperty ]
|
2019-02-19 10:40:12 +00:00
|
|
|
*/
|
2014-08-07 19:10:37 +00:00
|
|
|
public function testAttachListener() {
|
|
|
|
$mapper = new EchoAbstractMapperStub();
|
2021-05-04 16:06:42 +00:00
|
|
|
$mapper->attachListener( 'testMethod', 'key_a', static function () {
|
2015-10-01 13:48:52 +00:00
|
|
|
} );
|
2014-08-07 19:10:37 +00:00
|
|
|
|
2019-02-19 10:35:54 +00:00
|
|
|
$class = new ReflectionClass( EchoAbstractMapperStub::class );
|
2014-08-07 19:10:37 +00:00
|
|
|
$property = $class->getProperty( 'listeners' );
|
|
|
|
$property->setAccessible( true );
|
|
|
|
$listeners = $property->getValue( $mapper );
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'testMethod', $listeners );
|
|
|
|
$this->assertArrayHasKey( 'key_a', $listeners['testMethod'] );
|
2020-08-10 10:54:10 +00:00
|
|
|
$this->assertIsCallable( $listeners['testMethod']['key_a'] );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ 'mapper' => $mapper, 'property' => $property ];
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAttachListenerWithException() {
|
|
|
|
$mapper = new EchoAbstractMapperStub();
|
2019-02-19 10:39:13 +00:00
|
|
|
$this->expectException( MWException::class );
|
2021-05-04 16:06:42 +00:00
|
|
|
$mapper->attachListener( 'nonExistingMethod', 'key_a', static function () {
|
2015-10-01 13:48:52 +00:00
|
|
|
} );
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAttachListener
|
|
|
|
*/
|
|
|
|
public function testGetMethodListeners( $data ) {
|
2022-11-02 20:47:04 +00:00
|
|
|
/** @var AbstractMapper $mapper */
|
2014-08-07 19:10:37 +00:00
|
|
|
$mapper = $data['mapper'];
|
|
|
|
|
|
|
|
$listeners = $mapper->getMethodListeners( 'testMethod' );
|
|
|
|
$this->assertArrayHasKey( 'key_a', $listeners );
|
2020-08-10 10:54:10 +00:00
|
|
|
$this->assertIsCallable( $listeners['key_a'] );
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAttachListener
|
|
|
|
*/
|
|
|
|
public function testGetMethodListenersWithException( $data ) {
|
2022-11-02 20:47:04 +00:00
|
|
|
/** @var AbstractMapper $mapper */
|
2014-08-07 19:10:37 +00:00
|
|
|
$mapper = $data['mapper'];
|
|
|
|
|
2019-02-19 10:39:13 +00:00
|
|
|
$this->expectException( MWException::class );
|
|
|
|
$mapper->getMethodListeners( 'nonExistingMethod' );
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAttachListener
|
|
|
|
*/
|
|
|
|
public function testDetachListener( $data ) {
|
2022-11-02 20:47:04 +00:00
|
|
|
/** @var AbstractMapper $mapper */
|
2014-08-07 19:10:37 +00:00
|
|
|
$mapper = $data['mapper'];
|
2019-02-19 10:40:12 +00:00
|
|
|
/** @var ReflectionProperty $property */
|
2014-08-07 19:10:37 +00:00
|
|
|
$property = $data['property'];
|
|
|
|
|
|
|
|
$mapper->detachListener( 'testMethod', 'key_a' );
|
|
|
|
$listeners = $property->getValue( $mapper );
|
|
|
|
$this->assertArrayHasKey( 'testMethod', $listeners );
|
|
|
|
$this->assertTrue( !isset( $listeners['testMethod']['key_a'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|