Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Nov 30, 2023
1 parent 9f4aaee commit 9e02423
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 1 deletion.
98 changes: 98 additions & 0 deletions src/SonsOfPHP/Component/Search/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,102 @@
*/
class Query implements QueryInterface, \ArrayAccess, \JsonSerializable
{
private array $fields = [
'offset' => 0,
'length' => null,
];

/**
* {@inheritdoc}
*/
public function has(string $field): bool
{
return array_key_exists($field, $this->fields);
}

/**
* {@inheritdoc}
*/
public function get(string $field): mixed
{
if (!$this->has($field)) {
return null;
}

return $this->fields[$field];
}

/**
* {@inheritdoc}
*/
public function remove(string $field): self
{
if (!in_array(strtolower($field), ['offset', 'length'])) {
unset($this->fields[$field]);
}

return $this;
}

/**
* {@inheritdoc}
*/
public function set(string $field, mixed $value): self
{
if (0 === strcasecmp('offset', $field) || 0 === strcasecmp('length', $field)) {
if (0 === strcasecmp('offset', $field) && !is_int($value)) {
throw new \InvalidArgumentException('Offset must be integer');
}

if (0 === strcasecmp('length', $field) && !is_int($value) && !is_null($value)) {
throw new \InvalidArgumentException('Length must be integer or null');
}
$field = strtolower($field);
}

$this->fields[$field] = $value;

return $this;
}

/**
* {@inheritdoc}
*/
public function getOffset(): int
{
return $this->fields['offset'];
}

/**
* {@inheritdoc}
*/
public function getLength(): ?int
{
return $this->fields['length'];
}

public function offsetExists(mixed $offset): bool
{
return $this->has($offset);
}

public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}

public function offsetSet(mixed $offset, mixed $value): void
{
$this->set($offset, $value);
}

public function offsetUnset(mixed $offset): void
{
$this->remove($offset);
}

public function jsonSerialize(): mixed
{
return $this->fields;
}
}
199 changes: 199 additions & 0 deletions src/SonsOfPHP/Component/Search/Tests/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Search\Tests;

use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Search\Query;
use SonsOfPHP\Contract\Search\QueryInterface;

/**
* @coversDefaultClass \SonsOfPHP\Component\Search\Query
*
* @uses \SonsOfPHP\Component\Search\Query
*/
final class QueryTest extends TestCase
{
/**
* @coversNothing
*/
public function testItHasTheCorrectInterface(): void
{
$query = new Query();

$this->assertInstanceOf(QueryInterface::class, $query);
}

/**
* @covers ::getOffset
*/
public function testGetOffset(): void
{
$query = new Query();

$this->assertSame(0, $query->getOffset());
$query['offset'] = 100;
$this->assertSame(100, $query->getOffset());
}

/**
* @covers ::getLength
*/
public function testGetLength(): void
{
$query = new Query();

$this->assertNull($query->getLength());
$query['length'] = 100;
$this->assertSame(100, $query->getLength());
}

/**
* @covers ::has
*/
public function testHas(): void
{
$query = new Query();

// defaults
$this->assertTrue($query->has('offset'));
$this->assertTrue($query->has('length'));

$this->assertFalse($query->has('testing'));
$query->set('testing', 'test');
$this->assertTrue($query->has('testing'));
}

/**
* @covers ::get
*/
public function testGet(): void
{
$query = new Query();

// defaults
$this->assertSame(0, $query->get('offset'));
$this->assertNull($query->get('length'));

$value = 'test value';
$this->assertSame($query, $query->set('value', $value));
$this->assertSame($value, $query->get('value'));
}

/**
* @covers ::get
*/
public function testGetWhenFieldDoesNotExist(): void
{
$query = new Query();

$this->assertNull($query->get('not a real field'));
}

/**
* @covers ::remove
*/
public function testRemove(): void
{
$query = new Query();
$this->assertFalse($query->has('unit.test'));
$query->set('unit.test', 'value');
$this->assertTrue($query->has('unit.test'));

$this->assertSame($query, $query->remove('unit.test'));
$this->assertFalse($query->has('unit.test'));
}

/**
* @covers ::remove
*/
public function testRemoveWithFieldThatDoesNotExist(): void
{
$query = new Query();
$this->assertFalse($query->has('unit.test'));
$this->assertSame($query, $query->remove('unit.test'));
$this->assertFalse($query->has('unit.test'));
}

/**
* @covers ::remove
*/
public function testRemoveCannotRemoveField(): void
{
$query = new Query();

$this->assertTrue($query->has('offset'));
$this->assertTrue($query->has('length'));

$this->assertSame($query, $query->remove('offset'));
$this->assertSame($query, $query->remove('length'));

$this->assertTrue($query->has('offset'));
$this->assertTrue($query->has('length'));
}

/**
* @covers ::set
*/
public function testItCanSetOffset(): void
{
$query = new Query();

$this->assertSame($query, $query->set('offset', 100));
$this->assertSame(100, $query->get('offset'));
}

/**
* @covers ::set
*/
public function testItCanSetLength(): void
{
$query = new Query();

$this->assertSame($query, $query->set('length', 100));
$this->assertSame(100, $query->get('length'));
}

/**
* @covers ::set
*/
public function testSet(): void
{
$query = new Query();

$this->assertFalse($query->has('field'));
$this->assertSame($query, $query->set('field', 'value'));
$this->assertTrue($query->has('field'));
$this->assertSame('value', $query->get('field'));
}

/**
* @covers ::offsetExists
* @covers ::offsetGet
* @covers ::offsetSet
* @covers ::offsetUnset
*/
public function testItWorksLikeAnArray(): void
{
$query = new Query();

$query['field'] = 'value';
$this->assertSame('value', $query['field']);

$this->assertArrayHasKey('field', $query);
unset($query['field']);
$this->assertArrayNotHasKey('field', $query);
}

/**
* @covers ::jsonSerialize
*/
public function testJsonSerialize(): void
{
$query = new Query();

// default
$this->assertSame('{"offset":0,"length":null}', json_encode($query));
}
}
4 changes: 3 additions & 1 deletion src/SonsOfPHP/Contract/Search/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
*/
interface QueryInterface// extends \ArrayAccess, \JsonSerializable
{
public function get(string $field): self;
public function has(string $field): bool;
public function get(string $field): mixed;
public function remove(string $field): self;
public function set(string $field, mixed $value): self;

public function getOffset(): int;
Expand Down

0 comments on commit 9e02423

Please sign in to comment.