ProfileTest::testCreate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 62
rs 9.0472
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Form\Factory;
6
7
use AbterPhp\Admin\Domain\Entities\User as Entity;
8
use AbterPhp\Admin\Domain\Entities\UserGroup;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AbterPhp\Admin\Form\Factory\UserGroup. 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...
9
use AbterPhp\Admin\Domain\Entities\UserLanguage;
10
use AbterPhp\Admin\Orm\UserGroupRepo;
11
use AbterPhp\Admin\Orm\UserLanguageRepo;
12
use AbterPhp\Framework\I18n\ITranslator;
13
use Opulence\Http\Requests\RequestMethods;
14
use Opulence\Orm\IEntity;
15
use Opulence\Sessions\ISession;
16
use Opulence\Sessions\Session;
17
use PHPUnit\Framework\MockObject\MockObject;
18
use PHPUnit\Framework\TestCase;
19
20
class ProfileTest extends TestCase
21
{
22
    /** @var Profile - System Under Test */
23
    protected Profile $sut;
24
25
    /** @var ISession|MockObject */
26
    protected $sessionMock;
27
28
    /** @var ITranslator|MockObject */
29
    protected $translatorMock;
30
31
    /** @var UserGroupRepo|MockObject */
32
    protected $userGroupRepoMock;
33
34
    /** @var UserLanguageRepo|MockObject */
35
    protected $userLanguageRepoMock;
36
37
    public function setUp(): void
38
    {
39
        $this->sessionMock = $this->createMock(Session::class);
40
        $this->sessionMock->expects($this->any())->method('get')->willReturnArgument(0);
41
42
        $this->translatorMock = $this->createMock(ITranslator::class);
43
        $this->translatorMock->expects($this->any())->method('translate')->willReturnArgument(0);
44
45
        $this->userGroupRepoMock = $this->createMock(UserGroupRepo::class);
46
47
        $this->userLanguageRepoMock = $this->createMock(UserLanguageRepo::class);
48
49
        $this->sut = new Profile(
50
            $this->sessionMock,
51
            $this->translatorMock,
52
            $this->userGroupRepoMock,
53
            $this->userLanguageRepoMock
54
        );
55
    }
56
57
    public function testCreate()
58
    {
59
        $action            = 'foo';
60
        $method            = RequestMethods::POST;
61
        $showUrl           = 'bar';
62
        $entityId          = '96368723-292f-4943-9903-83ad552fc118';
63
        $password          = 'baz';
64
        $username          = 'zorro79';
65
        $email             = '[email protected]';
66
        $canLogin          = true;
67
        $isGravatarAllowed = true;
68
        $allUserGroups     = [
69
            new UserGroup('dc2abea5-8021-4228-882a-31b91fe3687a', 'ug-22', 'UG 22'),
70
            new UserGroup('75ccb863-ce02-4b2d-b655-af4c92f2dbe6', 'ug-73', 'UG 73'),
71
            new UserGroup('aff15988-2170-4b10-9aad-4ed2ea19f73e', 'ug-112', 'UG 112'),
72
            new UserGroup('143522ce-5e0e-4abb-8c4b-67d88ba90d9d', 'ug-432', 'UG 432'),
73
        ];
74
        $userGroups        = [
75
            $allUserGroups[0],
76
            $allUserGroups[2],
77
        ];
78
        $allUserLanguages  = [
79
            new UserLanguage('5027689a-39da-4810-b0b6-2ae42e387698', 'ul-52', 'UL 52'),
80
            new UserLanguage('afb423cc-6272-4ff4-8a62-e28cac6cb1d1', 'ul-77', 'UL 77'),
81
            new UserLanguage('38030970-10be-4b6b-9dc6-38d6a74310ca', 'ul-93', 'UL 93'),
82
            new UserLanguage('15c3f8ef-d5ff-4210-a5a2-3732f4073a1b', 'ul-94', 'UL 94'),
83
        ];
84
        $userLanguage      = $allUserLanguages[1];
85
86
        $this->userGroupRepoMock->expects($this->any())->method('getAll')->willReturn($allUserGroups);
87
        $this->userLanguageRepoMock->expects($this->any())->method('getAll')->willReturn($allUserLanguages);
88
89
        $entityStub = new Entity(
90
            $entityId,
91
            $username,
92
            $email,
93
            $password,
94
            $canLogin,
95
            $isGravatarAllowed,
96
            $userLanguage,
97
            $userGroups
98
        );
99
100
        $form = (string)$this->sut->create($action, $method, $showUrl, $entityStub);
101
102
        $this->assertStringContainsString($action, $form);
103
        $this->assertStringNotContainsString($showUrl, $form);
104
        $this->assertStringContainsString('CSRF', $form);
105
        $this->assertStringContainsString('POST', $form);
106
        $this->assertStringContainsString('username', $form);
107
        $this->assertStringContainsString('email', $form);
108
        $this->assertStringContainsString('password', $form);
109
        $this->assertStringContainsString('password_confirmed', $form);
110
        $this->assertStringContainsString('raw_password', $form);
111
        $this->assertStringContainsString('raw_password_confirmed', $form);
112
        $this->assertStringContainsString('can_login', $form);
113
        $this->assertStringContainsString('type="hidden" id="can_login"', $form);
114
        $this->assertStringContainsString('is_gravatar_allowed', $form);
115
        $this->assertStringContainsString('user_group_ids', $form);
116
        $this->assertStringContainsString('user_language_id', $form);
117
        $this->assertStringContainsString('selected', $form);
118
        $this->assertStringContainsString('button', $form);
119
    }
120
121
    public function testCreateThrowsExceptionIfWrongEntityIsProvided()
122
    {
123
        $this->expectException(\InvalidArgumentException::class);
124
125
        $entityStub = $this->createMock(IEntity::class);
126
127
        $this->sut->create('foo', 'bar', '/baz', $entityStub);
128
    }
129
130
    /**
131
     * @return MockObject|Entity
132
     */
133
    protected function createMockEntity()
134
    {
135
        return $this->createMock(Entity::class);
136
    }
137
}
138