| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Unit; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\Notifications\Mapper\AbstractMapper; |
| 7 |
use MediaWiki\Extension\Notifications\Test\EchoAbstractMapperStub; |
| 8 |
use MediaWikiUnitTestCase; |
| 9 |
use ReflectionClass; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class AbstractMapperTest extends MediaWikiUnitTestCase { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public function testAttachListener() { |
| 20 |
$mapper = new EchoAbstractMapperStub(); |
| 21 |
$mapper->attachListener( 'testMethod', 'key_a', static function () { |
| 22 |
} ); |
| 23 |
|
| 24 |
$class = new ReflectionClass( EchoAbstractMapperStub::class ); |
| 25 |
$property = $class->getProperty( 'listeners' ); |
| 26 |
$listeners = $property->getValue( $mapper ); |
| 27 |
|
| 28 |
$this->assertArrayHasKey( 'testMethod', $listeners ); |
| 29 |
$this->assertArrayHasKey( 'key_a', $listeners['testMethod'] ); |
| 30 |
$this->assertIsCallable( $listeners['testMethod']['key_a'] ); |
| 31 |
|
| 32 |
return [ 'mapper' => $mapper, 'property' => $property ]; |
| 33 |
} |
| 34 |
|
| 35 |
public function testAttachListenerWithException() { |
| 36 |
$mapper = new EchoAbstractMapperStub(); |
| 37 |
$this->expectException( InvalidArgumentException::class ); |
| 38 |
$mapper->attachListener( 'nonExistingMethod', 'key_a', static function () { |
| 39 |
} ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function testGetMethodListeners( $data ) { |
| 46 |
|
| 47 |
$mapper = $data['mapper']; |
| 48 |
|
| 49 |
$listeners = $mapper->getMethodListeners( 'testMethod' ); |
| 50 |
$this->assertArrayHasKey( 'key_a', $listeners ); |
| 51 |
$this->assertIsCallable( $listeners['key_a'] ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function testGetMethodListenersWithException( $data ) { |
| 58 |
|
| 59 |
$mapper = $data['mapper']; |
| 60 |
|
| 61 |
$this->expectException( InvalidArgumentException::class ); |
| 62 |
$mapper->getMethodListeners( 'nonExistingMethod' ); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function testDetachListener( $data ) { |
| 69 |
|
| 70 |
$mapper = $data['mapper']; |
| 71 |
|
| 72 |
$property = $data['property']; |
| 73 |
|
| 74 |
$mapper->detachListener( 'testMethod', 'key_a' ); |
| 75 |
$listeners = $property->getValue( $mapper ); |
| 76 |
$this->assertArrayHasKey( 'testMethod', $listeners ); |
| 77 |
$this->assertTrue( !isset( $listeners['testMethod']['key_a'] ) ); |
| 78 |
} |
| 79 |
|
| 80 |
} |