ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
PageMetricsTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\ContentPage;
22 
34 use ilDBInterface;
35 
36 class PageMetricsTest extends TestCase
37 {
39  {
40  $this->expectException(CouldNotFindPageMetrics::class);
41 
42  $database = $this->getMockBuilder(ilDBInterface::class)->getMock();
43  $database->method('fetchAssoc')->willReturn(null);
44 
45  $repo = new PageMetricsRepositoryImp($database);
46  $repo->findBy(1, 1, 'de');
47  }
48 
50  {
51  $readingTimeInMinutes = 4711;
52  $language = 'en';
53  $pageId = 1337;
54  $contentPageId = 1337;
55 
56  $database = $this->getMockBuilder(ilDBInterface::class)->getMock();
57  $database->method('fetchAssoc')->willReturn(
58  [
59  'content_page_id' => $contentPageId,
60  'page_id' => $pageId,
61  'lang' => $language,
62  'reading_time' => $readingTimeInMinutes
63  ]
64  );
65 
66  $repo = new PageMetricsRepositoryImp($database);
67  $pageMetrics = $repo->findBy($contentPageId, $pageId, $language);
68 
69  $this->assertSame($readingTimeInMinutes, $pageMetrics->readingTime()->minutes());
70  $this->assertSame($language, $pageMetrics->language());
71  $this->assertSame($contentPageId, $pageMetrics->contentPageId());
72  $this->assertSame($pageId, $pageMetrics->pageId());
73  }
74 
76  {
77  $database = $this->getMockBuilder(ilDBInterface::class)->getMock();
78 
79  $pageMetrics = $this->getMockBuilder(PageMetrics::class)->disableOriginalConstructor()->getMock();
80  $pageMetrics->expects($this->once())->method('contentPageId');
81  $pageMetrics->expects($this->once())->method('pageId');
82  $pageMetrics->expects($this->once())->method('language');
83  $pageMetrics->expects($this->once())->method('readingTime');
84 
85  $repo = new PageMetricsRepositoryImp($database);
86  $repo->store($pageMetrics);
87  }
88 
90  {
91  $database = $this->getMockBuilder(ilDBInterface::class)->getMock();
92 
93  $pageMetrics = $this->getMockBuilder(PageMetrics::class)->disableOriginalConstructor()->getMock();
94  $pageMetrics->expects($this->once())->method('contentPageId');
95  $pageMetrics->expects($this->once())->method('pageId');
96  $pageMetrics->expects($this->once())->method('language');
97 
98  $repo = new PageMetricsRepositoryImp($database);
99  $repo->delete($pageMetrics);
100  }
101 
102  public function testCommandsWorkAsExpected(): void
103  {
104  $getPageMetrics = new GetPageMetricsCommand(1337, 'de');
105  $this->assertSame('de', $getPageMetrics->getLanguage());
106  $this->assertSame(1337, $getPageMetrics->getContentPageId());
107 
108  $storePageMetrics = new StorePageMetricsCommand(1337, 'de');
109  $this->assertSame('de', $storePageMetrics->getLanguage());
110  $this->assertSame(1337, $storePageMetrics->getContentPageId());
111  }
112 
113  public function testEventsWorkAsExpected(): void
114  {
115  $page = $this->getMockBuilder(ilContentPagePage::class)->disableOriginalConstructor()->getMock();
116 
117  $pageUpdated = new PageUpdatedEvent($page);
118  $this->assertSame($page, $pageUpdated->page());
119  }
120 
122  {
123  $readingTimeInMinutes = 4711;
124  $language = 'fr';
125  $pageId = 1337;
126  $contentPageId = 1337;
127 
128  $readingTime = new PageReadingTime($readingTimeInMinutes);
129 
130  $pageMetrics = $this->getMockBuilder(PageMetrics::class)->disableOriginalConstructor()->getMock();
131  $pageMetrics->method('contentPageId')->willReturn($contentPageId);
132  $pageMetrics->method('pageId')->willReturn($pageId);
133  $pageMetrics->method('language')->willReturn($language);
134  $pageMetrics->method('readingTime')->willReturn($readingTime);
135 
136  $repo = $this->getMockBuilder(PageMetricsRepository::class)->getMock();
137  $repo->expects($this->once())->method('findBy')->with($contentPageId, $pageId, $language)->willReturn($pageMetrics);
138 
139  $service = new PageMetricsService(
140  $repo,
141  new \ILIAS\Refinery\Factory(
142  new \ILIAS\Data\Factory(),
143  $this->getMockBuilder(\ilLanguage::class)->disableOriginalConstructor()->getMock(),
144  )
145  );
146 
147  $receivedPageMetrics = $service->get(new GetPageMetricsCommand($contentPageId, $language));
148  $this->assertSame($pageMetrics, $receivedPageMetrics);
149  }
150 }
Interface Observer Contains several chained tasks and infos about them.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Builds data types.
Definition: Factory.php:35
$service
Definition: ltiservices.php:40
testRepositoryThrowsExceptionWhenPageMetricsShouldBeRetrievedButNoPageMetricsExist()