Completed
Push — master ( c4148d...4cccaa )
by Korotkov
03:01 queued 11s
created

ObserverTest::getTeam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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;
13
use Behavioral\Observer\FootballSubject;
14
use Behavioral\Observer\FootballObserver;
15
use Behavioral\Observer\SubjectInterface;
16
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;
17
18
class ObserverTest extends PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var SubjectInterface
22
     */
23
    private $team;
24
25
    protected function setUp(): void
26
    {
27
        $this->team = new FootballSubject('Manchester United');
28
    }
29
30
    public function testFootballSubject(): void
31
    {
32
        $this->assertInstanceOf(FootballSubject::class, $this->team);
33
        $this->assertEquals('Manchester United', $this->team->getSubjectName());
0 ignored issues
show
Bug introduced by
The method getSubjectName() does not exist on Behavioral\Observer\SubjectInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Behavioral\Observer\SubjectInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $this->assertEquals('Manchester United', $this->team->/** @scrutinizer ignore-call */ getSubjectName());
Loading history...
34
    }
35
36
    public function testTeamAction(): void
37
    {
38
        $this->team->attachObserver(new FootballObserver('John'));
39
        $this->team->attachObserver(new FootballObserver('Bill'));
40
41
        ob_start();
42
        $this->team->notifyObservers(new FootballEvent(FootballEvent::GOAL));
43
        $goal = ob_get_clean();
44
45
        $this->assertEquals(
46
            $goal,
47
            "John has get information about: Manchester United Goal!!! \nBill has get information about: Manchester United Goal!!! \n"
48
        );
49
50
        ob_start();
51
        $this->team->notifyObservers(new FootballEvent(FootballEvent::MISS));
52
        $miss = ob_get_clean();
53
54
        $this->assertEquals(
55
            $miss,
56
            "John has get information about: Manchester United missing a ball((( \nBill has get information about: Manchester United missing a ball((( \n"
57
        );
58
59
        ob_start();
60
        $this->team->notifyObservers(new FootballEvent(FootballEvent::VIOLATION));
61
        $violation = ob_get_clean();
62
63
        $this->assertEquals(
64
            $violation,
65
            "John has get information about: Manchester United getting a yellow card \nBill has get information about: Manchester United getting a yellow card \n"
66
        );
67
68
        ob_start();
69
        $this->team->notifyObservers(new FootballEvent('random'));
70
        $random = ob_get_clean();
71
72
        $this->assertEquals(
73
            $random,
74
            "John has get information about: Manchester United random \nBill has get information about: Manchester United random \n"
75
        );
76
    }
77
78
    public function testDetachObserver()
79
    {
80
        $observer = new FootballObserver('Петя');
81
        $this->assertEquals($observer->getObserverName(), 'Петя');
82
83
        $this->team->attachObserver($observer);
84
        $this->assertEquals('Петя', $this->getProperty('observers')->getValue($this->team)['Петя']->getObserverName());
85
86
        $this->team->detachObserver('Петя');
87
        $this->assertCount(0, $this->getProperty('observers')->getValue($this->team));
88
    }
89
90
    protected function getProperty(string $name): \ReflectionProperty
91
    {
92
        $class = new \ReflectionClass($this->team);
93
        $property = $class->getProperty($name);
94
        $property->setAccessible(true);
95
96
        return $property;
97
    }
98
}
99