Last active
October 5, 2017 21:14
-
-
Save Grummfy/46380212899acd0f64ac96df1128bec0 to your computer and use it in GitHub Desktop.
2017-10-05 PHP Leuven meetup - atoum
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
<?php | |
use mageekguy\atoum\reports; | |
use mageekguy\atoum\reports\coverage; | |
use mageekguy\atoum\writers\std; | |
use mageekguy\atoum\report\fields\runner\result\logo; | |
// telemetry | |
use mageekguy\atoum\telemetry; | |
// | |
// config of tests | |
// | |
// branch coverage | |
$script->enableBranchAndPathCoverage(); // comment it if you got some segfault on code coverage | |
$runner->addTestsFromDirectory(__DIR__ . '/tests/UnitS'); | |
// | |
// Reports | |
// | |
$report = $script->addDefaultReport(); | |
$extension = new reports\extension($script); | |
$extension->addToRunner($runner); | |
// html report | |
$coverage = new coverage\html(); | |
$coverage->addWriter(new std\out()); | |
$coverage->setOutPutDirectory(__DIR__ . '/tests/reports/unit/'); | |
$runner->addReport($coverage); | |
$report->addField(new logo()); | |
// telemetry | |
$telemetry = new telemetry\report(); | |
$telemetry->addWriter(new std\out()); | |
$runner->addReport($telemetry); | |
// $telemetry->readProjectNameFromComposerJson(__DIR__ . '/composer.json'); | |
// $telemetry->sendAnonymousProjectName(); | |
$telemetry->setProjectName('grummfy/demo-php-leuven-meetup-2017-10-05'); |
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
php vendor/bin/atoum -ncc -f tests/test.php --methods *::testIdenticalVsEqual | |
php vendor/bin/atoum -ncc -f tests/test.php --methods *::testAsserterTypes | |
php vendor/bin/atoum -ncc -f tests/test.php --methods *::testPrecision | |
php vendor/bin/atoum -ncc -f tests/test.php | |
php vendor/bin/atoum -f tests/test.php |
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
{ | |
"require-dev": { | |
"atoum/telemetry-extension": "^1.0", | |
"atoum/reports-extension": "^3.0" | |
} | |
} |
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
<?php | |
// tests/UnitS/demoTest.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(''); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment