Skip to content

Commit

Permalink
Merge pull request #1 from xinningsu/more-testing-functions
Browse files Browse the repository at this point in the history
more testing functions
  • Loading branch information
xinningsu committed Jul 21, 2019
2 parents 87df37a + 4846370 commit 941dea7
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea
/.DS_Store
/composer.lock
/build
/vendor
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ install:

script:
- vendor/bin/phpcs -n --standard=PSR12 --ignore=./vendor/ --extensions=php ./
- mkdir -p build/logs
- vendor/bin/phpunit

after_success:
- travis_retry php vendor/bin/php-coveralls -v
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# html-query

[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
[![Build Status](https://api.travis-ci.org/xinningsu/html-query.svg?branch=master)](https://travis-ci.org/xinningsu/html-query)
[![Coverage Status](https://coveralls.io/repos/github/xinningsu/html-query/badge.svg?branch=master)](https://coveralls.io/github/xinningsu/html-query)

A jQuery-like html processor written in PHP

# Quick Start
Expand Down Expand Up @@ -35,6 +40,32 @@ echo $hq('.content')->text();
//this is content...
```

### Set contents
```php
<?php
$html = '
<html>
<head>
<title>Html Query</title>
</head>
<body>
<h1 class="title">this is title</h1>
<div class="content">this is <b>content</b>...</div>
</body>
</html>
';
$hq = HQ::html($html);

echo $hq('.title')->html('this is new title')->html();
//this is new title

echo $hq('.content')->html('this is <b>new content</b>...');
//this is <b>new content</b>...

echo $hq('.content')->text('this is new content...')->html();
//this is new content...
```

### Get attributes
```php
<?php
Expand Down Expand Up @@ -215,4 +246,4 @@ echo $hq('.container')->outerHtml();

# License

MIT
[MIT](./LICENSE)
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"require-dev": {
"phpunit/phpunit": "~7.5",
"squizlabs/php_codesniffer": "3.*"
"squizlabs/php_codesniffer": "3.*",
"php-coveralls/php-coveralls": "^2.1"
},
"autoload": {
"psr-4": {
Expand All @@ -29,4 +30,4 @@
"tests/"
]
}
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
16 changes: 8 additions & 8 deletions src/HtmlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public function getVal()
return $selected->getAttr('value');
}

$fistChild = $ht->children('option:first-child');
$fistChild = $ht->xpathFind('child::*[1]');
if ($fistChild->count()) {
return $fistChild->getAttr('value');
}
Expand All @@ -456,7 +456,7 @@ public function setVal(string $value)
{
return $this->each(function (DOMNode $node) use ($value) {
if (!($node instanceof DOMElement)) {
return null;
return;
}

switch ($node->tagName) {
Expand Down Expand Up @@ -694,10 +694,10 @@ public function setCss(string $name, ?string $value)

$css = Helper::splitCss($style);
if (!array_key_exists($name, $css)) {
$keys = array_keys($css);
$allKeys = array_keys($css);
$arr = array_combine(
$keys,
array_change_key_case($keys, CASE_LOWER)
$allKeys,
array_map('strtolower', $allKeys)
);

$keys = array_keys($arr, strtolower($name));
Expand Down Expand Up @@ -733,10 +733,10 @@ public function removeCss(string $name)
unset($css[$name]);
$removed = true;
} else {
$keys = array_keys($css);
$allKeys = array_keys($css);
$arr = array_combine(
$keys,
array_change_key_case($keys, CASE_LOWER)
$allKeys,
array_map('strtolower', $allKeys)
);

$keys = array_keys($arr, strtolower($name));
Expand Down
6 changes: 1 addition & 5 deletions src/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,7 @@ protected function relationResolve(string $relation, ?string $until = null)
*/
protected function shouldResolve(Closure $function, $index = 0)
{
try {
$reflection = new ReflectionFunction($function);
} catch (ReflectionException $exception) {
return false;
}
$reflection = new ReflectionFunction($function);

$parameters = $reflection->getParameters();
if ($parameters && array_key_exists($index, $parameters)) {
Expand Down
44 changes: 40 additions & 4 deletions tests/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function testAttr()
100,
$hq->find('img')->eq(1)->setAttr('width', 100)->attr('width')
);

$this->assertNull($hq->attr('none'));
}

public function testRemoveAttr()
Expand Down Expand Up @@ -152,6 +154,8 @@ public function testHasAttr()

$this->assertTrue($hq->find('img')->eq(0)->hasAttr('alt'));
$this->assertFalse($hq->find('img')->eq(1)->hasAttr('alt'));

$this->assertFalse($hq->hasAttr('alt'));
}

public function testProp()
Expand Down Expand Up @@ -229,10 +233,15 @@ public function testData()
$hq->find('.p-0')->data('id', 2)->outerHtml()
);

$hq->find('.p-0')->data(
'content',
['id' => 1, 'tag' => 'dom']
)->removeData('id');
$this->assertEquals(
'<p class="p-0" data-id="2" data-name="test">test</p>',
$hq->find('.p-0')->data(['name' => 'test'])->outerHtml()
);

$hq->find('.p-0')
->data('content', ['id' => 1, 'tag' => 'dom'])
->removeData('id')
->removeData('name');

$this->assertEquals(
'<p class="p-0" data-content=\'{"id":1,"tag":"dom"}\'>test</p>',
Expand Down Expand Up @@ -350,6 +359,11 @@ public function testRemoveClass()
'<p>test</p>',
$hq->find('p')->removeClass()->outerHtml()
);

$this->assertEquals(
'<p>test</p>',
$hq->find('p')->removeClass('test')->outerHtml()
);
}

public function testToggleClass()
Expand Down Expand Up @@ -395,6 +409,11 @@ public function testToggleClass()
'<p>test</p>',
$hq->find('p')->toggleClass(null, false)->outerHtml()
);

$this->assertEquals(
'<p class="foo">test</p>',
$hq->find('p')->toggleClass('foo')->outerHtml()
);
}

public function testCss()
Expand Down Expand Up @@ -477,5 +496,22 @@ public function testCss()
</div>',
$hq->outerHtml()
);

$this->assertNull($hq->find('.content')->css('none'));

$hq->find('img')->eq(0)->css('WIDTH', '100px');
$this->assertEquals('100px', $hq->find('img')->eq(0)->css('width'));

$hq->find('img')->eq(0)->css('width', '99px');
$this->assertEquals(
'<img src="1.png" style="width: 99px;">',
$hq->find('img')->eq(0)->outerHtml()
);

$hq->find('img')->eq(0)->removeCss('WIDTH');
$this->assertEquals(
'<img src="1.png" style="">',
$hq->find('img')->eq(0)->outerHtml()
);
}
}
22 changes: 22 additions & 0 deletions tests/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ public function testVal()
->val("It's really good.")
->val()
);

