Passed
Branch 4 (f3d551)
by Simon
03:46
created

PageUpdateServiceTest::testUpdateNoVisits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\GoogleAPI\Tests;
4
5
use Firesphere\GoogleAPI\Services\PageUpdateService;
6
use Firesphere\GoogleAPI\Tests\Mock\AnalyticsResponseHomePageMock;
7
use Firesphere\GoogleAPI\Tests\Mock\AnalyticsResponseNoPageMock;
8
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Security\DefaultAdminService;
12
use SilverStripe\Security\IdentityStore;
13
use SilverStripe\Security\Security;
14
15
class PageUpdateServiceTest extends SapphireTest
16
{
17
18
    /**
19
     * @var PageUpdateService
20
     */
21
    protected $service;
22
23
    public function setUp()
24
    {
25
        $this->service = new PageUpdateService();
26
        $adminService = DefaultAdminService::create();
27
        $admin = $adminService->findOrCreateDefaultAdmin();
28
        Security::setCurrentUser($admin);
29
        return parent::setUp();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::setUp() targeting SilverStripe\Dev\SapphireTest::setUp() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
30
    }
31
    
32
    /**
33
     * Validate an array with two empty values finds the homepage
34
     */
35
    public function testFindHomePage()
36
    {
37
        $testArray = ['', ''];
38
        $page = $this->service->findPage($testArray);
39
        $this->assertInstanceOf(Page::class, $page);
40
        $this->assertEquals('home', $page->URLSegment);
41
    }
42
43
    /**
44
     * Validate we're traversing correctly down in to the child pages
45
     */
46 View Code Duplication
    public function testFindChildPage()
47
    {
48
        /** @var Page $homePage */
49
        $homePage = Page::get()->filter(['URLSegment' => 'home'])->first();
50
        /** @var Page $homePage */
51
        $child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]);
52
        $child->write();
53
        $child->publishRecursive();
54
        $testArray = ['', '', 'test-page'];
55
        $page = $this->service->findPage($testArray);
56
        $this->assertInstanceOf(Page::class, $page);
57
        $this->assertEquals('/home/test-page/', $page->Link());
58
    }
59
60
    /**
61
     * Make sure the get params are ignored
62
     */
63 View Code Duplication
    public function testFindChildPageWithQuery()
64
    {
65
        /** @var Page $homePage */
66
        $homePage = Page::get()->filter(['URLSegment' => 'home'])->first();
67
        /** @var Page $homePage */
68
        $child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]);
69
        $child->write();
70
        $child->publishRecursive();
71
        $testArray = ['', '', 'test-page', '?stage=Stage'];
72
        $page = $this->service->findPage($testArray);
73
        $this->assertInstanceOf(Page::class, $page);
74
        $this->assertEquals('/home/test-page/', $page->Link());
75
    }
76
77
    /**
78
     * Make sure an empty last value is skipped
79
     */
80 View Code Duplication
    public function testFindPageWithEmptyLast()
81
    {
82
        /** @var Page $homePage */
83
        $homePage = Page::get()->filter(['URLSegment' => 'home'])->first();
84
        /** @var Page $homePage */
85
        $child = Page::create(['Title' => 'Test Page', 'ParentID' => $homePage->ID]);
86
        $child->write();
87
        $child->publishRecursive();
88
        $testArray = ['', '', 'test-page', ''];
89
        $page = $this->service->findPage($testArray);
90
        $this->assertInstanceOf(Page::class, $page);
91
        $this->assertEquals('/home/test-page/', $page->Link());
92
    }
93
94
    /**
95
     * Validate updating the pages works
96
     */
97
    public function testUpdateVisits()
98
    {
99
        $result = $this->service->updateVisits([new AnalyticsResponseHomePageMock()]);
100
        $this->assertEquals(1, $result);
101
        $page = Page::get()->filter(['URLSegment' => 'home'])->first();
102
        $this->assertEquals(45477, $page->VisitCount);
103
    }
104
105
    /**
106
     * Validate that if no pages is found, nothing is done.
107
     */
108
    public function testUpdateNoVisits()
109
    {
110
        $result = $this->service->updateVisits([new AnalyticsResponseNoPageMock()]);
111
        $this->assertEquals(0, $result);
112
        $this->assertFalse($this->service->batched);
113
    }
114
}
115