Lens

Object-oriented

Imagine that we’re testing a class from your project that looks like this:

src/Math.php
<?php namespace Example; use RangeException; class Math { public function add($m, $n) { return $m + $n; } public function divide($m, $n) { if ($n === 0) { throw new RangeException(); } return $m / $n; } }

Let’s write the unit tests:

lens/tests/Math.php
<?php namespace Example; use RangeException; // Test $math = new Math(); $c = $math->add(1, 1); // Output $c = 2; // Test $math = new Math(); $c = $math->divide($a, $b); // Input $a = 1; $b = 2; // Output $c = 0.5; // Input $a = 1; $b = 0; // Output throw new RangeException();

Download this example

Now run the Lens executable to see the results:

lens # Passed tests: 3

Congratulations! You can test object-oriented code!

Now let’s see what happens when we need a mock.