Skip to content

Instantly share code, notes, and snippets.

@Grummfy
Last active December 17, 2017 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grummfy/d4a51c206488ea984d9889957e44a079 to your computer and use it in GitHub Desktop.
Save Grummfy/d4a51c206488ea984d9889957e44a079 to your computer and use it in GitHub Desktop.
PHPAntwerp meetup 2017-12-20
<?php
// .atoum.php
// $script->addDefaultArguments(
// '--loop'
// );
{
"require-dev": {
"atoum/reports-extension": "^3.0",
"atoum/telemetry-extension": "^1.0",
"atoum/atoum": "^3.2"
}
}
php vendor/bin/atoum -ncc -f tests/UnitS/Foo.php --methods *::testIdenticalVsEqual
php vendor/bin/atoum -ncc -f tests/UnitS/Foo.php --methods *::testAsserterTypes
php vendor/bin/atoum -ncc -f tests/UnitS/Foo.php --methods *::testPrecision
php vendor/bin/atoum -ncc -f tests/UnitS/Foo.php
php vendor/bin/atoum -f tests/UnitS/Foo.php
<?php
// parent-directory/tests/UnitS/Foo.php
namespace Foo\Bar {
class Number {
public function __construct($number) {
$this->number = $number;
}
}
class Foo {
public function baz(float $bar) {
return 1.1 + $bar;
}
public function unwrap(Number $number) {
return $number->number;
}
public function withIf(bool $a, bool $b) {
if ($a) {
echo 'a';
} elseif ($b) {
echo 'b';
}
if ($a && $b) {
echo 'ab';
}
}
}
}
namespace test\UnIts\Foo\Bar {
class Foo extends \atoum {
public function testIdenticalVsEqual() {
$this->newTestedInstance;
$number = new \Foo\Bar\Number(42);
$this
->assert('basic value demonstration')
->variable($this->testedInstance->unwrap($number))
->isEqualTo($number->number)
->isIdenticalTo($number->number);
$this
->assert('basic value demonstration, with new instance')
->variable($this->testedInstance->unwrap(new \Foo\Bar\Number(42)))
->isEqualTo($number->number)
->isIdenticalTo($number->number);
$number = new \Foo\Bar\Number($number);
$this
->assert('object comparison, same instance')
->variable($this->testedInstance->unwrap($number))
->isEqualTo($number->number)
->isIdenticalTo($number->number);
$this
->assert('object comparison, with new instances')
->variable($this->testedInstance->unwrap(new \Foo\Bar\Number(new \Foo\Bar\Number(42))))
->isEqualTo($number->number)
//->isIdenticalTo($number->number, 'That\'s a different instance');
->isNotIdenticalTo($number->number, 'That\'s a different instance');
}
public function testAsserterTypes() {
$this->newTestedInstance;
$number = new \Foo\Bar\Number(42);
$this
->assert('using variable (generic)')
->variable($this->testedInstance->unwrap($number))
->isEqualTo($number->number)
->isIdenticalTo($number->number);
// 2 assertions
$this
->assert('using a type')
->integer($this->testedInstance->unwrap($number))
->isEqualTo($number->number)
->isIdenticalTo($number->number);
// 3 assertions
}
public function testPrecision() {
$this->newTestedInstance;
$this
->float($this->testedInstance->baz(40.9))
->isNotIdenticalTo(42)
->isEqualTo(42)
->isNearlyEqualTo(41.8, 0.01);
}
public function testAB() {
$this->newTestedInstance;
$this->output(function(){$this->testedInstance->withIf(true, false);})->isEqualTo('a');
//$this->output(function(){$this->testedInstance->withIf(true, true);})->isEqualTo('aab');
//$this->output(function(){$this->testedInstance->withIf(false, true);})->isEqualTo('b');
//$this->output(function(){$this->testedInstance->withIf(false, false);})->isEqualTo('');
}
public function testMock() {
$m = new \mock\Countable();
$this->calling($m)->count = 1;
$this->calling($m)->count[2]->throw = new \DomainException('You can\'t count me');
$this->integer(count($m))->isEqualTo(1);
$this->exception(function() use($m) {$this->integer(count($m));})->isInstanceOf(\DomainException::class);
$this->integer(count($m))->isEqualTo(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment