PageTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 36
c 2
b 1
f 0
dl 0
loc 83
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testIsPublishedIfPageIsNotDraft() 0 7 1
A setUp() 0 14 1
A testGetCategoryNameIsEmptyStringIfNoCategoryIsSet() 0 7 1
A testIsPublishedIfPageIsDraft() 0 7 1
A testCreateGrid() 0 8 1
A testGetCategoryReturnsCategoryNameIfSet() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Grid\Factory;
6
7
use AbterPhp\Admin\Grid\Factory\GridFactory;
8
use AbterPhp\Admin\Grid\Factory\PaginationFactory;
9
use AbterPhp\Framework\Grid\IGrid;
10
use AbterPhp\Website\Domain\Entities\Page as Entity;
11
use AbterPhp\Website\Domain\Entities\PageCategory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AbterPhp\Website\Grid\Factory\PageCategory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use AbterPhp\Website\Grid\Factory\Table\Page as TableFactory;
13
use AbterPhp\Website\Grid\Filters\Page as Filters;
14
use Opulence\Routing\Urls\UrlGenerator;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
18
class PageTest extends TestCase
19
{
20
    /** @var Page - System Under Test */
21
    protected $sut;
22
23
    /** @var MockObject|UrlGenerator */
24
    protected $urlGeneratorMock;
25
26
    /** @var MockObject|PaginationFactory */
27
    protected $paginationFactoryMock;
28
29
    /** @var MockObject|TableFactory */
30
    protected $tableFactoryMock;
31
32
    /** @var MockObject|GridFactory */
33
    protected $gridFactoryMock;
34
35
    /** @var MockObject|Filters */
36
    protected $filtersMock;
37
38
    public function setUp(): void
39
    {
40
        $this->urlGeneratorMock      = $this->createMock(UrlGenerator::class);
41
        $this->paginationFactoryMock = $this->createMock(PaginationFactory::class);
42
        $this->tableFactoryMock      = $this->createMock(TableFactory::class);
43
        $this->gridFactoryMock       = $this->createMock(GridFactory::class);
44
        $this->filtersMock           = $this->createMock(Filters::class);
45
46
        $this->sut = new Page(
47
            $this->urlGeneratorMock,
48
            $this->paginationFactoryMock,
49
            $this->tableFactoryMock,
50
            $this->gridFactoryMock,
51
            $this->filtersMock
52
        );
53
    }
54
55
    public function testCreateGrid()
56
    {
57
        $params  = [];
58
        $baseUrl = '';
59
60
        $actualResult = $this->sut->createGrid($params, $baseUrl);
61
62
        $this->assertInstanceOf(IGrid::class, $actualResult);
63
    }
64
65
    public function testGetCategoryNameIsEmptyStringIfNoCategoryIsSet()
66
    {
67
        $entity = new Entity('', '', '', '', '', '', false);
68
69
        $actualResult = $this->sut->getCategoryName($entity);
70
71
        $this->assertSame('<i class="material-icons pmd-sm">remove</i>', $actualResult);
72
    }
73
74
    public function testGetCategoryReturnsCategoryNameIfSet()
75
    {
76
        $categoryName = 'foo';
77
        $category     = new PageCategory('', $categoryName, '');
78
        $entity       = new Entity('', '', '', '', '', '', false, $category);
79
80
        $actualResult = $this->sut->getCategoryName($entity);
81
82
        $this->assertSame($categoryName, $actualResult);
83
    }
84
85
    public function testIsPublishedIfPageIsDraft()
86
    {
87
        $entity = new Entity('', '', '', '', '', '', true);
88
89
        $actualResult = $this->sut->isPublished($entity);
90
91
        $this->assertStringContainsString('is-danger', $actualResult);
92
    }
93
94
    public function testIsPublishedIfPageIsNotDraft()
95
    {
96
        $entity = new Entity('', '', '', '', '', '', false);
97
98
        $actualResult = $this->sut->isPublished($entity);
99
100
        $this->assertStringContainsString('is-success', $actualResult);
101
    }
102
}
103