diff --git a/tests/ProfaneValidatorBuilder.php b/tests/ProfaneValidatorBuilder.php new file mode 100644 index 0000000..374cd68 --- /dev/null +++ b/tests/ProfaneValidatorBuilder.php @@ -0,0 +1,48 @@ +profaneValidator = new ProfaneValidator; + + if ($dictionary) { + $this->profaneValidator->setDictionary($dictionary); + } + } + + /** + * [validate description] + * @param array $parameters [description] + * @return [type] [description] + */ + public function validate(array $parameters) + { + list($attribute, $text, $parameters) = $parameters; + + return $this->build()->validate($attribute, $text, $parameters); + } + + /** + * [build description] + * @return LaravelProfane\ProfaneValidator + */ + public function build() + { + return $this->profaneValidator; + } +} diff --git a/tests/ProfaneValidatorTest.php b/tests/ProfaneValidatorTest.php index 9654201..e0eea94 100644 --- a/tests/ProfaneValidatorTest.php +++ b/tests/ProfaneValidatorTest.php @@ -2,140 +2,156 @@ namespace LaravelProfaneTests; -use LaravelProfaneTests\TestCase; use LaravelProfane\ProfaneValidator; +use LaravelProfaneTests\TestCase; +use LaravelProfaneTests\ProfaneValidatorBuilder; class ProfaneValidatorTest extends TestCase { - public function test_can_validate_a_word_with_numbers() - { - $attribute = 'username'; - $word = 'culero23'; - $parameters = ['es']; - $profane = new ProfaneValidator(); - $this->assertFalse($profane->validate($attribute, $word, $parameters)); - } - - public function test_can_validate_a_text() - { - $attribute = 'description'; - $text = 'fck you bitch. 幹!'; - $parameters = ['es', 'en' , 'zh-tw']; - - $profane = new ProfaneValidator(); - - $this->assertFalse($profane->validate($attribute, $text, $parameters)); - } - - public function test_can_evaluate_profanity_of_a_word() - { - $word = 'fuck'; - $profane = new ProfaneValidator(); - $this->assertTrue($profane->isProfane($word)); - } - - public function test_can_evaluate_profanity_of_a_sentence() - { - $word = 'fuck you if you read this'; - $profane = new ProfaneValidator(); - $this->assertTrue($profane->isProfane($word)); - } - - public function test_can_evaluate_profanity_of_a_html_string() - { - $word = 'fuck you if you read this.'; - $profane = new ProfaneValidator(); - $this->assertTrue($profane->isProfane($word)); - } - - public function test_can_evaluate_as_caseless_mode() - { - $word = 'FUCK you BITCH if you read this.'; - $profane = new ProfaneValidator(); - $this->assertTrue($profane->isProfane($word)); - } - - public function test_match_exact_word() - { - $profane = new ProfaneValidator(); - // it thinks class ~= ass - $this->assertTrue($profane->isProfane('class')); - // but this should be profane - $this->assertTrue($profane->isProfane('sucker96')); - } - public function test_can_set_dictionary_when_you_pass_a_locale() { - $profane = new ProfaneValidator(); - $profane->setDictionary('es'); + $builder = new ProfaneValidatorBuilder('es'); + $expected = include __DIR__.'/../src/dict/es.php'; - $this->assertEquals($profane->getBadwords(), $expected); + + $this->assertEquals($builder->build()->getBadwords(), $expected); } public function test_can_set_dictionary_when_you_pass_a_file() { - $profane = new ProfaneValidator(); - $profane->setDictionary(__DIR__.'/../src/dict/es.php'); + $builder = new ProfaneValidatorBuilder(__DIR__.'/../src/dict/es.php'); + $expected = include __DIR__.'/../src/dict/es.php'; - $this->assertEquals($profane->getBadwords(), $expected); + + $this->assertEquals($builder->build()->getBadwords(), $expected); } public function test_can_set_dictionary_when_you_pass_an_array_of_files() { - $profane = new ProfaneValidator(); - $profane->setDictionary([__DIR__.'/../src/dict/es.php', __DIR__.'/../src/dict/en.php']); + $builder = new ProfaneValidatorBuilder([ + __DIR__.'/../src/dict/es.php', + __DIR__.'/../src/dict/en.php' + ]); - $expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php'); + $expected = array_merge( + include __DIR__.'/../src/dict/es.php', + include __DIR__.'/../src/dict/en.php' + ); - $this->assertEquals($profane->getBadwords(), $expected); + $this->assertEquals($builder->build()->getBadwords(), $expected); } public function test_can_set_dictionary_when_you_pass_an_array_of_locales() { - $profane = new ProfaneValidator(); - $profane->setDictionary(['es', 'en', 'it', 'zh-tw']); - $expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/it.php', include __DIR__.'/../src/dict/zh-tw.php'); - $this->assertEquals($profane->getBadwords(), $expected); + $builder = new ProfaneValidatorBuilder(['es', 'en', 'it', 'zh-tw']); + + $expected = array_merge( + include __DIR__.'/../src/dict/es.php', + include __DIR__.'/../src/dict/en.php', + include __DIR__.'/../src/dict/it.php', + include __DIR__.'/../src/dict/zh-tw.php' + ); + + $this->assertEquals($builder->build()->getBadwords(), $expected); + } + + public function test_can_validate_a_word_with_numbers() + { + $builder = new ProfaneValidatorBuilder(); + + $this->assertFalse($builder->validate(['username', 'culero23', ['es']])); + } + + public function test_can_validate_a_text() + { + $builder = new ProfaneValidatorBuilder(); + + $this->assertFalse($builder->validate(['description', 'fck you bitch. 幹!', ['es', 'en' , 'zh-tw']])); + } + + public function test_can_evaluate_profanity_of_a_word() + { + $builder = new ProfaneValidatorBuilder(); + + $word = 'fuck'; + + $this->assertTrue($builder->build()->isProfane($word)); + } + + public function test_can_evaluate_profanity_of_a_sentence() + { + $builder = new ProfaneValidatorBuilder(); + + $word = 'fuck you if you read this'; + + $this->assertTrue($builder->build()->isProfane($word)); + } + + public function test_can_evaluate_profanity_of_a_html_string() + { + $builder = new ProfaneValidatorBuilder(); + + $word = 'fuck you if you read this.'; + + $this->assertTrue($builder->build()->isProfane($word)); + } + + public function test_can_evaluate_as_caseless_mode() + { + $builder = new ProfaneValidatorBuilder(); + + $word = 'FUCK you BITCH if you read this.'; + + $this->assertTrue($builder->build()->isProfane($word)); + } + + public function test_match_exact_word() + { + $builder = new ProfaneValidatorBuilder(); + + // it thinks class ~= ass + $this->assertTrue($builder->build()->isProfane('class')); + + // but this should be profane + $this->assertTrue($builder->build()->isProfane('sucker96')); } public function test_can_validate_a_bad_word_with_accent() { - $profane = new ProfaneValidator(); - $profane->setDictionary('sk'); + $builder = new ProfaneValidatorBuilder('sk'); + $word = "piča"; - $this->assertTrue( $profane->isProfane($word) ); + + $this->assertTrue($builder->build()->isProfane($word)); } public function test_enie_in_spanish_is_evaluated() { - $profane = new ProfaneValidator(); - $profane->setDictionary('es'); - $word = "coño"; + $builder = new ProfaneValidatorBuilder('es'); + // in spanish coño =! cono - $this->assertTrue( $profane->isProfane($word) ); + $word = "coño"; + + $this->assertTrue($builder->build()->isProfane($word)); } public function test_can_validate_a_word_in_greek() { $this->mockConfigs(); - $profane = new ProfaneValidator(); - $profane->setDictionary('gr'); + $builder = new ProfaneValidatorBuilder('gr'); $word = "μαλάκας"; - $this->assertTrue($profane->isProfane($word)); + $this->assertTrue($builder->build()->isProfane($word)); } public function test_can_validate_a_text_in_greek() { $this->mockConfigs(); - $attribute = 'description'; - $text = 'εισαι πουτανα'; - $parameters = ['en' , 'gr']; - $profane = new ProfaneValidator(); + $builder = new ProfaneValidatorBuilder(); - $this->assertFalse($profane->validate($attribute, $text, $parameters)); + $this->assertFalse($builder->validate(['description', 'εισαι πουτανα', ['en' , 'gr']])); } }