Test Failed
Push — master ( d988ba...5843df )
by Alexey
03:10
created

MainControllerTest::testAjaxUserAutoComplete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7
class MainControllerTest extends WebTestCase
8
{
9
    public function testUserSearch()
10
    {
11
        $client = static::createClient();
12
        $crawler = $client->request('GET', '/');
13
14
        $userSearchForm = $crawler->filter('form.form-inline')->form();
15
        $userSearchForm['skobkin_bundle_pointtoolsbundle_user_search[login]'] = 'testuser';
16
17
        $crawler = $client->submit($userSearchForm);
1 ignored issue
show
Documentation introduced by
$userSearchForm is of type array<string,string,{"sk...arch[login]":"string"}>, but the function expects a object<Symfony\Component\DomCrawler\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$crawler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
18
19
        $this->assertTrue($client->getResponse()->isRedirect('/user/testuser'), 'Redirect to testuser\'s page didn\'t happen');
20
    }
21
22
    public function testNonExistingUserSearch()
23
    {
24
        $client = static::createClient();
25
        $crawler = $client->request('GET', '/');
26
27
        $userSearchForm = $crawler->filter('form.form-inline')->form();
28
        $userSearchForm['skobkin_bundle_pointtoolsbundle_user_search[login]'] = 'non-existing-user';
29
30
        $crawler = $client->submit($userSearchForm);
1 ignored issue
show
Documentation introduced by
$userSearchForm is of type array<string,string,{"sk...arch[login]":"string"}>, but the function expects a object<Symfony\Component\DomCrawler\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
32
        $this->assertFalse($client->getResponse()->isRedirection(), 'Redirect to non-existing user on the main page');
33
34
        $formElement = $crawler->filter('form.form-inline')->first();
35
36
        $this->assertEquals(1, $formElement->count(), 'Form not found after searching non-existing user');
37
38
        $loginInputElement = $formElement->filter('#skobkin_bundle_pointtoolsbundle_user_search_login')->first();
39
40
        $this->assertEquals(1, $loginInputElement->count(), 'Login form input element not found after search of non existing user');
41
42
        $errorsListElement = $loginInputElement->siblings()->filter('span.help-block')->children()->filter('ul.list-unstyled')->first();
43
44
        $this->assertEquals(1, $errorsListElement->count(), 'Form errors list not found after search of non-existing user');
45
46
        $firstError = $errorsListElement->children()->first();
47
48
        $this->assertEquals(1, $firstError->count(), 'No errors in the list');
49
        $this->assertEquals(' Login not found', $firstError->text(), 'Incorrect error text');
50
    }
51
52
    public function testUserStats()
53
    {
54
        $client = static::createClient();
55
        $crawler = $client->request('GET', '/');
56
57
        $userStatsBlock = $crawler->filter('.container.service-stats');
58
59
        // Assuming we have stats block
60
        $this->assertEquals(1, $userStatsBlock->count(), 'Stats block not found');
61
        // @todo rewrite to named classes
62
        // Assuming we have at least one user shown
63
        $this->assertGreaterThan(
64
            0,
65
            $userStatsBlock->children()->first()->children()->last()->text(),
66
            'Zero service users shown on the main page'
67
        );
68
        // Assuming we have at least one subscriber
69
        $this->assertGreaterThan(
70
            0,
71
            $userStatsBlock->children()->eq(1)->children()->last()->text(),
72
            'Zero service subscribers shows on the main page'
73
        );
74
    }
75
76
    /**
77
     * Tests AJAX user search autocomplete and returns JSON response string
78
     *
79
     * @return string
80
     */
81
    public function testAjaxUserAutoComplete()
82
    {
83
        $client = static::createClient();
84
        $crawler = $client->request('GET', '/ajax/users/search/testuser');
0 ignored issues
show
Unused Code introduced by
$crawler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
86
        $this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"');
87
88
        return $client->getResponse()->getContent();
89
    }
90
91
    /**
92
     * @depends testAjaxUserAutoComplete
93
     *
94
     * @param $json
95
     */
96
    public function testAjaxUserAutoCompleteHasOptions($json)
97
    {
98
        $data = json_decode($json);
99
100
        $this->assertNotNull($data, 'JSON data successfully decoded and not empty');
101
        $this->assertTrue(is_array($data), 'JSON data is array');
102
        $this->assertGreaterThan(0, count($data), 'Array has at least one element');
103
104
        return $data;
105
    }
106
107
    /**
108
     * @depends testAjaxUserAutoCompleteHasOptions
109
     *
110
     * @param array $users
111
     */
112
    public function testAjaxUserAutoCompleteHasValidUserObjects(array $users)
113
    {
114
        foreach ($users as $key => $user) {
115
            $this->assertTrue(array_key_exists('login', $user), sprintf('%d row of result has \'login\' field', $key));
116
            $this->assertTrue(array_key_exists('name', $user), sprintf('%d row of result has \'name\' field', $key));
117
        }
118
    }
119
120
    public function testAjaxUserAutoCompleteForNonExistingUser()
121
    {
122
        $client = static::createClient();
123
        $crawler = $client->request('GET', '/ajax/users/search/aksdjhaskdjhqwhdgqkjwhdgkjah');
0 ignored issues
show
Unused Code introduced by
$crawler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
125
        $this->assertTrue($client->getResponse()->headers->contains('Content-Type', 'application/json'), 'Response has "Content-Type" = "application/json"');
126
127
        $data = json_decode($client->getResponse()->getContent());
128
129
        $this->assertNotNull($data, 'JSON data successfully decoded and not empty');
130
        $this->assertTrue(is_array($data), 'JSON data is array');
131
        $this->assertEquals(0, count($data), 'Array has no elements');
132
    }
133
}
134