diff --git a/src/Providers/HttpServiceProvider.php b/src/Providers/HttpServiceProvider.php index 9d041d2..aed001a 100644 --- a/src/Providers/HttpServiceProvider.php +++ b/src/Providers/HttpServiceProvider.php @@ -23,5 +23,9 @@ class HttpServiceProvider implements ServiceProviderContract $kernel->bind('post',function (...$args){ return HttpService::post($this,...$args); }); + + $kernel->bind('postJson',function (...$args){ + return HttpService::postJson($this,...$args); + }); } } \ No newline at end of file diff --git a/src/Providers/SystemServiceProvider.php b/src/Providers/SystemServiceProvider.php index a31b802..e0299c4 100644 --- a/src/Providers/SystemServiceProvider.php +++ b/src/Providers/SystemServiceProvider.php @@ -24,5 +24,9 @@ class SystemServiceProvider implements ServiceProviderContract return $this->query()->getData($callback)->all(); }); + $kernel->bind('pipe',function (Closure $callback = null){ + return $callback($this); + }); + } } \ No newline at end of file diff --git a/src/QueryList.php b/src/QueryList.php index 5f7df09..5f3abeb 100644 --- a/src/QueryList.php +++ b/src/QueryList.php @@ -35,7 +35,9 @@ use Closure; * @method QueryList encoding(string $outputEncoding,string $inputEncoding = null) * @method QueryList get($url,$args = null,$otherArgs = []) * @method QueryList post($url,$args = null,$otherArgs = []) + * @method QueryList postJson($url,$args = null,$otherArgs = []) * @method QueryList use($plugins,...$opt) + * @method QueryList pipe(Closure $callback = null) */ class QueryList { diff --git a/src/Services/HttpService.php b/src/Services/HttpService.php index 7461d8e..77993c2 100644 --- a/src/Services/HttpService.php +++ b/src/Services/HttpService.php @@ -45,4 +45,15 @@ class HttpService $ql->setHtml($html); return $ql; } + + public static function postJson(QueryList $ql,$url,$args = null,$otherArgs = []) + { + $otherArgs = array_merge([ + 'cookies' => self::getCookieJar(), + 'verify' => false + ],$otherArgs); + $html = GHttp::postJson($url,$args,$otherArgs); + $ql->setHtml($html); + return $ql; + } } \ No newline at end of file diff --git a/tests/Dom/FindTest.php b/tests/Dom/FindTest.php new file mode 100644 index 0000000..acf5c84 --- /dev/null +++ b/tests/Dom/FindTest.php @@ -0,0 +1,71 @@ +html = $this->getSnippet('snippet-1'); + $this->ql = QueryList::html($this->html); + } + + /** + * @test + */ + public function find_first_dom_attr() + { + $img = []; + $img[] = $this->ql->find('img')->attr('src'); + $img[] = $this->ql->find('img')->src; + $img[] = $this->ql->find('img:eq(0)')->src; + $img[] = $this->ql->find('img')->eq(0)->src; + + $alt = $this->ql->find('img')->alt; + $abc = $this->ql->find('img')->abc; + + $this->assertCount(1,array_unique($img)); + $this->assertEquals($alt,'这是图片'); + $this->assertEquals($abc,'这是一个自定义属性'); + + } + + /** + * @test + */ + public function find_second_dom_attr() + { + + $img2 = []; + $img2[] = $this->ql->find('img')->eq(1)->alt; + $img2[] = $this->ql->find('img:eq(1)')->alt; + $img2[] = $this->ql->find('.second_pic')->alt; + + $this->assertCount(1,array_unique($img2)); + + } + + /** + * @test + */ + public function find_dom_all_attr() + { + $imgAttr = $this->ql->find('img:eq(0)')->attr('*'); + $linkAttr = $this->ql->find('a:eq(1)')->attr('*'); + $this->assertCount(3,$imgAttr); + $this->assertCount(1,$linkAttr); + } +} \ No newline at end of file diff --git a/tests/Feature/HttpTest.php b/tests/Feature/HttpTest.php new file mode 100644 index 0000000..6900eed --- /dev/null +++ b/tests/Feature/HttpTest.php @@ -0,0 +1,33 @@ + 'foo' + ]; + QueryList::postJson('http://foo.com',$data,[ + 'handler' => $mock + ]); + $this->assertEquals((string)$mock->getLastRequest()->getBody(),json_encode($data)); + } +} \ No newline at end of file diff --git a/tests/Feature/MethodTest.php b/tests/Feature/MethodTest.php new file mode 100644 index 0000000..684cbd0 --- /dev/null +++ b/tests/Feature/MethodTest.php @@ -0,0 +1,36 @@ +html = $this->getSnippet('snippet-1'); + } + + /** + * @test + */ + public function pipe() + { + $html = $this->html; + $qlHtml = QueryList::pipe(function(QueryList $ql) use($html){ + $ql->setHtml($html); + return $ql; + })->getHtml(); + $this->assertEquals($html,$qlHtml); + } +} \ No newline at end of file