summaryrefslogtreecommitdiff
blob: cf91a4593653b19a9472f2fc7e5972497e063e90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

class EchoAbstractMapperTest extends MediaWikiTestCase {

	public function testAttachListener() {
		$mapper = new EchoAbstractMapperStub();
		$mapper->attachListener( 'testMethod', 'key_a', function() {} );

		$class = new ReflectionClass( 'EchoAbstractMapperStub' );
		$property = $class->getProperty( 'listeners' );
		$property->setAccessible( true );
		$listeners = $property->getValue( $mapper );

		$this->assertArrayHasKey( 'testMethod', $listeners );
		$this->assertArrayHasKey( 'key_a', $listeners['testMethod'] );
		$this->assertTrue( is_callable( $listeners['testMethod']['key_a'] ) );
		return array( 'mapper' => $mapper, 'property' => $property );
	}

	/**
	 * @expectedException MWException
	 */
	public function testAttachListenerWithException() {
		$mapper = new EchoAbstractMapperStub();
		$mapper->attachListener( 'nonExistingMethod', 'key_a', function() {} );
	}

	/**
	 * @depends testAttachListener
	 */
	public function testGetMethodListeners( $data ) {
		$mapper = $data['mapper'];
		$property = $data['property'];

		$listeners = $mapper->getMethodListeners( 'testMethod' );
		$this->assertArrayHasKey( 'key_a', $listeners );
		$this->assertTrue( is_callable( $listeners['key_a'] ) );
	}

	/**
	 * @depends testAttachListener
	 * @expectedException MWException
	 */
	public function testGetMethodListenersWithException( $data ) {
		$mapper = $data['mapper'];
		$property = $data['property'];

		$listeners = $mapper->getMethodListeners( 'nonExistingMethod' );
	}

	/**
	 * @depends testAttachListener
	 */
	public function testDetachListener( $data ) {
		$mapper = $data['mapper'];
		$property = $data['property'];

		$mapper->detachListener( 'testMethod', 'key_a' );
		$listeners = $property->getValue( $mapper );
		$this->assertArrayHasKey( 'testMethod', $listeners );
		$this->assertTrue( !isset( $listeners['testMethod']['key_a'] ) );
	}

}

/**
 * Create a stub class for testing the abstract class
 */
class EchoAbstractMapperStub extends EchoAbstractMapper {

	public function testMethod() {

	}

}