Skip to content

Commit

Permalink
re #1 fix structure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tonydspaniard committed Jun 10, 2019
1 parent 8023b4c commit 0c5bd5d
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 52 deletions.
15 changes: 3 additions & 12 deletions src/Altair/Structure/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,6 @@ public function get($key, $default = null)
return $pair->value;
}

if (func_num_args() === 1) {
throw new OutOfBoundsException('Out of bounds');
}

return $default;
}

Expand Down Expand Up @@ -411,11 +407,6 @@ public function remove($key, $default = null)
}
}

// Check if a default was provided
if (func_num_args() === 1) {
throw new OutOfBoundsException('Out of bounds');
}

return $default;
}

Expand Down Expand Up @@ -458,23 +449,23 @@ public function &offsetGet($offset)
return $pair->value;
}

throw new OutOfBoundsException();
throw new OutOfBoundsException('Out of bounds');
}

/**
* {@inheritDoc}
*/
public function offsetUnset($offset)
{
$this->remove($offset, null);
$this->remove($offset);
}

/**
* {@inheritDoc}
*/
public function offsetExists($offset)
{
return $this->get($offset, null) !== null;
return $this->get($offset) !== null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Altair/Structure/Pair.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __get($name)

return;
}
throw new OutOfBoundsException();
throw new OutOfBoundsException('Out of bounds');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Altair/Structure/PriorityNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public function __get($name)

return;
}
throw new OutOfBoundsException();
throw new OutOfBoundsException('Out of bounds');
}
}
6 changes: 3 additions & 3 deletions src/Altair/Structure/PriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function count(): int
public function peek()
{
if ($this->isEmpty()) {
throw new UnderflowException();
throw new UnderflowException('Queue is empty');
}

return $this->heap[0]->value;
Expand All @@ -84,7 +84,7 @@ public function peek()
public function pop()
{
if ($this->isEmpty()) {
throw new UnderflowException();
throw new UnderflowException('Queue is empty');
}

// Last leaf of the heap to become the new root.
Expand Down Expand Up @@ -180,7 +180,7 @@ protected function right(int $index): int
*/
protected function parent(int $index): int
{
return ($index - 1) / 2;
return (int)(($index - 1) / 2);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Altair/Structure/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function offsetSet($offset, $value)
if ($offset === null) {
$this->push($value);
} else {
throw new OutOfBoundsException();
throw new OutOfBoundsException('Out of bounds');
}
}

Expand All @@ -131,7 +131,7 @@ public function offsetSet($offset, $value)
*/
public function offsetGet($offset)
{
throw new Error();
throw new Error('Not supported');
}

/**
Expand All @@ -141,7 +141,7 @@ public function offsetGet($offset)
*/
public function offsetUnset($offset)
{
throw new Error();
throw new Error('Not supported');
}

/**
Expand All @@ -151,6 +151,6 @@ public function offsetUnset($offset)
*/
public function offsetExists($offset)
{
throw new Error();
throw new Error('Not supported');
}
}
6 changes: 3 additions & 3 deletions src/Altair/Structure/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public function offsetSet($offset, $value)
return;
}

throw new OutOfBoundsException();
throw new OutOfBoundsException('Out of bounds');
}

/**
Expand All @@ -331,7 +331,7 @@ public function offsetGet($offset)
*/
public function offsetExists($offset)
{
throw new Error();
throw new Error('Not supported');
}

/**
Expand All @@ -341,6 +341,6 @@ public function offsetExists($offset)
*/
public function offsetUnset($offset)
{
throw new Error();
throw new Error('Not supported');
}
}
8 changes: 4 additions & 4 deletions src/Altair/Structure/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function offsetSet($offset, $value)
if ($offset === null) {
$this->push($value);
} else {
throw new OutOfBoundsException();
throw new OutOfBoundsException('Out of bounds');
}
}

Expand All @@ -140,7 +140,7 @@ public function offsetSet($offset, $value)
*/
public function offsetGet($offset)
{
throw new Error();
throw new Error('Not supported');
}

/**
Expand All @@ -150,7 +150,7 @@ public function offsetGet($offset)
*/
public function offsetUnset($offset)
{
throw new Error();
throw new Error('Not supported');
}

