Lens

Mocks

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

src/Greeter.php
<?php namespace Example; class Greeter { public function greet(Person $person) { $name = $person->getName(); $day = date('l', time()); return "Hello {$name}, it's {$day} today!"; } }
src/Person.php
<?php namespace Example; class Person { public function getName() { return null; } }

This looks like a real challenge because:

But! We are using Lens, so none of this is a problem for us.

We’ll simply list the external interactions that we’re expecting:

lens/tests/Greeter.php
<?php namespace Example; // Test $greeter = new Greeter(); $output = $greeter->greet($person); // Input $person = new Person(); // Output $person->getName(); // return 'Ann'; time(); // return 1518302651; $output = "Hello Ann, it's Saturday today!";

Did you see how we used a comment to provide the expected behavior for each function call? That’s all there is to it!

Download this example

Now run the Lens executable to see the results:

lens # Passed tests: 1

Congratulations! You’re able to mock built-in functions and external classes!

And that was the entire learning curve for Lens. Now you can test anything. All that’s left is to write more unit tests and watch your metrics climb.