diff --git a/tests/BodyTest.php b/tests/BodyTest.php index bf753ee..8c1936f 100755 --- a/tests/BodyTest.php +++ b/tests/BodyTest.php @@ -45,8 +45,6 @@ public function testBodyMethod(): void } /** - * Get the expected results. - * * @param $body array * * @return array @@ -57,8 +55,6 @@ protected function getExpected(array $body = []): array } /** - * Get the actual results. - * * @param $body array * * @return array diff --git a/tests/IgnoreTest.php b/tests/IgnoreTest.php index 2735b2b..0cfd016 100755 --- a/tests/IgnoreTest.php +++ b/tests/IgnoreTest.php @@ -1,45 +1,79 @@ getExpected(404), $this->getActual(404)); - self::assertEquals($this->getExpected(500, 404), $this->getActual(500, 404)); + self::assertEquals( + $this->getExpected(404), + $this->getActual(404) + ); + self::assertEquals( + $this->getExpected(500, 404), + $this->getActual(500, 404) + ); } /** - * Get The expected results. + * @param int ...$args + * * @return array + * @throws ClassAlreadyExistsException + * @throws ClassIsFinalException + * @throws DuplicateMethodException + * @throws InvalidMethodNameException + * @throws OriginalConstructorInvocationRequiredException + * @throws ReflectionException + * @throws RuntimeException + * @throws UnknownTypeException + * @throws \PHPUnit\Framework\InvalidArgumentException */ - protected function getExpected() + protected function getActual(int ...$args): array { - $query = $this->getQueryArray(); - - $query["client"]["ignore"] = func_get_args(); - - return $query; + return $this->getQueryObject() + ->ignore($args) + ->toArray(); } - /** - * Get The actual results. - * @return mixed - */ - protected function getActual() + protected function getExpected(int ...$args): array { - return $this->getQueryObject()->ignore(func_get_args())->query(); + $query = $this->getQueryArray(); + $query['client']['ignore'] = $args; + + return $query; } } diff --git a/tests/OrderTest.php b/tests/OrderTest.php index 24e7a21..293f653 100755 --- a/tests/OrderTest.php +++ b/tests/OrderTest.php @@ -32,8 +32,6 @@ public function testOrderByMethod(): void } /** - * Get The expected results. - * * @param string $field * @param string $direction * @@ -51,8 +49,6 @@ protected function getExpected( } /** - * Get The actual results. - * * @param string $field * @param string $direction * diff --git a/tests/SelectTest.php b/tests/SelectTest.php index 7bbe4a9..cf748f6 100755 --- a/tests/SelectTest.php +++ b/tests/SelectTest.php @@ -1,57 +1,83 @@ getExpected("title", "content"), - $this->getActual("title", "content") + $this->getExpected('foo', 'bar'), + $this->getActual('foo', 'bar') ); } /** - * Get The expected results. + * @param string ...$fields * * @return array + * @throws ClassAlreadyExistsException + * @throws ClassIsFinalException + * @throws DuplicateMethodException + * @throws InvalidMethodNameException + * @throws OriginalConstructorInvocationRequiredException + * @throws ReflectionException + * @throws RuntimeException + * @throws UnknownTypeException + * @throws \PHPUnit\Framework\InvalidArgumentException */ - protected function getExpected(): array + protected function getActual(string ...$fields): array { - $query = $this->getQueryArray(); - - $query["body"]["_source"]['include'] = func_get_args(); - $query["body"]["_source"]['exclude'] = []; - - return $query; + return $this + ->getQueryObject() + ->select($fields) + ->toArray(); } /** - * Get The actual results. + * @param string ...$fields * * @return array */ - protected function getActual(): array + protected function getExpected(string ...$fields): array { - return $this - ->getQueryObject() - ->select(func_get_args()) - ->query(); + $query = $this->getQueryArray(); + + $query['body']['_source']['includes'] = $fields; + $query['body']['_source']['excludes'] = []; + + return $query; } } diff --git a/tests/SkipTest.php b/tests/SkipTest.php index c7a393f..cf4a08b 100755 --- a/tests/SkipTest.php +++ b/tests/SkipTest.php @@ -28,8 +28,6 @@ public function testSkipMethod(): void } /** - * Get The expected results. - * * @param string $from * * @return array @@ -44,8 +42,6 @@ protected function getExpected(string $from): array } /** - * Get The actual results. - * * @param string $from * * @return array diff --git a/tests/Traits/ESQueryTrait.php b/tests/Traits/ESQueryTrait.php index ee312f4..fb2c55a 100755 --- a/tests/Traits/ESQueryTrait.php +++ b/tests/Traits/ESQueryTrait.php @@ -6,15 +6,6 @@ use Elasticsearch\Client; use Matchory\Elasticsearch\Connection; use Matchory\Elasticsearch\Query; -use PHPUnit\Framework\InvalidArgumentException; -use PHPUnit\Framework\MockObject\ClassAlreadyExistsException; -use PHPUnit\Framework\MockObject\ClassIsFinalException; -use PHPUnit\Framework\MockObject\DuplicateMethodException; -use PHPUnit\Framework\MockObject\InvalidMethodNameException; -use PHPUnit\Framework\MockObject\OriginalConstructorInvocationRequiredException; -use PHPUnit\Framework\MockObject\ReflectionException; -use PHPUnit\Framework\MockObject\RuntimeException; -use PHPUnit\Framework\MockObject\UnknownTypeException; /** * Class ESQueryTrait @@ -26,14 +17,14 @@ trait ESQueryTrait * * @var string */ - protected $index = "my_index"; + protected $index = 'my_index'; /** - * Test type name + * Test query offset * - * @var string + * @var int */ - protected $type = "my_type"; + protected $skip = 0; /** * Test query limit @@ -42,12 +33,18 @@ trait ESQueryTrait */ protected $take = 10; - /** - * Test query offset - * - * @var int - */ - protected $skip = 0; + protected function getClient(): Client + { + return $this + ->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->getMock(); + } + + protected function getConnection(): Connection + { + return new Connection($this->getClient()); + } /** * Expected query array @@ -60,7 +57,6 @@ protected function getQueryArray(array $body = []): array { return [ 'index' => $this->index, - 'type' => $this->type, 'body' => $body, 'from' => $this->skip, 'size' => $this->take, @@ -70,28 +66,14 @@ protected function getQueryArray(array $body = []): array /** * ES query object * + * @param Query|null $query + * * @return Query - * @throws InvalidArgumentException - * @throws ClassAlreadyExistsException - * @throws ClassIsFinalException - * @throws DuplicateMethodException - * @throws InvalidMethodNameException - * @throws OriginalConstructorInvocationRequiredException - * @throws ReflectionException - * @throws RuntimeException - * @throws UnknownTypeException */ - protected function getQueryObject(): Query + protected function getQueryObject(?Query $query = null): Query { - return (new Query( - new Connection( - $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ) - )) + return ($query ?? new Query($this->getConnection())) ->index($this->index) - ->type($this->type) ->take($this->take) ->skip($this->skip); } diff --git a/tests/WhereBetweenTest.php b/tests/WhereBetweenTest.php index b764227..2c2f007 100755 --- a/tests/WhereBetweenTest.php +++ b/tests/WhereBetweenTest.php @@ -1,69 +1,101 @@ getExpected("views", 500, 1000), - $this->getActual("views", 500, 1000) + $this->getExpected('views', 500, 1000), + $this->getActual('views', 500, 1000) ); self::assertEquals( - $this->getExpected("views", [500, 1000]), - $this->getActual("views", [500, 1000]) + $this->getExpected('views', [500, 1000]), + $this->getActual('views', [500, 1000]) ); } /** - * Get The expected results. + * @param $name + * @param $first + * @param null $last * + * @return array + * @throws InvalidArgumentException + * @throws ClassAlreadyExistsException + * @throws ClassIsFinalException + * @throws DuplicateMethodException + * @throws InvalidMethodNameException + * @throws OriginalConstructorInvocationRequiredException + * @throws ReflectionException + * @throws RuntimeException + * @throws UnknownTypeException + */ + protected function getActual($name, $first, $last = null): array + { + return $this + ->getQueryObject() + ->whereBetween($name, $first, $last) + ->toArray(); + } + + /** * @param $name - * @param $first_value - * @param $second_value + * @param $first + * @param $last * * @return array */ - protected function getExpected($name, $first_value, $second_value = null): array + protected function getExpected($name, $first, $last = null): array { $query = $this->getQueryArray(); - if (is_array($first_value) && count($first_value) == 2) { - $second_value = $first_value[1]; - $first_value = $first_value[0]; + if (is_array($first) && count($first) === 2) { + [$first, $last] = $first; } - $query["body"]["query"]["bool"]["filter"][] = [ - "range" => [ + $query['body']['query']['bool']['filter'][] = [ + 'range' => [ $name => [ - "gte" => $first_value, - "lte" => $second_value, + 'gte' => $first, + 'lte' => $last, ], ], ]; return $query; } - - /** - * Get The actual results. - * - * @param $name - * @param $first_value - * @param $second_value - * - * @return mixed - */ - protected function getActual($name, $first_value, $second_value = null) - { - return $this->getQueryObject()->whereBetween($name, $first_value, $second_value)->query(); - } } diff --git a/tests/WhereInTest.php b/tests/WhereInTest.php index 096f2f4..4ccc23e 100755 --- a/tests/WhereInTest.php +++ b/tests/WhereInTest.php @@ -1,9 +1,19 @@ getExpected("status", ["pending", "draft"]), - $this->getActual("status", ["pending", "draft"]) + $this->getExpected('status', ['pending', 'draft']), + $this->getActual('status', ['pending', 'draft']) ); } /** - * Get The expected results. - * * @param string $name * @param array $value * * @return array + * @throws \PHPUnit\Framework\InvalidArgumentException + * @throws ClassAlreadyExistsException + * @throws ClassIsFinalException + * @throws DuplicateMethodException + * @throws InvalidMethodNameException + * @throws OriginalConstructorInvocationRequiredException + * @throws ReflectionException + * @throws RuntimeException + * @throws UnknownTypeException */ - protected function getExpected(string $name, array $value = []): array + protected function getActual(string $name, array $value = []): array { - $query = $this->getQueryArray(); - - $query["body"]["query"]["bool"]["filter"][] = [ - "terms" => [ - $name => $value, - ], - ]; - - return $query; + return $this + ->getQueryObject() + ->whereIn($name, $value) + ->toArray(); } /** - * Get The actual results. - * * @param string $name * @param array $value * * @return array */ - protected function getActual($name, $value = []): array + protected function getExpected(string $name, array $value = []): array { - return $this - ->getQueryObject() - ->whereIn($name, $value) - ->query(); + $query = $this->getQueryArray(); + + $query['body']['query']['bool']['filter'][] = [ + 'terms' => [ + $name => $value, + ], + ]; + + return $query; } } diff --git a/tests/WhereNotInTest.php b/tests/WhereNotInTest.php index b3c345c..1fe578e 100755 --- a/tests/WhereNotInTest.php +++ b/tests/WhereNotInTest.php @@ -28,8 +28,6 @@ public function testWhereNotInMethod(): void } /** - * Get The expected results. - * * @param string $name * @param array $value * @@ -47,8 +45,6 @@ protected function getExpected(string $name, array $value = []): array } /** - * Get The actual results. - * * @param $name * @param array $value * diff --git a/tests/WhereNotTest.php b/tests/WhereNotTest.php index 79d09e1..c11e4c1 100755 --- a/tests/WhereNotTest.php +++ b/tests/WhereNotTest.php @@ -75,8 +75,6 @@ public function testWhereNotMethod(): void } /** - * Get The expected results. - * * @param $name * @param string $operator * @param null $value @@ -152,8 +150,6 @@ protected function getExpected( } /** - * Get The actual results. - * * @param $name * @param string $operator * @param null $value diff --git a/tests/WhereTest.php b/tests/WhereTest.php index 0906eaa..27fc439 100755 --- a/tests/WhereTest.php +++ b/tests/WhereTest.php @@ -1,10 +1,19 @@ ", - ">=", - "<", - "<=", - "like", - "exists", + '=', + '!=', + '>', + '>=', + '<', + '<=', + 'like', + 'exists', ]; /** * Test the where() method. * * @return void + * @throws ClassAlreadyExistsException + * @throws ClassIsFinalException + * @throws DuplicateMethodException * @throws ExpectationFailedException * @throws InvalidArgumentException + * @throws InvalidMethodNameException + * @throws OriginalConstructorInvocationRequiredException + * @throws ReflectionException + * @throws RuntimeException + * @throws UnknownTypeException + * @throws \InvalidArgumentException + * @throws \PHPUnit\Framework\InvalidArgumentException */ public function testWhereMethod(): void { self::assertEquals( - $this->getExpected("status", "published"), - $this->getActual("status", "published") + $this->getExpected('status', 'published'), + $this->getActual('status', 'published') ); self::assertEquals( - $this->getExpected("status", "=", "published"), - $this->getActual("status", "=", "published") + $this->getExpected('status', '=', 'published'), + $this->getActual('status', '=', 'published') ); self::assertEquals( - $this->getExpected("views", ">", 1000), - $this->getActual("views", ">", 1000) + $this->getExpected('views', '>', 1000), + $this->getActual('views', '>', 1000) ); self::assertEquals( - $this->getExpected("views", ">=", 1000), - $this->getActual("views", ">=", 1000) + $this->getExpected('views', '>=', 1000), + $this->getActual('views', '>=', 1000) ); self::assertEquals( - $this->getExpected("views", "<=", 1000), - $this->getActual("views", "<=", 1000) + $this->getExpected('views', '<=', 1000), + $this->getActual('views', '<=', 1000) ); self::assertEquals( - $this->getExpected("content", "like", "hello"), - $this->getActual("content", "like", "hello") + $this->getExpected('content', 'like', 'hello'), + $this->getActual('content', 'like', 'hello') ); self::assertEquals( - $this->getExpected("website", "exists", true), - $this->getActual("website", "exists", true) + $this->getExpected('website', 'exists', true), + $this->getActual('website', 'exists', true) ); self::assertEquals( - $this->getExpected("website", "exists", false), - $this->getActual("website", "exists", false) + $this->getExpected('website', 'exists', false), + $this->getActual('website', 'exists', false) ); } /** - * Get The expected results. + * @param string $name + * @param string $operator + * @param mixed|null $value * + * @return array + * @throws \InvalidArgumentException + * @throws \PHPUnit\Framework\InvalidArgumentException + * @throws ClassAlreadyExistsException + * @throws ClassIsFinalException + * @throws DuplicateMethodException + * @throws InvalidMethodNameException + * @throws OriginalConstructorInvocationRequiredException + * @throws ReflectionException + * @throws RuntimeException + * @throws UnknownTypeException + */ + protected function getActual( + string $name, + string $operator = '=', + $value = null + ): array { + return $this + ->getQueryObject() + ->where($name, $operator, $value) + ->toArray(); + } + + /** * @param string $name * @param string $operator * @param mixed|null $value @@ -89,7 +134,7 @@ public function testWhereMethod(): void */ protected function getExpected( string $name, - string $operator = "=", + string $operator = '=', $value = null ): array { $query = $this->getQueryArray(); @@ -100,42 +145,42 @@ protected function getExpected( true )) { $value = $operator; - $operator = "="; + $operator = '='; } $filter = []; $must = []; $must_not = []; - if ($operator === "=") { - $filter[] = ["term" => [$name => $value]]; + if ($operator === '=') { + $filter[] = ['term' => [$name => $value]]; } - if ($operator === ">") { - $filter[] = ["range" => [$name => ["gt" => $value]]]; + if ($operator === '>') { + $filter[] = ['range' => [$name => ['gt' => $value]]]; } - if ($operator === ">=") { - $filter[] = ["range" => [$name => ["gte" => $value]]]; + if ($operator === '>=') { + $filter[] = ['range' => [$name => ['gte' => $value]]]; } - if ($operator === "<") { - $filter[] = ["range" => [$name => ["lt" => $value]]]; + if ($operator === '<') { + $filter[] = ['range' => [$name => ['lt' => $value]]]; } - if ($operator === "<=") { - $filter[] = ["range" => [$name => ["lte" => $value]]]; + if ($operator === '<=') { + $filter[] = ['range' => [$name => ['lte' => $value]]]; } - if ($operator === "like") { - $must[] = ["match" => [$name => $value]]; + if ($operator === 'like') { + $must[] = ['match' => [$name => $value]]; } - if ($operator === "exists") { + if ($operator === 'exists') { if ($value) { - $must[] = ["exists" => ["field" => $name]]; + $must[] = ['exists' => ['field' => $name]]; } else { - $must_not[] = ["exists" => ["field" => $name]]; + $must_not[] = ['exists' => ['field' => $name]]; } } @@ -144,39 +189,19 @@ protected function getExpected( $bool = []; if (count($must)) { - $bool["must"] = $must; + $bool['must'] = $must; } if (count($must_not)) { - $bool["must_not"] = $must_not; + $bool['must_not'] = $must_not; } if (count($filter)) { - $bool["filter"] = $filter; + $bool['filter'] = $filter; } - $query["body"]["query"]["bool"] = $bool; + $query['body']['query']['bool'] = $bool; return $query; } - - /** - * Get The actual results. - * - * @param string $name - * @param string $operator - * @param mixed|null $value - * - * @return array - */ - protected function getActual( - string $name, - string $operator = "=", - $value = null - ): array { - return $this - ->getQueryObject() - ->where($name, $operator, $value) - ->query(); - } }