2014-08-07 19:10:37 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2019-10-23 10:23:09 +00:00
|
|
|
* @covers \EchoAbstractMapper
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2019-07-10 20:47:28 +00:00
|
|
|
class EchoAbstractMapperTest extends MediaWikiUnitTestCase {
|
2014-08-07 19:10:37 +00:00
|
|
|
|
2019-02-19 10:40:12 +00:00
|
|
|
/**
|
|
|
|
* @return array [ 'mapper' => EchoAbstractMapper, 'property' => ReflectionProperty ]
|
|
|
|
*/
|
2014-08-07 19:10:37 +00:00
|
|
|
public function testAttachListener() {
|
|
|
|
$mapper = new EchoAbstractMapperStub();
|
2015-10-01 13:48:52 +00:00
|
|
|
$mapper->attachListener( 'testMethod', 'key_a', function () {
|
|
|
|
} );
|
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 );
|
2015-10-01 13:48:52 +00:00
|
|
|
$mapper->attachListener( 'nonExistingMethod', 'key_a', function () {
|
|
|
|
} );
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAttachListener
|
|
|
|
*/
|
|
|
|
public function testGetMethodListeners( $data ) {
|
2019-02-19 10:40:12 +00:00
|
|
|
/** @var EchoAbstractMapper $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 ) {
|
2019-02-19 10:40:12 +00:00
|
|
|
/** @var EchoAbstractMapper $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 ) {
|
2019-02-19 10:40:12 +00:00
|
|
|
/** @var EchoAbstractMapper $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'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|