Created
December 19, 2018 17:00
-
-
Save franksteinberg/5db0cf571a7ffbe1ca9f28b11fdc6d97 to your computer and use it in GitHub Desktop.
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
<?php | |
use App\Website; | |
class ItCanMatchWildcardQueriesTest extends TestCase | |
{ | |
/** | |
* @test | |
* @dataProvider matchingQueries | |
*/ | |
public function search_can_find_wildcard_matches($matchingQuery) | |
{ | |
// Arrange | |
$websiteNoWildcard = factory(Website::class)->create(['domain' => 'matching.com']); | |
$websiteWildcardNoMatch = factory(Website::class)->create(['domain' => '*nomatchsite.com']); | |
$websiteWildcardWithMatch = factory(Website::class)->create(['domain' => $matchingQuery]); | |
// Act | |
$actual = $this->getMatch('thisShouldbematching.com'); | |
// Assert | |
$this->assertEquals( | |
$websiteWildcardWithMatch->id, | |
$actual->id | |
); | |
} | |
/** | |
* @test | |
* @dataProvider nonmatchingQueries | |
*/ | |
public function it_can_return_null_if_no_match_is_found($matchingQuery) | |
{ | |
// Arrange | |
$websiteNoWildcard = factory(Website::class)->create(['domain' => 'matching.com']); | |
$websiteWildcardNoMatch = factory(Website::class)->create(['domain' => '*nomatchsite.com']); | |
$websiteFromQuery = factory(Website::class)->create(['domain' => $matchingQuery]); | |
// Act | |
$actual = $this->getMatch('thisShouldbematching.com'); | |
// Assert | |
$this->assertNull($actual); | |
} | |
protected function getMatch(string $domainGiven) | |
{ | |
return Website::where('domain', strtolower($domainGiven))->first() ?? $this->getWildcardMatches($domainGiven); | |
} | |
protected function getWildcardMatches(string $domainGiven) | |
{ | |
return Website::where('domain', 'like', '%*%')->get()->first(function ($index, $website) use ($domainGiven) { | |
return fnmatch(strtolower($website->domain), strtolower($domainGiven)); | |
}); | |
} | |
public function matchingQueries() | |
{ | |
return [ | |
['*MATCHING.com'], | |
['*matching.com*'], | |
['*ching.c*'], | |
['*m*g.com*'], | |
['thisShouldbematching.com'], | |
['thisshouldbematch*'], | |
]; | |
} | |
public function nonmatchingQueries() | |
{ | |
return [ | |
['*matching.co'], | |
['matching.co*'], | |
['thihoul*tching.com'], | |
['thisshouldbematch'], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment