Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class SignUpController {
- public function __construct($validator) {
- $this->validator = $validator;
- }
- public function openUserAccount($email, $password) {
- if ( ! $this->validator->validate($email)) throw new Exception("Email is invalid");
- //do registration
- return true;
- }
- }
- class Validator {
- public function validate($email) {
- if (preg_match("/^[^\.]+@.+$/", $email)) return true; //this is first requirement
- //this true after requirement changed but support I forgot to made a change to this class
- //because it written by someone else and I don't know it exist
- // TODO: you can uncomment this line to make requirement met
- // if (preg_match("/.+@.+/", $email)) return true;
- return false;
- }
- }
- class AllTest extends PHPUnit_Framework_TestCase {
- /**
- * @expectedException Exception
- */
- public function test_openUserAccount_not_allow_period_in_email() {
- $mock = $this->getMock('Validator');
- $mock->expects($this->once())
- ->method('validate')
- ->with('my.name@example.com')
- ->will($this->returnValue(false));
- $ctrl = new SignUpController($mock);
- $ctrl->openUserAccount('my.name@example.com', 'password');
- }
- public function test_openUserAccount_return_true_with_valid_email() {
- $mock = $this->getMock('Validator');
- $mock->expects($this->once())
- ->method('validate')
- ->with('valid@example.com')
- ->will($this->returnValue(true));
- $ctrl = new SignUpController($mock);
- $this->assertSame(true, $ctrl->openUserAccount('valid@example.com', 'password'));
- }
- //There are much more test for openUserAccount that handle every edge case that might happen
- //All test continued more than 500 line that make test hard to find what missing else
- //Sometimes other developer mock class in other file, somewhere else that I don't known
- //and we're almost forgot it because it's a long time ago
- // // TODO: uncomment below test to see all test still pass
- // // after requirement changed, I add new test then all test still pass
- // // without any modification to another test
- // public function test_openUserAccount_return_true_with_period_in_email() {
- // $mock = $this->getMock('Validator');
- // $mock->expects($this->once())
- // ->method('validate')
- // ->with('my.name@example.com')
- // ->will($this->returnValue(true));
- // $ctrl = new SignUpController($mock);
- // $this->assertSame(true, $ctrl->openUserAccount('my.name@example.com', 'password'));
- // }
- // I need to combine Validator test to this class to make phpunit run all test in same file
- public function test_validate_with_period_return_false() {
- $inst = new Validator();
- $this->assertSame(false, $inst->validate('with.period@example.com'));
- }
- public function test_validate_without_period_return_true() {
- $inst = new Validator();
- $this->assertSame(true, $inst->validate('withoutperiod@example.com'));
- }
- // //This is a test that support new requirement of validator
- // //even i add this test and remove test_validate_with_period_return_false
- // //how about mock of Validator in test above? how do I known where is using this class
- // //Mock don't reflect error after this method changed
- // public function test_validate_after_requirement_changed_period_must_allow() {
- // $inst = new Validator();
- // $this->assertSame(true, $inst->validate('with.period@example.com'));
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement