Skip to content

Commit

Permalink
Cleaned up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Radiergummi committed Nov 30, 2021
1 parent 9ffab37 commit e4af833
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 219 deletions.
4 changes: 0 additions & 4 deletions tests/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public function testBodyMethod(): void
}

/**
* Get the expected results.
*
* @param $body array
*
* @return array
Expand All @@ -57,8 +55,6 @@ protected function getExpected(array $body = []): array
}

/**
* Get the actual results.
*
* @param $body array
*
* @return array
Expand Down
72 changes: 53 additions & 19 deletions tests/IgnoreTest.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,79 @@
<?php

declare(strict_types=1);

namespace Matchory\Elasticsearch\Tests;

use Matchory\Elasticsearch\Tests\Traits\ESQueryTrait;

use PHPUnit\Framework\ExpectationFailedException;
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;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\RecursionContext\InvalidArgumentException;

class IgnoreTest extends TestCase
{

use ESQueryTrait;

/**
* Test the ignore() method.
* @return void
* @throws ClassAlreadyExistsException
* @throws ClassIsFinalException
* @throws DuplicateMethodException
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws InvalidMethodNameException
* @throws OriginalConstructorInvocationRequiredException
* @throws ReflectionException
* @throws RuntimeException
* @throws UnknownTypeException
* @throws \PHPUnit\Framework\InvalidArgumentException
* @test
*/
public function testIgnoreMethod(): void
public function ignore(): void
{
self::assertEquals($this->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;
}
}
4 changes: 0 additions & 4 deletions tests/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function testOrderByMethod(): void
}

/**
* Get The expected results.
*
* @param string $field
* @param string $direction
*
Expand All @@ -51,8 +49,6 @@ protected function getExpected(
}

/**
* Get The actual results.
*
* @param string $field
* @param string $direction
*
Expand Down
66 changes: 46 additions & 20 deletions tests/SelectTest.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,83 @@
<?php

declare(strict_types=1);

namespace Matchory\Elasticsearch\Tests;

use Matchory\Elasticsearch\Tests\Traits\ESQueryTrait;
use PHPUnit\Framework\ExpectationFailedException;
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;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\RecursionContext\InvalidArgumentException;

class SelectTest extends TestCase
{

use ESQueryTrait;

/**
* Test the select() method.
*
* @return void
* @throws ClassAlreadyExistsException
* @throws ClassIsFinalException
* @throws DuplicateMethodException
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws InvalidMethodNameException
* @throws OriginalConstructorInvocationRequiredException
* @throws ReflectionException
* @throws RuntimeException
* @throws UnknownTypeException
* @throws \PHPUnit\Framework\InvalidArgumentException
* @test
*/
public function testIgnoreMethod(): void
public function select(): void
{
self::assertEquals(
$this->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;
}
}
4 changes: 0 additions & 4 deletions tests/SkipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public function testSkipMethod(): void
}

/**
* Get The expected results.
*
* @param string $from
*
* @return array
Expand All @@ -44,8 +42,6 @@ protected function getExpected(string $from): array
}

/**
* Get The actual results.
*
* @param string $from
*
* @return array
Expand Down
58 changes: 20 additions & 38 deletions tests/Traits/ESQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit e4af833

Please sign in to comment.