Skip to content

Commit

Permalink
Where tests complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Compolomus committed May 8, 2018
1 parent 3519717 commit e772c90
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
13 changes: 7 additions & 6 deletions src/System/Conditions.php
Expand Up @@ -50,11 +50,12 @@ public function add(string $field, string $condition, $value): void

public function type(string $condition, $value): string
{
return \in_array($condition, ['in', 'not in'], true)
? '(' . $this->concat($value) . ')'
: (\in_array($condition, ['between', 'not between'], true)
? implode(' AND ', $value)
: $value
);
return (!\in_array($condition, ['in', 'not in'], true)
? (!\in_array($condition, ['between', 'not between'], true)
? $value
: implode(' AND ', $value)
)
: '(' . $this->concat($value) . ')'
);
}
}
2 changes: 1 addition & 1 deletion src/System/Placeholders.php
Expand Up @@ -11,7 +11,7 @@ public function __construct(array $placeholders = []) // reset
$this->placeholders = $placeholders;
}

public function set(string $key, $value)
public function set(string $key, $value): void
{
$this->placeholders[':' . $key] = $value;
}
Expand Down
3 changes: 1 addition & 2 deletions src/System/Traits/Caller.php
Expand Up @@ -19,10 +19,9 @@ public function setBase($base): void

public function __call(string $method, $args)
{
if (!method_exists(__CLASS__, $method) && method_exists($this->base, $method)) {
if (!method_exists(__CLASS__, $method)) {
return $this->base->$method(...$args);
}
throw new BuilderException('Undefined method' . $method . ' |Caller call|');
}

public function __toString()
Expand Down
57 changes: 51 additions & 6 deletions tests/BuilderTest.php
Expand Up @@ -5,7 +5,10 @@
use Compolomus\LSQLQueryBuilder\Builder;
use Compolomus\LSQLQueryBuilder\BuilderException;
use Compolomus\LSQLQueryBuilder\BuilderFactory;
use Compolomus\LSQLQueryBuilder\Parts\Where;
use Compolomus\LSQLQueryBuilder\System\Conditions;
use Compolomus\LSQLQueryBuilder\System\Fields;
use Compolomus\LSQLQueryBuilder\System\Placeholders;
use PHPUnit\Framework\TestCase;

class BuilderTest extends TestCase
Expand Down Expand Up @@ -82,12 +85,6 @@ public function testSelectException(): void
(new Builder('table'))->select(['dummy'])->setFunction('notDummy', 'count');
}

public function testCallerException(): void
{
$this->expectException(BuilderException::class);
(new Builder('table'))->select(['dummy'])->setFun('notDummy', 'count');
}

public function testHelperConcat(): void
{
$testArray = [1, 2, 3];
Expand Down Expand Up @@ -126,6 +123,54 @@ public function testHelperUid(): void
{
$this->assertNotFalse(preg_match('/^[a-f0-9]{10}DUMMY$/i', (new Builder('dummy'))->uid('dummy')));
}

public function testPlaceholders(): void
{
$placeholders = new Placeholders();
$this->assertEquals($placeholders->get(), []);
$placeholders->set('dummy', 'dummy');
$placeholders->set('dummy1', 'dummy1');
$this->assertEquals($placeholders->get(), [':dummy' => 'dummy', ':dummy1' => 'dummy1']);
}

public function testConditions(): void
{
$conditions = new Conditions();
$this->assertEquals($conditions->conditions(), []);
$conditions->add('dummy1', '<', 123);
$conditions->add('dummy2', 'between', [1, 2]);
$conditions->add('dummy3', 'not in', [3, 4, 5]);
$conditions->add('dummy4', '>', 456);
$this->assertCount(4, $conditions->conditions());
}

public function testConditionsException(): void
{
$conditions = new Conditions();
$this->expectException(BuilderException::class);
$conditions->add('dummy', 'qwerty', 1);
}

public function testWhere(): void
{
$select = (new Builder('dummy'))
->select()
->where([['id', '=', 15], ['firm_id', 'not in', [1, 2, 3]]])
->add('dummy', '<', 123)
->where([['age', '>', 17], ['friends', '>=', 177]], 'or');
$this->assertNotFalse(
preg_match('/^SELECT * FROM `dummy` WHERE (`id` = \:[a-f0-9]{10}W AND `firm_id` NOT IN \:[a-f0-9]{10}W AND `dummy` <= \:[a-f0-9]{10}W) AND (`age` > \:[a-f0-9]{10}W OR `friends` >= \:[a-f0-9]{10}W)$/i',
$select->result()
)
);
$this->assertCount(5, $select->placeholders());
}

public function testWhereException(): void
{
$this->expectException(BuilderException::class);
new Where([], 'dummy');
}
//
// public function testInsert()
// {
Expand Down

0 comments on commit e772c90

Please sign in to comment.