|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author : Jagepard <[email protected]> |
|
7
|
|
|
* @license https://mit-license.org/ MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Behavioral\Observer\Tests; |
|
11
|
|
|
|
|
12
|
|
|
use Behavioral\Observer\{FootballEvent, FootballSubject, FootballObserver, SubjectInterface}; |
|
13
|
|
|
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class ObserverTest extends PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
private SubjectInterface $team; |
|
18
|
|
|
|
|
19
|
|
|
protected function setUp(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->team = new FootballSubject('Manchester United'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testFootballSubject(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->assertInstanceOf(FootballSubject::class, $this->team); |
|
27
|
|
|
$this->assertEquals('Manchester United', $this->team->getName()); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testTeamAction(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->team->attachObserver(new FootballObserver('John')); |
|
33
|
|
|
$this->team->attachObserver(new FootballObserver('Bill')); |
|
34
|
|
|
|
|
35
|
|
|
ob_start(); |
|
36
|
|
|
$this->team->notifyObservers(new FootballEvent(FootballEvent::GOAL)); |
|
37
|
|
|
$goal = ob_get_clean(); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertEquals( |
|
40
|
|
|
"John has get information about: Manchester United Goal!!! \nBill has get information about: Manchester United Goal!!! \n\n", |
|
41
|
|
|
$goal |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
ob_start(); |
|
45
|
|
|
$this->team->notifyObservers(new FootballEvent(FootballEvent::MISS)); |
|
46
|
|
|
$miss = ob_get_clean(); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals( |
|
49
|
|
|
"John has get information about: Manchester United missing a ball((( \nBill has get information about: Manchester United missing a ball((( \n\n", |
|
50
|
|
|
$miss |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
ob_start(); |
|
54
|
|
|
$this->team->notifyObservers(new FootballEvent(FootballEvent::VIOLATION)); |
|
55
|
|
|
$violation = ob_get_clean(); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertEquals( |
|
58
|
|
|
"John has get information about: Manchester United getting a yellow card \nBill has get information about: Manchester United getting a yellow card \n\n", |
|
59
|
|
|
$violation |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
ob_start(); |
|
63
|
|
|
$this->team->notifyObservers(new FootballEvent('random')); |
|
64
|
|
|
$random = ob_get_clean(); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals( |
|
67
|
|
|
"John has get information about: Manchester United random \nBill has get information about: Manchester United random \n\n", |
|
68
|
|
|
$random |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testDetachObserver() |
|
73
|
|
|
{ |
|
74
|
|
|
$observer = new FootballObserver('Петя'); |
|
75
|
|
|
$this->assertEquals('Петя', $observer->getName()); |
|
76
|
|
|
|
|
77
|
|
|
$this->team->attachObserver($observer); |
|
78
|
|
|
$this->assertEquals('Петя', $this->getProperty('observers')->getValue($this->team)['Петя']->getName()); |
|
79
|
|
|
|
|
80
|
|
|
$this->team->detachObserver('Петя'); |
|
81
|
|
|
$this->assertCount(0, $this->getProperty('observers')->getValue($this->team)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function getProperty(string $name): \ReflectionProperty |
|
85
|
|
|
{ |
|
86
|
|
|
$class = new \ReflectionClass($this->team); |
|
87
|
|
|
$property = $class->getProperty($name); |
|
88
|
|
|
$property->setAccessible(true); |
|
89
|
|
|
|
|
90
|
|
|
return $property; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths