Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EventTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Event;
9 
12 
18 class EventTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $event;
24 
28  protected $observers;
29 
33  protected $observer;
34 
35  protected function setUp()
36  {
37  $data = [
38  'name' => 'ObserverName',
39  'block' => 'testBlockName',
40  ];
41  $this->event = new Event($data);
42  $this->observers = new Collection();
43  $this->observer = new Observer($data);
44  $this->observers->addObserver($this->observer);
45  }
46 
47  protected function tearDown()
48  {
49  unset($this->event);
50  }
51 
52  public function testGetObservers()
53  {
54  $this->event->addObserver($this->observer);
55  $expected = $this->observers;
56  $result = $this->event->getObservers();
57  $this->assertEquals($expected, $result);
58  }
59 
60  public function testAddObservers()
61  {
62  $data = ['name' => 'Add New Observer'];
63  $observer = new Observer($data);
64  $this->event->addObserver($observer);
65  $actual = $this->event->getObservers()->getObserverByName($data['name']);
66  $this->assertSame($observer, $actual);
67  }
68 
69  public function testRemoveObserverByName()
70  {
71  $data = [
72  'name' => 'ObserverName',
73  ];
74  $this->event->addObserver($this->observer);
75  $expected = \Magento\Framework\Event\Observer\Collection::class;
76  $actual = $this->event->getObservers()->removeObserverByName($data['name']);
77  $this->assertInstanceOf($expected, $actual);
78  }
79 
80  public function testDispatch()
81  {
82  $this->assertInstanceOf(\Magento\Framework\Event::class, $this->event->dispatch());
83  }
84 
85  public function testGetName()
86  {
87  $data = 'ObserverName';
88  $this->assertEquals($data, $this->event->getName());
89  $this->event = new Event();
90  $this->assertNull($this->event->getName());
91  }
92 
93  public function testGetBlock()
94  {
95  $block = 'testBlockName';
96  $this->assertEquals($block, $this->event->getBlock());
97  }
98 }
$block
Definition: block.php:8