Completed
Pull Request — master (#8)
by Korotkov
02:47
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    : Korotkov Danila <[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
/**
19
 * Class ObserverTest
20
 * @package Behavioral\Observer\Tests
21
 */
22
class ObserverTest extends PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var SubjectInterface
26
     */
27
    private $team;
28
29
    protected function setUp(): void
30
    {
31
        $this->team = new FootballSubject('Manchester United');
32
    }
33
34
    public function testFootballSubject(): void
35
    {
36
        $this->assertInstanceOf(FootballSubject::class, $this->getTeam());
37
        $this->assertEquals('Manchester United', $this->getTeam()->getName());
0 ignored issues
show
Bug introduced by
The method getName() 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

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