-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from trendwerk/term
Term dimension
- Loading branch information
Showing
4 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
namespace Trendwerk\Search\Dimension; | ||
|
||
use BadMethodCallException; | ||
use wpdb; | ||
|
||
final class Term implements Dimension | ||
{ | ||
private $options; | ||
private $tableAlias = 'searchTerm'; | ||
private $wpdb; | ||
|
||
public function __construct(wpdb $wpdb, array $options) | ||
{ | ||
if (! isset($options['taxonomy'])) { | ||
throw new BadMethodCallException('`taxonomy` is a required property.'); | ||
} | ||
|
||
$this->options = $options; | ||
$this->wpdb = $wpdb; | ||
} | ||
|
||
public function join($aliasCount = 0) | ||
{ | ||
$tableAlias = $this->tableAlias . $aliasCount; | ||
|
||
$sql = "INNER JOIN {$this->wpdb->term_relationships} AS {$tableAlias} "; | ||
$sql .= "ON ({$this->wpdb->posts}.ID = {$tableAlias}.object_id)"; | ||
|
||
return $sql; | ||
} | ||
|
||
public function search($searchWord, $aliasCount = 0) | ||
{ | ||
$tableAlias = $this->tableAlias . $aliasCount; | ||
$searchWord = $this->wpdb->esc_like($searchWord); | ||
|
||
$termIds = array_map('absint', $this->termsFor($searchWord)); | ||
|
||
if (count($termIds) == 0) { | ||
return; | ||
} | ||
|
||
$termIds = implode(',', $termIds); | ||
|
||
return "{$tableAlias}.term_taxonomy_id IN ({$termIds})"; | ||
} | ||
|
||
private function termsFor($searchWord) | ||
{ | ||
return $this->wpdb->get_col($this->wpdb->prepare("SELECT {$this->wpdb->term_taxonomy}.term_id | ||
FROM {$this->wpdb->term_taxonomy} | ||
INNER JOIN {$this->wpdb->terms} USING(term_id) | ||
WHERE taxonomy = %s | ||
AND {$this->wpdb->terms}.name LIKE %s | ||
", $this->options['taxonomy'], "%{$searchWord}%")); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
namespace Trendwerk\Search\Test; | ||
|
||
use BadMethodCallException; | ||
use Mockery; | ||
use Trendwerk\Search\Dimension\Term; | ||
use WP_Mock; | ||
|
||
final class TermTest extends TestCase | ||
{ | ||
private $tableAlias = 'searchTerm'; | ||
private $wpdb; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->wpdb = Mockery::mock('wpdb'); | ||
$this->wpdb->posts = 'wp_posts'; | ||
$this->wpdb->term_relationships = 'wp_term_relationships'; | ||
$this->wpdb->term_taxonomy = 'wp_term_taxonomy'; | ||
$this->wpdb->terms = 'wp_terms'; | ||
} | ||
|
||
public function testKeyRequired() | ||
{ | ||
$this->expectException(BadMethodCallException::class); | ||
$meta = new Term($this->wpdb, []); | ||
} | ||
|
||
public function testJoin() | ||
{ | ||
$tableAliasCount = 2; | ||
$tableAlias = $this->tableAlias . $tableAliasCount; | ||
|
||
$expectation = "INNER JOIN {$this->wpdb->term_relationships} AS {$tableAlias} "; | ||
$expectation .= "ON ({$this->wpdb->posts}.ID = {$tableAlias}.object_id)"; | ||
|
||
$term = $this->create('taxonomyName'); | ||
|
||
$result = $term->join($tableAliasCount); | ||
|
||
$this->assertEquals($expectation, $result); | ||
} | ||
|
||
public function testSearch() | ||
{ | ||
$this->search('Testterm', 'testTaxonomy'); | ||
} | ||
|
||
public function testSearchAliasCount() | ||
{ | ||
$this->search('Term', 'taxonomy', 2); | ||
} | ||
|
||
public function testNoHit() | ||
{ | ||
$this->search('Testterm', 'testTaxonomy', 2, []); | ||
} | ||
|
||
private function search($searchWord, $taxonomy, $tableAliasCount = 0, $foundTermIds = [18, 12]) | ||
{ | ||
$tableAlias = $this->tableAlias . $tableAliasCount; | ||
|
||
if (count($foundTermIds) == 0) { | ||
$expectation = ''; | ||
} else { | ||
$termIds = implode(',', $foundTermIds); | ||
$expectation = "{$tableAlias}.term_taxonomy_id IN ({$termIds})"; | ||
} | ||
|
||
$term = $this->create($taxonomy); | ||
|
||
WP_Mock::wpPassthruFunction('absint', ['times' => count($foundTermIds)]); | ||
|
||
$this->wpdb->shouldReceive('esc_like') | ||
->once() | ||
->with($searchWord) | ||
->andReturn($searchWord); | ||
|
||
$this->wpdb->shouldReceive('prepare') | ||
->once() | ||
->andReturnUsing(function ($sql, $taxonomy, $search) { | ||
return sprintf($sql, $taxonomy, $search); | ||
}); | ||
|
||
$this->wpdb->shouldReceive('get_col') | ||
->once() | ||
->andReturn($foundTermIds); | ||
|
||
$result = $term->search($searchWord, $tableAliasCount); | ||
|
||
$this->assertEquals($expectation, $result); | ||
} | ||
|
||
private function create($taxonomy) | ||
{ | ||
$term = new Term($this->wpdb, [ | ||
'taxonomy' => $taxonomy, | ||
]); | ||
|
||
return $term; | ||
} | ||
} |