Last active
March 26, 2020 17:16
-
-
Save pyrsmk/5f413fe8249933589ece82d98e618c4f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
return new class extends Sophie | |
{ | |
function mount() | |
{ | |
$this->author = fn() => new Author; | |
$this->date = fn() => new DateTime; | |
$this->body = fn() => random_text(); | |
$this->tags = fn() => [random_string(), random_string()]; | |
$this->subject = fn() => new Article( | |
$this->author, | |
$this->date, | |
$this->body, | |
$this->tags, | |
); | |
} | |
function unmount() { | |
Database::clear(); | |
} | |
function it_creates_a_valid_article() | |
{ | |
yield $this->subject->author => is_an_instance_of(Author::class); | |
yield $this->subject->date => is_an_instance_of(DateTime::class); | |
yield $this->subject->body => is_a_string(); | |
yield $this->subject->body => is_empty(); | |
yield $this->subject->tags => is_an_array(); | |
} | |
function it_saves_the_article_to_the_database() | |
{ | |
$this->subject; | |
yield Article::count() => equals(1); | |
} | |
function when_author_does_not_exist() | |
{ | |
$this->author = fn() => new NullAuthor; | |
it('should raise an error', function () { | |
yield fn() => $this->subject => throws(RecordNotFound::class); | |
}); | |
it('should not save the article', function () { | |
$this->subject; | |
yield Article::count() => equals(0); | |
}); | |
} | |
function when_there_is_no_tag() | |
{ | |
$this->tags = fn() => []; | |
it('should not register any tag', function () { | |
$this->subject; | |
yield Tag::count() => equals(0); | |
}); | |
} | |
function when_there_is_one_or_more_tags() | |
{ | |
it('should register the new tags'); // NotImplementedException | |
when('all tags are valid', function () { | |
yield fn() => $this->subject => does_not_throw(); | |
}); | |
with('blacklisted tags', function () { | |
$this->tags = fn() => ['pute', 'ok']; | |
when('only some tags are blacklisted', function () { | |
it('should throw a BlacklistedTagException', function () { | |
yield fn() => $this->subject => throws( | |
BlacklistedTagException::class | |
); | |
}); | |
it('should register the valid ones', function () { | |
yield Tag::count() => equals(1); | |
yield fn() => Tag::find('ok') => does_not_throw(); | |
}); | |
}); | |
when('all tags are blacklisted', function () { | |
$this->tags = fn() => ['pute', 'bite']; | |
it('should throw a BlacklistedTagException', function () { | |
yield fn() => $this->subject => throws( | |
BlacklistedTagException::class | |
); | |
}); | |
it('should not register any tag', function () { | |
yield Tag::count() => equals(0); | |
}); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment