Completed
Push — master ( e6f585...763af6 )
by Korotkov
07:13 queued 05:12
created

ObserverTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getProperty() 0 7 1
B testTeamAction() 0 28 1
A testFootballSubject() 0 4 1
A setUp() 0 3 1
A testDetachObserver() 0 10 1
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 PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;
16
17
/**
18
 * Class ObserverTest
19
 * @package Behavioral\Observer\Tests
20
 */
21
class ObserverTest extends PHPUnit_Framework_TestCase
22
{
23
24
    /**
25
     * @var FootballSubject
26
     */
27
    protected $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->team);
37
        $this->assertEquals('Manchester United', $this->team->getName());
38
    }
39
40
    public function testTeamAction(): void
41
    {
42
        $this->team->attachObserver(new FootballObserver('John'));
43
        $this->team->attachObserver(new FootballObserver('Bill'));
44
45
        ob_start();
46
        $this->team->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->team->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->team->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->team->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->team->attachObserver($observer);
76
        $this->assertEquals('Петя', $this->getProperty('observers')->getValue($this->team)['Петя']->getName());
77
78
        $this->team->detachObserver('Петя');
79
        $this->assertCount(0, $this->getProperty('observers')->getValue($this->team));
80
    }
81
82
    protected function getProperty(string $name): \ReflectionProperty
83
    {
84
        $class    = new \ReflectionClass($this->team);
85
        $property = $class->getProperty($name);
86
        $property->setAccessible(true);
87
88
        return $property;
89
    }
90
}
91