Created
November 15, 2012 08:06
-
-
Save ikr/4077316 to your computer and use it in GitHub Desktop.
Splitting the tests at http://blog.xiag.ru/2012/11/blog-post_15.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function test_event_repetition_isnt_allowed() { | |
$el = new EventLimiter; | |
$el->confirmAllowed("sms"); // Заметь, без ассёрта! Уже проверяли. | |
$this->assertFalse($el->confirmAllowed("sms")); | |
} | |
//========== | |
class EventLimiter { | |
private $count = 0; | |
public function confirmAllowed($event) { | |
$this->count++; | |
return ($this->count < 1); | |
} | |
} | |
//затем | |
public function test_two_different_events_in_a_row_are_allowed() { | |
$el = new EventLimiter; | |
$el->confirmAllowed("sms1"); | |
$this->assertTrue($el->confirmAllowed("sms2")); | |
} | |
//========== | |
class EventLimiter { | |
private $counts = []; | |
public function confirmAllowed($event) { | |
if (!isset($this->counts[$event])) { | |
$this->counts[$event] = 0; | |
} // дописал сверху этот if | |
// А потом *эффектно* поменял count на counts | |
$this->counts[$event]++; | |
return ($this->counts[$event] < 1); | |
} | |
} | |
//тепепь можно добавить в конструктор лимит | |
public function test_events_repetionion_limit_is_set_on_construction() { | |
$el = new EventLimiter(2); | |
$el->confirmAllowed("sms"); | |
$this->assertTrue($el->confirmAllowed("sms")); | |
$this->assertFalse($el->confirmAllowed("sms")); | |
} | |
//========== | |
class EventLimiter { | |
private $limit; | |
private $counts = []; | |
public function __construct($limit) { | |
$this->limit = $limit ?: 1; | |
} | |
public function confirmAllowed($event) { | |
if (!isset($this->counts[$event])) { | |
$this->counts[$event] = 0; | |
} | |
$this->counts[$event]++; | |
return ($this->counts[$event] < $this->limit); | |
// опять же *эффектно* меняем 1 на $this->limit | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment