Skip to content

Commit

Permalink
Merge pull request #33 from kschroeder/develop
Browse files Browse the repository at this point in the history
Built an ugly unit test for crud workings
  • Loading branch information
kschroeder committed Feb 18, 2017
2 parents 6e52cc1 + 0f71e5b commit 15d8961
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
48 changes: 45 additions & 3 deletions lib/Config/Storage/RelationalDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Zend\Db\Sql\Ddl\Column\Varchar;
use Zend\Db\Sql\Ddl\Constraint\UniqueKey;
use Zend\Db\Sql\Ddl\CreateTable;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Sql;

class RelationalDatabase implements StorageInterface
Expand Down Expand Up @@ -110,16 +111,57 @@ public function setValue($path, $value, $requestedContext = Config::CONTEXT_DEFA
if (!in_array($requestedContext, $contexts)) {
throw new InvalidContextException('Could not find the context: ' . $requestedContext);
}

$sql = new Sql($this->adapter);

/*
* We can't do an ON DUPLICATE UPDATE because not all adapters support that. So we need to do a select
* followed by an insert or update
*/

if ($this->exists($sql, $path, $requestedContext)) {
$this->doUpdate($sql, $path, $value, $requestedContext);
} else {
$this->doInsert($sql, $path, $value, $requestedContext);
}

$this->data[$requestedContext][$path] = $value;
}

protected function exists(Sql $sql, $path, $context)
{
$select = $sql->select(self::TABLE);
$select->columns(['cnt' => new Expression('COUNT(*)')]);
$select->where(['path' => $path, 'context' => $context]);
$select = $select->getSqlString($this->adapter->getPlatform());

$result = $this->adapter->query($select)->execute();
$check = $result->current();
return $check && $check['cnt'] > 0;
}

protected function doInsert(Sql $sql, $path, $value, $context)
{
$insert = $sql->insert(self::TABLE);
$insert->values([
'path' => $path,
'value' => $value,
'context' => $requestedContext
'context' => $context
]);
$insert = $insert->getSqlString($this->adapter->getPlatform());
$this->adapter->query($insert);
$this->data[$requestedContext][$path] = $value;

$this->adapter->query($insert)->execute();
}

protected function doUpdate(Sql $sql, $path, $value, $context)
{
$update = $sql->update(self::TABLE);
$update->set([
'value' => $value
])->where(['path' => $path, 'context' => $context]);

$update = $update->getSqlString($this->adapter->getPlatform());
$this->adapter->query($update)->execute();
}

public function create()
Expand Down
6 changes: 5 additions & 1 deletion lib/Console/Command/ConfigurationSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function configure()
;

$this->addArgument('path', InputArgument::REQUIRED, 'Configuration Path');
$this->addArgument('value', InputArgument::REQUIRED, 'Value');
$this->addArgument('value', InputArgument::OPTIONAL, 'Value');
$this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', Config::CONTEXT_DEFAULT);
}

Expand Down Expand Up @@ -55,6 +55,10 @@ protected function execute(InputInterface $input, OutputInterface $output)

$builderFactory->getPersistence()->setValue($path, $value, $context);

if (!$value) {
$value = '<empty>';
}

$out = sprintf("Set %s to %s (context: %s)", $path, $value, $context);

$output->writeln($out);
Expand Down
48 changes: 48 additions & 0 deletions tests/Storage/CrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Magium\Configuration\File\Context\XmlFile;
use PHPUnit\Framework\TestCase;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Platform\Sqlite;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Sql;

Expand All @@ -32,6 +35,51 @@ public function testRelationalCreateOnInvalidContextWithoutConfigThrowsException
$db->setValue('path', 'value', 'boogers');
}

public function testMakeSureUpdateAndInsertAreCalledAppropriately()
{
$adapter = $this->getMockBuilder(Adapter::class)->setConstructorArgs(
[['driver' => 'pdo_sqlite', 'database' => ':memory:']]
)->getMock();
$adapter->expects(self::any())->method('getPlatform')->willReturn(
new Sqlite(new \PDO('sqlite::memory:'))
);
$me = $this;
$adapter->expects(self::exactly(4))->method('query')->willReturnCallback(function($param) use ($me) {
static $state = -1;
$state++;
$result = $me->createMock(ResultInterface::class);
switch ($state) {
case 0:
TestCase::assertContains('SELECT', $param);
$result->expects(self::once())->method('current')->willReturn(['cnt' => 0]);
$mock = $me->createMock(StatementInterface::class);
$mock->expects(TestCase::once())->method('execute')->willReturn($result);
return $mock;
case 1:
TestCase::assertContains('INSERT', $param);
$mock = $me->createMock(StatementInterface::class);
$mock->expects(TestCase::once())->method('execute');
return $mock;
case 2:
TestCase::assertContains('SELECT', $param);
$result->expects(self::once())->method('current')->willReturn(['cnt' => 1]);
$mock = $me->createMock(StatementInterface::class);
$mock->expects(TestCase::once())->method('execute')->willReturn($result);
return $mock;
case 3:
TestCase::assertContains('UPDATE', $param);
$mock = $me->createMock(StatementInterface::class);
$mock->expects(TestCase::once())->method('execute');
return $mock;
}
});

$relational = new RelationalDatabase($adapter);
$relational->setValue('path', 'value', Config::CONTEXT_DEFAULT);
$relational->setValue('path', 'value', Config::CONTEXT_DEFAULT);
}


public function testNullReturnedForNonExistentPath()
{
$db = $this->createDatabase();
Expand Down

0 comments on commit 15d8961

Please sign in to comment.