BasicTest::testCssParser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace PhpQuery\Tests;
3
use PhpQuery\PhpQuery;
4
use PhpQuery\PhpQueryObject;
5
6
PhpQuery::use_function(__NAMESPACE__);
7
8
class BasicTest extends \PHPUnit_Framework_TestCase {
9
  function provider() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
10
    // TODO change filename
11
    return array(
12
      array(
13
        PhpQuery::newDocumentFile(__DIR__ . '/test.html')
14
      )
15
    );
16
  }
17
18
  /**
19
   * @param PhpQueryObject $pq
20
   * @dataProvider provider
21
   * @return void
22
   */
23
  function testFilterWithPseudoclass($pq) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
    $pq = $pq->find('p')->filter('.body:gt(1)');
25
    $result = array(
26
      'p.body',
27
    );
28
29
    $this->assertTrue($pq->whois() == $result);
30
  }
31
32
  /**
33
   * @param PhpQueryObject $pq
34
   * @dataProvider provider
35
   * @return void
36
   */
37
  function testSlice($pq) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
    $testResult = array(
39
      'li#testID',
40
    );
41
    $pq = $pq->find('li')->slice(1, 2);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a object<PhpQuery\unknown_type>.

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...
Documentation introduced by
2 is of type integer, but the function expects a object<PhpQuery\unknown_type>|null.

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...
42
43
    $this->assertTrue($pq->whois() == $testResult);
44
  }
45
46
  /**
47
   * @param PhpQueryObject $pq
48
   * @dataProvider provider
49
   * @return void
50
   */
51
  function testSlice2($pq) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
    // SLICE2
53
    $testResult = array(
54
      'li#testID',
55
      'li',
56
      'li#i_have_nested_list',
57
      'li.nested',
58
    );
59
60
    $pq = $pq->find('li')->slice(1, -1);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a object<PhpQuery\unknown_type>.

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...
Documentation introduced by
-1 is of type integer, but the function expects a object<PhpQuery\unknown_type>|null.

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...
61
62
    $this->assertTrue($pq->whois() == $testResult);
63
  }
64
65
  /**
66
   * @return void
67
   */
68
  function testMultiInsert() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
69
    // Multi-insert
70
    $pq = PhpQuery::newDocument('<li><span class="field1"></span><span class="field1"></span></li>')->find('.field1')->php('longlongtest');
71
    $validResult = '<li><span class="field1"><php>longlongtest</php></span><span class="field1"><php>longlongtest</php></span></li>';
72
    similar_text($pq->htmlOuter(), $validResult, $similarity);
73
74
    $this->assertGreaterThan(80, $similarity);
75
76
  }
77
78
  /**
79
   * @param PhpQueryObject $pq
80
   * @dataProvider provider
81
   * @return void
82
   */
83
  function testIndex($pq) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
84
    $testResult = 1;
85
    $pq = $pq->find('p')->index($pq->find('p.title:first'));
86
87
    $this->assertTrue($pq == $testResult);
88
  }
89
90
  /**
91
   * @param PhpQueryObject $pq
92
   * @dataProvider provider
93
   * @return void
94
   */
95
  function testClone($pq) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
96
    $testResult = 3;
97
    $document = null;
98
    $pq = $pq->toReference($document)->find('p:first');
0 ignored issues
show
Documentation introduced by
$document is of type null, but the function expects a object<PhpQuery\PhpQueryObject>.

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...
99
100 View Code Duplication
    foreach (array(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
      0,
102
      1,
103
      2
104
    ) as $i) {
105
      $pq->clone()->addClass("clone-test")->addClass("class-$i")->insertBefore($pq);
106
    }
107
108
    $size = $document->find('.clone-test')->size();
109
    $this->assertEquals($testResult, $size);
110
  }
111
112
  /**
113
   * @param PhpQueryObject $pq
114
   * @dataProvider provider
115
   * @return void
116
   */
117
  function testNextSibling($pq) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
118
    $testResult = 3;
0 ignored issues
show
Unused Code introduced by
$testResult 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...
119
    $document = null;
0 ignored issues
show
Unused Code introduced by
$document 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...
120
    $result = $pq->find('li:first')->next()->next()->prev()->is('#testID');
121
122
    $this->assertTrue($result);
123
  }
124
125
  /**
126
   * @param PhpQueryObject $pq
127
   * @dataProvider provider
128
   * @return void
129
   */
130
  function testSimpleDataInsertion($pq) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
131
    $testName = 'Simple data insertion';
0 ignored issues
show
Unused Code introduced by
$testName 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...
132
    $testResult = <<<EOF
133
<div class="articles">
134
        div.articles text node
135
        <ul>
136
137
        <li>
138
                <p>This is paragraph of first LI</p>
139
                <p class="title">News 1 title</p>
140
                <p class="body">News 1 body</p>
141
            </li>
142
143
<li>
144
                <p>This is paragraph of first LI</p>
145
                <p class="title">News 2 title</p>
146
                <p class="body">News 2 body</p>
147
            </li>
148
<li>
149
                <p>This is paragraph of first LI</p>
150
                <p class="title">News 3</p>
151
                <p class="body">News 3 body</p>
152
            </li>
153
</ul>
154
<p class="after">paragraph after UL</p>
155
    </div>
156
EOF;
157
    $expected_pq = PhpQuery::newDocumentHTML($testResult);
158
    $rows = array(
159
      array(
160
        'title' => 'News 1 title',
161
        'body' => 'News 1 body',
162
      ),
163
      array(
164
        'title' => 'News 2 title',
165
        'body' => 'News 2 body',
166
      ),
167
      array(
168
        'title' => 'News 3',
169
        'body' => 'News 3 body',
170
      ),
171
    );
172
    $articles = $pq->find('.articles ul');
173
    $rowSrc = $articles->find('li')->remove()->eq(0);
174 View Code Duplication
    foreach ($rows as $r) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
      $row = $rowSrc->_clone();
176
      foreach ($r as $field => $value) {
177
        $row->find(".{$field}")->html($value);
178
        //		die($row->htmlOuter());
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
179
      }
180
      $row->appendTo($articles);
181
    }
182
    $result = $pq->find('.articles')->htmlOuter();
0 ignored issues
show
Unused Code introduced by
$result 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...
183
    //         print htmlspecialchars("<pre>{$result}</pre>").'<br />';
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
184
185
    $this->assertEqualXMLStructure($expected_pq->find('.articles')->elements[0], $pq->find('.articles')->elements[0]);
186
    //         $this->assertEqualXMLStructure(DOMDocument::loadHTML($testResult)->documentElement, DOMDocument::loadHTML($result)->documentElement);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
187
  }
188
189
  /**
190
   * @param PhpQueryObject $pq
0 ignored issues
show
Bug introduced by
There is no parameter named $pq. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
191
   * @dataProvider provider
192
   * @return void
193
   */
194
  public function testCssParser() {
195
    PhpQuery::$enableCssShorthand = FALSE;
196
197
    $expected_html = '<div style="color:red;display:none;margin:20px;padding:10px"><span>Hello World!</span></div>';
198
    $expected_pq = PhpQuery::newDocumentHTML($expected_html);
199
200
    $test_pq = PhpQuery::newDocumentHTML('<div style="margin:10px; padding:10px">');
0 ignored issues
show
Unused Code introduced by
$test_pq 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...
201
    $test = pq('div');
202
    $test->append('<span>Hello World!</span>');
203
    $test->hide();
204
    $test->css('color', 'red');
205
    $test->css('margin', '20px');
206
    $this->assertEqualXMLStructure($expected_pq->find('div')->elements[0], $test->elements[0]);
207
  }
208
}
209