ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilCtrlEventTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
32 {
34 
35  protected function setUp(): void
36  {
37  $this->subject = new ilCtrlSubject();
38  }
39 
40  public function testObserverAttachment(): void
41  {
42  // Default Attachment
43  $mocked_observer = $this->createMock(ilCtrlObserver::class);
44  $mocked_subject = $this->createMock(ilCtrlSubject::class);
45  $mocked_ilctrl = $this->getMockedCtrl($mocked_subject);
46  $mocked_subject->expects($this->once())
47  ->method('attach')
48  ->with($mocked_observer, ilCtrlEvent::ALL);
49 
50  $mocked_ilctrl->attachObserver($mocked_observer);
51 
52  $mocked_subject->expects($this->once())
53  ->method('detach')
54  ->with($mocked_observer, ilCtrlEvent::ALL);
55  $mocked_ilctrl->detachObserver($mocked_observer);
56 
57  // Command Class Attachment
58  $mocked_observer = $this->createMock(ilCtrlObserver::class);
59  $mocked_subject = $this->createMock(ilCtrlSubject::class);
60  $mocked_ilctrl = $this->getMockedCtrl($mocked_subject);
61  $mocked_subject->expects($this->once())
62  ->method('attach')
63  ->with($mocked_observer, ilCtrlEvent::COMMAND_CLASS_FORWARD);
64 
65  $mocked_ilctrl->attachObserver($mocked_observer, ilCtrlEvent::COMMAND_CLASS_FORWARD);
66 
67  $mocked_subject->expects($this->once())
68  ->method('detach')
69  ->with($mocked_observer, ilCtrlEvent::COMMAND_CLASS_FORWARD);
70  $mocked_ilctrl->detachObserver($mocked_observer, ilCtrlEvent::COMMAND_CLASS_FORWARD);
71 
72  // Command Determination Attachment
73  $mocked_observer = $this->createMock(ilCtrlObserver::class);
74  $mocked_subject = $this->createMock(ilCtrlSubject::class);
75  $mocked_ilctrl = $this->getMockedCtrl($mocked_subject);
76  $mocked_subject->expects($this->once())
77  ->method('attach')
78  ->with($mocked_observer, ilCtrlEvent::COMMAND_DETERMINATION);
79 
80  $mocked_ilctrl->attachObserver($mocked_observer, ilCtrlEvent::COMMAND_DETERMINATION);
81 
82  $mocked_subject->expects($this->once())
83  ->method('detach')
84  ->with($mocked_observer, ilCtrlEvent::COMMAND_DETERMINATION);
85  $mocked_ilctrl->detachObserver($mocked_observer, ilCtrlEvent::COMMAND_DETERMINATION);
86  }
87 
88  public function testNotifyEvents(): void
89  {
90  require_once __DIR__ . '/Data/GUI/class.ilCtrlBaseClass1TestGUI.php';
91  $base_class = new ilCtrlBaseClass1TestGUI();
92 
93  $mocked_ilctrl = $this->getMockedCtrl();
94 
95  $mocked_observer = $this->createMock(ilCtrlObserver::class);
96  $mocked_ilctrl->attachObserver($mocked_observer, ilCtrlEvent::ALL);
97  $consecutive = [
98  [ilCtrlEvent::COMMAND_CLASS_FORWARD, ilCtrlBaseClass1TestGUI::class],
99  [ilCtrlEvent::COMMAND_DETERMINATION, 'fallback']
100  ];
101  $mocked_observer
102  ->expects($this->exactly(2))
103  ->method('update')
104  ->willReturnCallback(
105  function ($event, $cmd) use (&$consecutive) {
106  $expected = array_shift($consecutive);
107  $this->assertEquals($expected, [$event, $cmd]);
108  }
109  );
110 
111  $mocked_ilctrl->forwardCommand($base_class);
112  $command = $mocked_ilctrl->getCmd('fallback');
113  }
114 
119  private function getMockedCtrl(?ilCtrlSubject $subject = null): ilCtrlInterface
120  {
121  return new ilCtrl(
122  $this->createMock(ilCtrlStructureInterface::class),
123  $this->createMock(ilCtrlTokenRepositoryInterface::class),
124  $this->createMock(ilCtrlPathFactoryInterface::class),
125  $this->createMock(ilCtrlContextInterface::class),
126  $this->createMock(ResponseSenderStrategy::class),
127  $this->createMock(ServerRequestInterface::class),
128  $this->createMock(RequestWrapper::class),
129  $this->createMock(RequestWrapper::class),
130  $this->createMock(Refinery::class),
131  $this->createMock(ilComponentFactory::class),
132  $subject ?? $this->subject,
133  $this->createMock(ilCtrlQueryParserInterface::class),
134  );
135  }
136 }
getMockedCtrl(?ilCtrlSubject $subject=null)
Helper function that returns an ilCtrl instance with mocked dependencies.
event string being used if
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ilCtrlSubject $subject
Class ilCtrlBaseClass1TestGUI.