Skip to content

Instantly share code, notes, and snippets.

@ikr
Created November 15, 2012 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikr/4077316 to your computer and use it in GitHub Desktop.
Save ikr/4077316 to your computer and use it in GitHub Desktop.
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