-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
addElement() and removeElement() added to QueryBuilder
- Loading branch information
Mauro Cassani
committed
Jan 8, 2018
1 parent
e94cf78
commit 1fa7491
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,23 @@ $array = [ | |
|
||
QueryBuilder::create($array); | ||
|
||
// to add an element to your array | ||
$element = [ | ||
'id' => 4, | ||
'title' => 'Patricia Lebsack', | ||
'email' => '[email protected]', | ||
'rate' => 2, | ||
'company' => [ | ||
'name' => 'Robel-Corkery', | ||
'catchPhrase' => 'Multi-tiered zero tolerance productivity', | ||
'bs' => 'transition cutting-edge web services' | ||
] | ||
]; | ||
$qb->addElement($element, 4); | ||
|
||
// to remove an element from array by his key | ||
$qb->removeElement(3); | ||
|
||
``` | ||
|
||
## Data consistency | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* This file is part of the ArrayQuery package. | ||
* | ||
* (c) Mauro Cassani<https://github.com/mauretto78> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ArrayQuery\Exceptions; | ||
|
||
class NotExistingElementException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -681,6 +681,98 @@ public function it_should_get_the_first_result_from_a_query() | |
} | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \ArrayQuery\Exceptions\NotConsistentDataException | ||
* @expectedExceptionMessage Element provided has no consistent data. | ||
*/ | ||
public function it_throws_NotConsistentDataException_if_an_element_with_not_consistent_data_is_provided() | ||
{ | ||
foreach ($this->usersArrays as $array) { | ||
$qb = QueryBuilder::create($array); | ||
$element = [ | ||
'id' => 234, | ||
'name' => 'Clementine Bauch 23', | ||
'username' => 'Samantha', | ||
'email' => '[email protected]', | ||
]; | ||
|
||
$qb->addElement($element, 234); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_should_add_an_element_to_array() | ||
{ | ||
foreach ($this->usersArrays as $array) { | ||
$qb = QueryBuilder::create($array); | ||
$element = [ | ||
'id' => 23, | ||
'name' => 'Clementine Bauch 23', | ||
'username' => 'Samantha', | ||
'email' => '[email protected]', | ||
'address' => [ | ||
'street' => 'Douglas Extension', | ||
'suite' => 'Suite 847', | ||
'city' => 'McKenziehaven', | ||
'zipcode' => '59590-4157', | ||
'geo' => [ | ||
'lat' => '-68.6102', | ||
'lng' => '-47.0653' | ||
] | ||
], | ||
'phone' => '1-463-123-4447', | ||
'website' => 'ramiro.info', | ||
'company' => [ | ||
'name' => 'Romaguera-Jacobson', | ||
'catchPhrase' => 'Face to face bifurcated interface', | ||
'bs' => 'e-enable strategic applications' | ||
], | ||
'registration_date' => '01/05/2017', | ||
'update_date' => '2017-05-30', | ||
'tags' => [ | ||
'apple', | ||
'pinapple', | ||
'pear', | ||
'apple-pie' | ||
] | ||
]; | ||
|
||
$qb->addElement($element, 23); | ||
|
||
$this->assertCount(11, $qb->getResults()); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \ArrayQuery\Exceptions\NotExistingElementException | ||
* @expectedExceptionMessage Element with key 232 does not exists. | ||
*/ | ||
public function it_throws_NotConsistentDataEfffxception_if_an_wrong_key_element_is_provided() | ||
{ | ||
foreach ($this->usersArrays as $array) { | ||
$qb = QueryBuilder::create($array); | ||
$qb->removeElement(232); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_should_remove_an_element_to_array() | ||
{ | ||
foreach ($this->usersArrays as $array) { | ||
$qb = QueryBuilder::create($array); | ||
$qb->removeElement(2); | ||
$qb->removeElement(3); | ||
|
||
$this->assertCount(8, $qb->getResults()); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
|