Completed
Push — master ( 03e47f...db33ac )
by WEBEWEB
06:42 queued 01:35
created

HookUtilityTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 68
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B testGetHooks() 0 61 6
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Tests\Utility;
13
14
use Exception;
15
use PHPUnit_Framework_TestCase;
16
use ReflectionClass;
17
use ReflectionMethod;
18
use WBW\Library\Core\Exception\Hook\HookMethodNotFoundException;
19
use WBW\Library\Core\Utility\HookUtility;
20
21
/**
22
 * Hook utility test.
23
 *
24
 * @author NdC/WBW <https://github.com/webeweb/>
25
 * @package WBW\Library\Core\Tests\Utility
26
 * @final
27
 */
28
final class HookUtilityTest extends PHPUnit_Framework_TestCase {
29
30
	/**
31
	 * Tests the getHooks() method.
32
	 */
33
	public function testGetHooks() {
34
35
		$classpath	 = getcwd() . "/Tests/Utility/";
36
		$namespace	 = "WBW\\Library\\Core\\Tests\\Utility\\";
37
38
		$hooks1 = HookUtility::getHooks($classpath, $namespace);
39
		foreach ($hooks1 as $current) {
40
41
			$this->assertEquals($classpath, $current["classpath"]);
42
			$this->assertEquals($namespace, $current["namespace"]);
43
			$this->assertStringEndsWith(".php", $current["filename"]);
44
			$this->assertContainsOnlyInstancesOf(ReflectionClass::class, [$current["class"]]);
45
			$this->assertNull($current["method"], "The method getHooks() does not return the expected value");
46
		}
47
48
		$hooks2 = HookUtility::getHooks($classpath, $namespace, "/Exception/");
49
		$this->assertCount(0, $hooks2);
50
51
		$hooks3 = HookUtility::getHooks($classpath, $namespace, "/HookUtilityTest/");
52
		$this->assertCount(1, $hooks3);
53
		foreach ($hooks3 as $current) {
54
55
			$this->assertEquals($classpath, $current["classpath"]);
56
			$this->assertEquals($namespace, $current["namespace"]);
57
			$this->assertEquals("HookUtilityTest.php", $current["filename"]);
58
			$this->assertContainsOnlyInstancesOf(ReflectionClass::class, [$current["class"]]);
59
			$this->assertNull($current["method"], "The method getHooks() does not return the expected value");
60
		}
61
62
		$hooks4 = HookUtility::getHooks($classpath, $namespace, "/HookUtilityTest/", "Exception");
63
		$this->assertCount(0, $hooks4);
64
65
		$hooks5 = HookUtility::getHooks($classpath, $namespace, "/HookUtilityTest/", PHPUnit_Framework_TestCase::class);
66
		$this->assertCount(1, $hooks5);
67
		foreach ($hooks5 as $current) {
68
69
			$this->assertEquals($classpath, $current["classpath"]);
70
			$this->assertEquals($namespace, $current["namespace"]);
71
			$this->assertEquals("HookUtilityTest.php", $current["filename"]);
72
			$this->assertContainsOnlyInstancesOf(ReflectionClass::class, [$current["class"]]);
73
			$this->assertNull($current["method"], "The method getHooks() does not return the expected value");
74
		}
75
76
		$hooks6 = HookUtility::getHooks($classpath, $namespace, "/HookUtilityTest/", PHPUnit_Framework_TestCase::class, "testGetHooks");
77
		$this->assertCount(1, $hooks6);
78
		foreach ($hooks6 as $current) {
79
80
			$this->assertEquals($classpath, $current["classpath"]);
81
			$this->assertEquals($namespace, $current["namespace"]);
82
			$this->assertEquals("HookUtilityTest.php", $current["filename"]);
83
			$this->assertContainsOnlyInstancesOf(ReflectionClass::class, [$current["class"]]);
84
			$this->assertContainsOnlyInstancesOf(ReflectionMethod::class, [$current["method"]]);
85
		}
86
87
		try {
88
			HookUtility::getHooks($classpath, $namespace, "/HookUtilityTest/", PHPUnit_Framework_TestCase::class, "testGetHook");
89
		} catch (Exception $ex) {
90
			$this->assertInstanceOf(HookMethodNotFoundException::class, $ex);
91
			$this->assertEquals("The hook method \"testGetHook\" is not found", $ex->getMessage());
92
		}
93
	}
94
95
}
96