Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":263:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:3:{s:59:"Behavioral\Observer\Tests\ObserverTest::testFootballSubject";d:0.001;s:54:"Behavioral\Observer\Tests\ObserverTest::testTeamAction";d:0.001;s:58:"Behavioral\Observer\Tests\ObserverTest::testDetachObserver";d:0.001;}}}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: php

# Define the php versions against we want to test our code
php:
- 7.2
- 7.4

before_script:
- wget http://getcomposer.org/composer.phar
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
[![Code Climate](https://codeclimate.com/github/Jagepard/PhpDesignPatterns-Observer/badges/gpa.svg)](https://codeclimate.com/github/Jagepard/PhpDesignPatterns-Observer)
[![License: MIT](https://img.shields.io/badge/license-MIT-498e7f.svg)](https://mit-license.org/)
-----
# Observer | [API](https://github.com/Jagepard/PhpDesignPatterns-Observer/blob/master/docs.md "Documentation API")
# Observer
```php run``` execute in terminal

![Observer](https://github.com/Jagepard/PhpDesignPatterns-Observer/blob/master/UML.png)
## Result:
```
John has get information about: Manchester United Goal!!!
Bill has get information about: Manchester United Goal!!!
John has get information about: Manchester United missing a ball(((
Bill has get information about: Manchester United missing a ball(((
John has get information about: Manchester United getting a yellow card
Bill has get information about: Manchester United getting a yellow card
John has get information about: Manchester United Goal!!!
Steve has get information about: Manchester United Goal!!!
John has get information about: Manchester United Offside
Steve has get information about: Manchester United Offside
```
Binary file removed UML.png
Binary file not shown.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"skype": "jagepard.ru"
},
"require": {
"php": ">=7.2"
"php": ">=7.4"
},
"require-dev" : {
"phpunit/phpunit": "^7"
Expand Down
70 changes: 0 additions & 70 deletions docs.md

This file was deleted.

5 changes: 1 addition & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="tests/build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-html" target="tests/build/coverage"/>
<log type="coverage-text" target="tests/build/coverage.txt"/>
<log type="coverage-clover" target="tests/build/logs/clover.xml"/>
</logging>
<filter>
<blacklist>
<directory suffix=".php">./vendor</directory>
</blacklist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
Expand Down
3 changes: 0 additions & 3 deletions src/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@

interface EventInterface
{
/**
* @return string
*/
public function getEventName(): string;
}
16 changes: 3 additions & 13 deletions src/FootballEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,17 @@

class FootballEvent implements EventInterface
{
const GOAL = 'Goal!!!';
const MISS = 'missing a ball(((';
const GOAL = 'Goal!!!';
const MISS = 'missing a ball(((';
const VIOLATION = 'getting a yellow card';

/**
* @var string
*/
private $eventName;
private string $eventName;

/**
* FootballEvent constructor.
* @param string $eventName
*/
public function __construct(string $eventName)
{
$this->eventName = $eventName;
}

/**
* @return string
*/
public function getEventName(): string
{
return $this->eventName;
Expand Down
12 changes: 1 addition & 11 deletions src/FootballObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,13 @@

class FootballObserver implements ObserverInterface
{
/**
* @var string
*/
protected $observerName;
protected string $observerName;

/**
* FootballObserver constructor.
* @param string $observerName
*/
public function __construct(string $observerName)
{
$this->observerName = $observerName;
}

/**
* @return string
*/
public function getObserverName(): string
{
return $this->observerName;
Expand Down
28 changes: 3 additions & 25 deletions src/FootballSubject.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,15 @@

class FootballSubject implements SubjectInterface
{
/**
* @var string
*/
private $subjectName;
/**
* @var array
*/
private $observers = [];
private string $subjectName;

private array $observers = [];

/**
* FootballSubject constructor.
* @param string $subjectName
*/
public function __construct(string $subjectName)
{
$this->subjectName = $subjectName;
}

/**
* @param ObserverInterface $observer
* @throws \Exception
*/
public function attachObserver(ObserverInterface $observer): void
{
if (array_key_exists($observer->getObserverName(), $this->observers)) {
Expand All @@ -42,9 +29,6 @@ public function attachObserver(ObserverInterface $observer): void
$this->observers[$observer->getObserverName()] = $observer;
}

/**
* @param string $subjectName
*/
public function detachObserver(string $subjectName): void
{
if (!array_key_exists($subjectName, $this->observers)) {
Expand All @@ -54,9 +38,6 @@ public function detachObserver(string $subjectName): void
unset($this->observers[$subjectName]);
}

/**
* @param EventInterface $event
*/
public function notifyObservers(EventInterface $event): void
{
foreach ($this->observers as $observer) {
Expand All @@ -69,9 +50,6 @@ public function notifyObservers(EventInterface $event): void
}
}

/**
* @return string
*/
public function getSubjectName(): string
{
return $this->subjectName;
Expand Down
3 changes: 0 additions & 3 deletions src/ObserverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@

interface ObserverInterface
{
/**
* @return string
*/
public function getObserverName(): string;
}
9 changes: 0 additions & 9 deletions src/SubjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@

interface SubjectInterface
{
/**
* @param ObserverInterface $observer
*/
public function attachObserver(ObserverInterface $observer): void;

/**
* @param string $observerName
*/
public function detachObserver(string $observerName): void;

/**
* @param EventInterface $event
*/
public function notifyObservers(EventInterface $event): void;
}
30 changes: 12 additions & 18 deletions tests/ObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@

namespace Behavioral\Observer\Tests;

use Behavioral\Observer\FootballEvent;
use Behavioral\Observer\FootballSubject;
use Behavioral\Observer\FootballObserver;
use Behavioral\Observer\SubjectInterface;
use Behavioral\Observer\{FootballEvent, FootballSubject, FootballObserver, SubjectInterface};
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;

class ObserverTest extends PHPUnit_Framework_TestCase
{
/**
* @var SubjectInterface
*/
private $team;
private SubjectInterface $team;

protected function setUp(): void
{
Expand All @@ -43,42 +37,42 @@ public function testTeamAction(): void
$goal = ob_get_clean();

$this->assertEquals(
$goal,
"John has get information about: Manchester United Goal!!! \nBill has get information about: Manchester United Goal!!! \n"
"John has get information about: Manchester United Goal!!! \nBill has get information about: Manchester United Goal!!! \n",
$goal
);

ob_start();
$this->team->notifyObservers(new FootballEvent(FootballEvent::MISS));
$miss = ob_get_clean();

$this->assertEquals(
$miss,
"John has get information about: Manchester United missing a ball((( \nBill has get information about: Manchester United missing a ball((( \n"
"John has get information about: Manchester United missing a ball((( \nBill has get information about: Manchester United missing a ball((( \n",
$miss
);

ob_start();
$this->team->notifyObservers(new FootballEvent(FootballEvent::VIOLATION));
$violation = ob_get_clean();

$this->assertEquals(
$violation,
"John has get information about: Manchester United getting a yellow card \nBill has get information about: Manchester United getting a yellow card \n"
"John has get information about: Manchester United getting a yellow card \nBill has get information about: Manchester United getting a yellow card \n",
$violation
);

ob_start();
$this->team->notifyObservers(new FootballEvent('random'));
$random = ob_get_clean();

$this->assertEquals(
$random,
"John has get information about: Manchester United random \nBill has get information about: Manchester United random \n"
"John has get information about: Manchester United random \nBill has get information about: Manchester United random \n",
$random
);
}

public function testDetachObserver()
{
$observer = new FootballObserver('Петя');
$this->assertEquals($observer->getObserverName(), 'Петя');
$this->assertEquals('Петя', $observer->getObserverName());

$this->team->attachObserver($observer);
$this->assertEquals('Петя', $this->getProperty('observers')->getValue($this->team)['Петя']->getObserverName());
Expand All @@ -89,7 +83,7 @@ public function testDetachObserver()

protected function getProperty(string $name): \ReflectionProperty
{
$class = new \ReflectionClass($this->team);
$class = new \ReflectionClass($this->team);
$property = $class->getProperty($name);
$property->setAccessible(true);

Expand Down