Last Updated: August 15, 2019
·
2.261K
· neochrome

Introducing Physalis, simple BDD for PHP

Coming back to [PHP][] after working for quite a while in JavaScript and letting [Jasmine][] taking care of my testing needs, I sorely missed the ease with how you write those specifications and dreaded having to extend some base test case class.

So I figured I'd try to do something about it and managed to get something that almost looked like those familiar [Jasmine][] specs:

<?php
require_once 'calc.php';

describe('calculator', function () {
  $calc;
  beforeEach(function () use (&$calc) {
    $calc = new Calculator();
  });

  describe('adds', function () use (&$calc) {
    it('positive numbers', function () use (&$calc) {
      expect($calc->add(1, 2))->toBe(3);
    });

    it('negative numbers', function () use (&$calc) {
      expect($calc->add(-4, -5))->toBe(-9);
    });

    it('with zero', function () use (&$calc) {
      expect($calc->add(0, 10))->toBe(10);
    });
  });

  describe('divides', function () use (&$calc) {
    it('positive numbers', function () use (&$calc) {
      expect($calc->div(8, 2))->toBe(4);
    });

    it('throws on division by zero', function () use (&$calc) {
      expect(function () use (&$calc) { $calc->div(1, 0); })->toThrow();
    });
  });
});
?>

If you're interested, take a look at the [project][] over at [GitHub][] and feel free to drop me a note if you find it useful.

[PHP]: http://www.php.net
[Jasmine]: http://pivotal.github.io/jasmine
[project]: https://github.com/neochrome/physalis
[GitHub]: https://github.com/

2 Responses
Add your response

Look interesting :)

over 1 year ago ·

ETT RAMVERK! GOGOGO!

over 1 year ago ·