/**
Expand All @@ -160,6 +160,6 @@ public function offsetUnset($offset)
*/
public function offsetExists($offset)
{
throw new Error();
throw new Error('Not supported');
}
}
13 changes: 8 additions & 5 deletions src/Altair/Structure/Traits/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ abstract public function toArray(): array;
* Pushes all values of either an array or traversable object.
* @param mixed $values
*/
protected function pushAll($values)
protected function pushAll($values): void
{
foreach ($values as $value) {
$this->internal[] = $value;
Expand All @@ -150,15 +150,18 @@ protected function pushAll($values)
*
* @return array
*/
protected function normalizeItems($items)
protected function normalizeItems($items): array
{
if (is_array($items)) {
return $items;
} elseif ($items instanceof CollectionInterface) {
}
if ($items instanceof CollectionInterface) {
return $items->toArray();
} elseif ($items instanceof JsonSerializable) {
}
if ($items instanceof JsonSerializable) {
return $items->jsonSerialize();
} elseif ($items instanceof Traversable) {
}
if ($items instanceof Traversable) {
return iterator_to_array($items);
}

Expand Down
16 changes: 8 additions & 8 deletions src/Altair/Structure/Traits/SequenceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function find($value)
public function first()
{
if (empty($this->internal)) {
throw new UnderflowException();
throw new UnderflowException('Is empty');
}

return $this->internal[0];
Expand All @@ -111,7 +111,7 @@ public function get(int $index)
public function insert(int $index, ...$values): SequenceInterface
{
if ($index < 0 || $index > count($this->internal)) {
throw new OutOfRangeException();
throw new OutOfRangeException('Out of bounds');
}

return new static(array_splice($this->internal, $index, 0, $values));
Expand All @@ -131,7 +131,7 @@ public function join(string $glue = null): string
public function last()
{
if ($this->isEmpty()) {
throw new UnderflowException();
throw new UnderflowException('Is empty');
}

return end($this->internal);
Expand All @@ -151,7 +151,7 @@ public function map(callable $callback): SequenceInterface
public function pop()
{
if ($this->isEmpty()) {
throw new UnderflowException();
throw new UnderflowException('Is empty');
}

$value = array_pop($this->internal);
Expand Down Expand Up @@ -238,7 +238,7 @@ public function set(int $index, $value): SequenceInterface
public function shift()
{
if ($this->isEmpty()) {
throw new UnderflowException();
throw new UnderflowException('Is empty');
}

$value = array_shift($this->internal);
Expand Down Expand Up @@ -338,7 +338,7 @@ public function &offsetGet($offset)
public function offsetUnset($offset)
{
// Unset should be quiet, so we shouldn't allow 'remove' to throw.
if (is_integer($offset) && $offset >= 0 && $offset < count($this)) {
if (is_int($offset) && $offset >= 0 && $offset < count($this)) {
$this->remove($offset);
}
}
Expand All @@ -360,10 +360,10 @@ public function offsetExists($offset)
*
* @param int $index
*/
protected function checkRange(int $index)
protected function checkRange(int $index): void
{
if ($index < 0 || $index >= count($this->internal)) {
throw new OutOfRangeException();
throw new OutOfRangeException('Out of range');
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Middleware/CorsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function setUp()
->setServerOrigin([
'scheme' => 'http',
'host' => 'example.com',
'port' => '123'
'port' => 123
])
->setRequestAllowedOrigins([
'http://good.example.com:321' => true,
Expand Down
5 changes: 2 additions & 3 deletions tests/Structure/Map/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ public function testGetDefault()
$this->assertEquals('a', $instance->get('?', 'a'));
}

public function testGetKeyNotFound()
public function testGetKeyNotFoundIsNull()
{
$instance = $this->getInstance();
$this->expectKeyNotFoundException();
$instance->get('?');
$this->assertEquals(null, $instance->get('?'));
}

public function testArrayAccessGet()
Expand Down
13 changes: 6 additions & 7 deletions tests/Structure/Map/remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public function testRandomRemove()
$reference = [];

for ($i = 0; $i < 10; $i++) {
for ($i = 0; $i < self::MANY; $i++) {
$k = rand(0, self::MANY * 2);
$v = rand();
for ($j = 0; $j < self::MANY; $j++) {
$k = random_int(0, self::MANY * 2);
$v = mt_rand();

$reference[$k] = $v;
$instance[$k] = $v;
}

for ($i = 0; $i < self::MANY; $i++) {
for ($l = 0; $l < self::MANY; $l++) {
$k = rand(0, self::MANY * 2);

unset($reference[$k], $instance[$k]);
Expand Down Expand Up @@ -123,10 +123,9 @@ public function testRemoveDefault()
$this->assertEquals('a', $instance->remove('?', 'a'));
}

public function testRemoveKeyNotFound()
public function testRemoveKeyNotFoundIsEqualNull()
{
$instance = $this->getInstance();
$this->expectKeyNotFoundException();
$instance->remove('?');
$this->assertEquals(null, $instance->remove('?'));
}
}

0 comments on commit 0c5bd5d

Please sign in to comment.