Created
April 23, 2014 23:51
-
-
Save MontealegreLuis/11236559 to your computer and use it in GitHub Desktop.
PHPUnit and phpspec examples
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 | |
namespace spec\Money; | |
use PhpSpec\ObjectBehavior; | |
use Money\Currency; | |
use Money\Money; | |
class MoneySpec extends ObjectBehavior | |
{ | |
public function it_should_be_initializable(Currency $currency) | |
{ | |
$this->beConstructedWith(100, $currency); | |
$this->shouldHaveType('Money\Money'); | |
} | |
// testDifferentCurrenciesCannotBeAdded | |
public function it_should_throw_exception_when_adding_amounts_with_different_currencies() | |
{ | |
$m2 = new Money(100, new Currency('USD')); | |
$this->beConstructedWith(100, new Currency('EUR')); | |
$this->shouldThrow('Money\InvalidArgumentException')->during('add', [$m2]); | |
} | |
// testSubtraction | |
public function it_should_subtract_amounts_with_the_same_currency() | |
{ | |
$m2 = new Money(200, new Currency('EUR')); | |
$this->beConstructedWith(100, new Currency('EUR')); | |
$diff = $this->subtract($m2); | |
$diff->getAmount()->shouldBeEqualTo(-100); | |
$diff->getCurrency()->shouldBeLike(new Currency('EUR')); | |
$this->shouldNotBeEqualTo($m2); | |
$this->shouldNotBeEqualTo($diff); | |
} | |
//testComparison | |
public function it_should_be_able_to_compare_amounts_with_the_same_currency() | |
{ | |
$this->beConstructedWith(1, new Currency('EUR')); | |
$euro2 = new Money(2, new Currency('EUR')); | |
$this->greaterThan($euro2)->shouldReturn(false); | |
$this->lessThan($euro2)->shouldReturn(true); | |
$this->compare($euro2)->shouldReturn(-1); | |
$this->compare($this)->shouldreturn(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 | |
/** | |
* This file is part of the Money library | |
* | |
* Copyright (c) 2011-2013 Mathias Verraes | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Money\Tests; | |
use PHPUnit_Framework_TestCase; | |
use Money\Money; | |
use Money\Currency; | |
class MoneyTest extends PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @expectedException Money\InvalidArgumentException | |
*/ | |
public function testDifferentCurrenciesCannotBeAdded() | |
{ | |
$m1 = new Money(100, new Currency('EUR')); | |
$m2 = new Money(100, new Currency('USD')); | |
$m1->add($m2); | |
} | |
public function testSubtraction() | |
{ | |
$m1 = new Money(100, new Currency('EUR')); | |
$m2 = new Money(200, new Currency('EUR')); | |
$diff = $m1->subtract($m2); | |
$expected = new Money(-100, new Currency('EUR')); | |
$this->assertEquals($expected, $diff); | |
// Should return a new instance | |
$this->assertNotSame($diff, $m1); | |
$this->assertNotSame($diff, $m2); | |
} | |
public function testComparison() | |
{ | |
$euro1 = new Money(1, new Currency('EUR')); | |
$euro2 = new Money(2, new Currency('EUR')); | |
$usd = new Money(1, new Currency('USD')); | |
$this->assertTrue($euro2->greaterThan($euro1)); | |
$this->assertFalse($euro1->greaterThan($euro2)); | |
$this->assertTrue($euro1->lessThan($euro2)); | |
$this->assertFalse($euro2->lessThan($euro1)); | |
$this->assertEquals(-1, $euro1->compare($euro2)); | |
$this->assertEquals(1, $euro2->compare($euro1)); | |
$this->assertEquals(0, $euro1->compare($euro1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment