Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Implement remove and contains method for Providers\DSL\Compilation\Pa…
Browse files Browse the repository at this point in the history
…rameters\ParameterCollection class
  • Loading branch information
Elliot Levin committed Apr 9, 2015
1 parent fcf5b32 commit 3bdbc66
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ dev-master
- Refactor `Analysis\PhpTypeSystem::getTypeFromValue` method logic into new static method `Analysis\TypeId::fromValue`
- Fix bug when attempting to parse function with a magic scope parameter type hint (eg `function (self $param) { ... }`.
- Implement `Analysis\TolerantExpressionAnalyser` which will convert analysis exceptions into the *mixed* type.
- Add `Providers\DSL\Compilation\Parameters\ParameterCollection::contains` to check whether the collection contains a parameter.
- Add `Providers\DSL\Compilation\Parameters\ParameterCollection::remove` to remove a previously added parameter.

3.1.0 (29/3/15)
===============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ public function add(IQueryParameter $parameter)
$this->parameters[] = $parameter;
}

/**
* Removes a query parameter from the collection.
*
* @param IQueryParameter $parameter
*
* @return void
*/
public function remove(IQueryParameter $parameter)
{
foreach($this->parameters as $key => $someParameter) {
if($someParameter === $parameter) {
unset($this->parameters[$key]);
}
}
}

/**
* Builds an immutable parameter registry from the added parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public function count()
return count($this->parameters);
}

/**
* Returns whether the collection contains the supplied parameter.
*
* @param IQueryParameter $parameter
*
* @return bool
*/
public function contains(IQueryParameter $parameter)
{
return in_array($parameter, $this->parameters, true);
}

/**
* @return IQueryParameter[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class StandardParameter extends QueryParameterBase
*/
protected $parameterId;

/**
* @param string $parameterId
* @param IParameterHasher $hasher
* @param null $data
*/
public function __construct($parameterId, IParameterHasher $hasher, $data = null)
{
parent::__construct($hasher, $data);
Expand Down
43 changes: 43 additions & 0 deletions Tests/Integration/Providers/DSL/ParameterCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Pinq\Tests\Integration\Providers\DSL;

use Pinq\Expressions as O;
use Pinq\Providers\DSL\Compilation\Parameters\IQueryParameter;
use Pinq\Providers\DSL\Compilation\Parameters\ParameterCollection;
use Pinq\Providers\DSL\Compilation\Parameters\ExpressionParameter;
use Pinq\Providers\DSL\Compilation\Parameters\ParameterHasher;
use Pinq\Providers\DSL\Compilation\Parameters\StandardParameter;
use Pinq\Queries\Functions;
use Pinq\Queries\ResolvedParameterRegistry;
use Pinq\Tests\PinqTestCase;
Expand Down Expand Up @@ -140,6 +142,47 @@ public function testStandardParameterId()
$this->assertResolvesTo(['123ewq'], ['foo-bar' => '123ewq']);
}

public function testCustomParameter()
{
$parameterMock = $this->getMock('Pinq\Providers\DSL\Compilation\Parameters\IQueryParameter');
$parameterMock->expects($this->any())
->method('evaluate')
->with($this->equalTo(new ResolvedParameterRegistry(['abc' => 'foobar'])))
->willReturn('resolved-value');

$this->collection->add($parameterMock);

$this->assertResolvesTo(['resolved-value'], ['abc' => 'foobar']);
}

public function testContainsParameter()
{
$this->collection->addId('foo', ParameterHasher::valueType());
$this->collection->addId('bar', ParameterHasher::valueType());

$parameters = $this->collection->getParameters();

$this->assertTrue($this->collection->contains($parameters[0]));
$this->assertFalse($this->collection->contains(new StandardParameter('some-id', ParameterHasher::valueType())));
}

public function testRemoveParameter()
{
$this->collection->addId('foo', ParameterHasher::valueType());
$this->collection->addId('bar', ParameterHasher::valueType());

$this->assertCount(2, $this->collection->getParameters());

$parameters = $this->collection->getParameters();
$this->collection->remove($parameters[0]);

$this->assertSame([1 => $parameters[1]], $this->collection->getParameters());

$this->collection->remove($parameters[1]);

$this->assertSame([], $this->collection->getParameters());
}

public function testCollectionSuppliesCorrectParameterHasToExpressionParameter()
{
$this->collection->addExpression(O\Expression::value('val-test'), $hasher = ParameterHasher::valueType());
Expand Down

0 comments on commit 3bdbc66

Please sign in to comment.