$hq->find('option')->removeAttr('selected');
$this->assertEquals(
'1',
$hq->find("select[name='type']")->val()
);

$hq->val('test');
$this->assertNull($hq->val());
$this->assertNull($hq->find('form')->val());

$html = '
<form method="post" action="/">
<select class="foo" name="type">
</select>
</form>
';
$hq = HQ::html($html);
$this->assertNull($hq->find("select[name='type']")->val());

$hq->find("select[name='type']")->val(2);
$this->assertNull($hq->find("select[name='type']")->val());
}

public function testHtml()
Expand Down
38 changes: 34 additions & 4 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class ExampleTest extends TestCase
{
public function testExample1()
public function testGetContents()
{
$html = '
<html>
Expand Down Expand Up @@ -38,7 +38,37 @@ public function testExample1()
);
}

public function testExample2()
public function testSetContents()
{
$html = '
<html>
<head>
<title>Html Query</title>
</head>
<body>
<h1 class="title">this is title</h1>
<div class="content">this is <b>content</b>...</div>
</body>
</html>
';
$hq = HQ::html($html);

$this->assertEquals(
'this is new title',
$hq('.title')->html('this is new title')->html()
);
$this->assertEquals(
'this is <b>new content</b>...',
$hq('.content')->html('this is <b>new content</b>...')->html()
);

$this->assertEquals(
'this is new content...',
$hq('.content')->text('this is new content...')->html()
);
}

public function testGetAttributes()
{
$html = '
<div class="container">
Expand Down Expand Up @@ -75,7 +105,7 @@ public function testExample2()
);
}

public function testExample3()
public function testChangeAttributes()
{
$html = '
<div class="container">
Expand Down Expand Up @@ -136,7 +166,7 @@ public function testExample3()
);
}

public function testExample4()
public function testChangeStructure()
{
$html = '
<div class="container">
Expand Down

0 comments on commit 941dea7

Please sign in to comment.