Skip to content

Commit

Permalink
Merge pull request #2 from jubianchi/constant_mocker
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
Hywan committed Nov 24, 2015
2 parents c744530 + 185aa36 commit 2169cae
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 8 deletions.
24 changes: 16 additions & 8 deletions classes/php/mocker/constant.php
Expand Up @@ -7,26 +7,34 @@

class constant extends mocker
{
public function __get($constantName)
public function __get($name)
{
return constant($this->getDefaultNamespace() . $constantName);
if ($this->__isset($name) === false)
{
throw new exceptions\constant('Constant \'' . $name . '\' is not defined in namespace \'' . trim($this->getDefaultNamespace(), '\\') . '\'');
}

return $this->getAdapter()->constant($this->getDefaultNamespace() . $name);
}

public function __set($constantName, $value)
public function __set($name, $value)
{
define($this->getDefaultNamespace() . $constantName, $value);
if (@$this->getAdapter()->define($this->getDefaultNamespace() . $name, $value) === false)
{
throw new exceptions\constant('Could not mock constant \'' . $name . '\' in namespace \'' . trim($this->getDefaultNamespace(), '\\') . '\'');
}

return $this;
}

public function __isset($constantName)
public function __isset($name)
{
return defined($this->getDefaultNamespace() . $constantName);
return $this->getAdapter()->defined($this->getDefaultNamespace() . $name);
}

public function __unset($constantName)
public function __unset($name)
{
return;
throw new exceptions\constant('Could not unset constant \'' . $name . '\' in namespace \'' . trim($this->getDefaultNamespace(), '\\') . '\'');
}

function addToTest(atoum\test $test)
Expand Down
7 changes: 7 additions & 0 deletions classes/php/mocker/exception.php
@@ -0,0 +1,7 @@
<?php

namespace mageekguy\atoum\php\mocker;

use mageekguy\atoum;

class exception extends \runtimeException implements atoum\exception {}
7 changes: 7 additions & 0 deletions classes/php/mocker/exceptions/constant.php
@@ -0,0 +1,7 @@
<?php

namespace mageekguy\atoum\php\mocker\exceptions;

use mageekguy\atoum\php\mocker;

class constant extends mocker\exception {}
124 changes: 124 additions & 0 deletions tests/units/classes/php/mocker/constant.php
@@ -0,0 +1,124 @@
<?php

namespace mageekguy\atoum\tests\units\php\mocker;

require_once __DIR__ . '/../../../runner.php';

use
mageekguy\atoum,
mageekguy\atoum\php
;

class constant extends atoum\test
{
public function test__set()
{
$this
->given(
$this->newTestedInstance,
$adapter = new atoum\test\adapter(),
php\mocker::setAdapter($adapter)
)

->if(
$adapter->define = true,
$this->testedInstance->setDefaultNameSpace($namespace = uniqid())
)
->then
->string($this->testedInstance->{$constant = uniqid()} = $value = uniqid())->isEqualTo($value)
->adapter($adapter)
->call('define')->withArguments($namespace . '\\' . $constant, $value)->once

->if($adapter->define = false)
->then
->exception(function(atoum\test $test) use (& $constant, & $value) {
$test->testedInstance->{$constant = uniqid('a')} = $value = uniqid();
}
)
->isInstanceOf('mageekguy\atoum\php\mocker\exceptions\constant')
->hasMessage('Could not mock constant \'' . $constant . '\' in namespace \'' . $namespace . '\'')
->adapter($adapter)
->call('define')->withArguments($namespace . '\\' . $constant, $value)->once
;
}

public function test__get()
{
$this
->given(
$this->newTestedInstance,
$adapter = new atoum\test\adapter(),
php\mocker::setAdapter($adapter)
)

->if(
$adapter->defined = false,
$this->testedInstance->setDefaultNameSpace($namespace = uniqid())
)
->then
->exception(function(atoum\test $test) use (& $constant, & $value) {
$test->testedInstance->{$constant = uniqid()};
}
)
->isInstanceOf('mageekguy\atoum\php\mocker\exceptions\constant')
->hasMessage('Constant \'' . $constant . '\' is not defined in namespace \'' . $namespace . '\'')
->adapter($adapter)
->call('defined')->withArguments($namespace . '\\' . $constant)->once

->if(
$adapter->defined = true,
$adapter->constant = $value = uniqid()
)
->then
->string($this->testedInstance->{$constant = uniqid()})->isEqualTo($value)
->adapter($adapter)
->call('defined')->withArguments($namespace . '\\' . $constant)->once
->call('constant')->withArguments($namespace . '\\' . $constant)->once
;
}

public function test__isset()
{
$this
->given(
$this->newTestedInstance,
$adapter = new atoum\test\adapter(),
php\mocker::setAdapter($adapter)
)

->if(
$adapter->defined = false,
$this->testedInstance->setDefaultNameSpace($namespace = uniqid())
)
->then
->boolean(isset($this->testedInstance->{$constant = uniqid()}))->isFalse
->adapter($adapter)
->call('defined')->withArguments($namespace . '\\' . $constant)->once

->if($adapter->defined = true)
->then
->boolean(isset($this->testedInstance->{$constant = uniqid()}))->isTrue
->adapter($adapter)
->call('defined')->withArguments($namespace . '\\' . $constant)->once
;
}

public function test__unset()
{
$this
->given(
$this->newTestedInstance,
$adapter = new atoum\test\adapter()
)

->if($this->testedInstance->setDefaultNameSpace($namespace = uniqid()))
->then
->exception(function(atoum\test $test) use (& $constant, & $value) {
unset($test->testedInstance->{$constant = uniqid()});
}
)
->isInstanceOf('mageekguy\atoum\php\mocker\exceptions\constant')
->hasMessage('Could not unset constant \'' . $constant . '\' in namespace \'' . $namespace . '\'')
;
}
}

0 comments on commit 2169cae

Please sign in to comment.