Completed
Push — master ( 4ac7d2...adee57 )
by Korotkov
02:34 queued 40s
created

ObserverTest::setUp()   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
 * @copyright Copyright (c) 2017, Korotkov Danila
8
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
9
 */
10
11
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Behavioral\Observer\FootballSubject;
13
use Behavioral\Observer\FootballObserver;
14
use Behavioral\Observer\FootballEvent;
15
16
class ObserverTest extends PHPUnit_Framework_TestCase
17
{
18
19
    /**
20
     * @var FootballSubject
21
     */
22
    protected $team;
23
24
    protected function setUp(): void
25
    {
26
        $this->team = new FootballSubject('Динамо');
27
    }
28
29
    public function testFootballSubject(): void
30
    {
31
        $this->assertInstanceOf(FootballSubject::class, $this->getTeam());
32
        $this->assertEquals('Динамо', $this->getTeam()->getSubjectName());
33
    }
34
35
    public function testTeamAction(): void
36
    {
37
        $this->getTeam()->attachObserver(new FootballObserver('Петя'));
38
        $this->getTeam()->attachObserver(new FootballObserver('Вася'));
39
40
        ob_start();
41
        $this->getTeam()->teamAction(new FootballEvent(FootballEvent::GOAL, $this->getTeam()));
42
        $goal = ob_get_clean();
43
44
        $this->assertEquals($goal, "Петя празнует ГОЛ!!! Динамо\nВася празнует ГОЛ!!! Динамо\n");
45
46
        ob_start();
47
        $this->getTeam()->teamAction(new FootballEvent(FootballEvent::MISS, $this->getTeam()));
48
        $miss = ob_get_clean();
49
50
        $this->assertEquals($miss, "Петя поддерживает Динамо после пропущенного мяча\nВася поддерживает Динамо после пропущенного мяча\n");
51
52
        ob_start();
53
        $this->getTeam()->teamAction(new FootballEvent(FootballEvent::FIRE, $this->getTeam()));
54
        $fire = ob_get_clean();
55
56
        $this->assertEquals($fire, "Петя поджигает файер\nВася поджигает файер\n");
57
58
        ob_start();
59
        $this->getTeam()->teamAction(new FootballEvent('random', $this->getTeam()));
60
        $random = ob_get_clean();
61
62
        $this->assertEquals($random, "Петя отреагировал на событие Динамо\nВася отреагировал на событие Динамо\n");
63
    }
64
65
    public function testDetachObserver()
66
    {
67
        $observer = new FootballObserver('Петя');
68
69
        $this->assertEquals($observer->getObserverName(), 'Петя');
70
71
        $this->getTeam()->attachObserver($observer);
72
73
        $this->assertEquals('Петя', $this->getProperty('observers')->getValue($this->getTeam())['Петя']->getObserverName());
74
75
        $this->getTeam()->detachObserver('Петя');
76
77
        $this->assertCount(0, $this->getProperty('observers')->getValue($this->getTeam()));
78
    }
79
80
    /**
81
     * @param string $name
82
     *
83
     * @return ReflectionProperty
84
     */
85
    protected function getProperty(string $name): ReflectionProperty
86
    {
87
        $class = new ReflectionClass($this->getTeam());
88
        $property = $class->getProperty($name);
89
        $property->setAccessible(true);
90
91
        return $property;
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97
    public function getTeam(): FootballSubject
98
    {
99
        return $this->team;
100
    }
101
}