Advertisement
Guest User

PHPCode for stackoverflow question

a guest
Jul 10th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. <?php
  2. class SignUpController {
  3.   public function __construct($validator) {
  4.     $this->validator = $validator;
  5.   }
  6.  
  7.   public function openUserAccount($email, $password) {
  8.     if ( ! $this->validator->validate($email)) throw new Exception("Email is invalid");
  9.     //do registration
  10.     return true;
  11.   }
  12. }
  13.  
  14. class Validator {
  15.   public function validate($email) {
  16.     if (preg_match("/^[^\.]+@.+$/", $email)) return true; //this is first requirement
  17.  
  18.     //this true after requirement changed but support I forgot to made a change to this class
  19.     //because it written by someone else and I don't know it exist
  20.     // TODO: you can uncomment this line to make requirement met
  21.     // if (preg_match("/.+@.+/", $email)) return true;
  22.  
  23.     return false;
  24.   }
  25. }
  26.  
  27. class AllTest extends PHPUnit_Framework_TestCase {
  28.  
  29.   /**
  30.   * @expectedException Exception
  31.   */
  32.   public function test_openUserAccount_not_allow_period_in_email() {
  33.     $mock = $this->getMock('Validator');
  34.     $mock->expects($this->once())
  35.          ->method('validate')
  36.        ->with('my.name@example.com')
  37.        ->will($this->returnValue(false));
  38.  
  39.     $ctrl = new SignUpController($mock);
  40.     $ctrl->openUserAccount('my.name@example.com', 'password');
  41.   }
  42.  
  43.   public function test_openUserAccount_return_true_with_valid_email() {
  44.     $mock = $this->getMock('Validator');
  45.     $mock->expects($this->once())
  46.          ->method('validate')
  47.        ->with('valid@example.com')
  48.        ->will($this->returnValue(true));
  49.  
  50.     $ctrl = new SignUpController($mock);
  51.     $this->assertSame(true, $ctrl->openUserAccount('valid@example.com', 'password'));
  52.   }
  53.  
  54.   //There are much more test for openUserAccount that handle every edge case that might happen
  55.   //All test continued more than 500 line that make test hard to find what missing else
  56.   //Sometimes other developer mock class in other file, somewhere else that I don't known
  57.   //and we're almost forgot it because it's a long time ago
  58.  
  59.   // // TODO: uncomment below test to see all test still pass
  60.   // // after requirement changed, I add new test then all test still pass
  61.   // // without any modification to another test
  62.   // public function test_openUserAccount_return_true_with_period_in_email() {
  63.   //   $mock = $this->getMock('Validator');
  64.   //   $mock->expects($this->once())
  65.   //        ->method('validate')
  66.   //      ->with('my.name@example.com')
  67.   //      ->will($this->returnValue(true));
  68.  
  69.   //   $ctrl = new SignUpController($mock);
  70.   //   $this->assertSame(true, $ctrl->openUserAccount('my.name@example.com', 'password'));
  71.   // }
  72.  
  73.   // I need to combine Validator test to this class to make phpunit run all test in same file
  74.   public function test_validate_with_period_return_false() {
  75.     $inst = new Validator();
  76.     $this->assertSame(false, $inst->validate('with.period@example.com'));
  77.   }
  78.  
  79.   public function test_validate_without_period_return_true() {
  80.     $inst = new Validator();
  81.     $this->assertSame(true, $inst->validate('withoutperiod@example.com'));
  82.   }
  83.  
  84.   // //This is a test that support new requirement of validator
  85.   // //even i add this test and remove test_validate_with_period_return_false
  86.   // //how about mock of Validator in test above? how do I known where is using this class
  87.   // //Mock don't reflect error after this method changed
  88.   // public function test_validate_after_requirement_changed_period_must_allow() {
  89.   //   $inst = new Validator();
  90.   //   $this->assertSame(true, $inst->validate('with.period@example.com'));
  91.   // }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement