Skip to content

Commit

Permalink
Merge pull request #1 from mehedimi/update-wpqb
Browse files Browse the repository at this point in the history
Compatible with WP QB
  • Loading branch information
mehedimi authored Apr 23, 2023
2 parents d25b968 + b2bf762 commit 808633e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 109 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"mockery/mockery": "^1.5"
},
"require": {
"mehedimi/wp-query-builder": "dev-main"
"mehedimi/wp-query-builder": "*"
},
"scripts": {
"test": "phpunit tests --testdox"
}
}
79 changes: 39 additions & 40 deletions src/Relations/WithTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,29 @@ class WithTaxonomy extends Relation
*/
protected $groupBy = false;


/**
* Extract object IDs
* Set one or many taxonomy type
*
* @return array
* @param array|string $taxonomy
* @return $this
*/
public function extractObjectKeys()
public function taxonomy($taxonomy)
{
return array_column($this->items, 'ID');
$this->taxonomies = is_array($taxonomy) ? $taxonomy : [$taxonomy];

return $this;
}

/**
* @inheritDoc
* Set group by taxonomy state
*
* @param $state
* @return $this
*/
protected function getLoadedItems()
public function groupByTaxonomy($state = true)
{
return $this->builder
->from($this->termTableName)
->select($this->termTableName . '.*', $this->termTaxonomyTable . '.count', $this->termTaxonomyTable . '.taxonomy', $this->termRelationshipTable . '.object_id')
->join($this->termRelationshipTable, function (Join $join) {
$join->on(
$this->termTableName . '.term_id', '=', $this->termTableName . '.term_id'
)->whereIn($this->termRelationshipTable . '.object_id', $this->extractObjectKeys());
})->join($this->termTaxonomyTable, function (Join $join) {
$join->on($this->termTaxonomyTable . '.term_taxonomy_id', '=', $this->termRelationshipTable . '.term_taxonomy_id');
if (!empty($this->taxonomies)) {
$join->whereIn($this->termTaxonomyTable . '.taxonomy', $this->taxonomies);
}
})->get();
$this->groupBy = $state;
return $this;
}

/**
Expand All @@ -95,37 +89,42 @@ protected function loadedItemsDictionary()
/**
* @inheritDoc
*/
protected function getItemFromDictionary($loadedItems, $item)
protected function getLoadedItems()
{
if (array_key_exists($item->ID, $loadedItems)) {
return $loadedItems[$item->ID];
}

return [];
return $this->builder
->from($this->termTableName)
->select($this->termTableName . '.*', $this->termTaxonomyTable . '.count', $this->termTaxonomyTable . '.taxonomy', $this->termRelationshipTable . '.object_id')
->join($this->termRelationshipTable, function (Join $join) {
$join->on(
$this->termTableName . '.term_id', '=', $this->termTableName . '.term_id'
)->whereIn($this->termRelationshipTable . '.object_id', $this->extractObjectKeys());
})->join($this->termTaxonomyTable, function (Join $join) {
$join->on($this->termTaxonomyTable . '.term_taxonomy_id', '=', $this->termRelationshipTable . '.term_taxonomy_id');
if (!empty($this->taxonomies)) {
$join->whereIn($this->termTaxonomyTable . '.taxonomy', $this->taxonomies);
}
})->get();
}

/**
* Set one or many taxonomy type
* Extract object IDs
*
* @param array|string $taxonomy
* @return $this
* @return array
*/
public function taxonomy($taxonomy)
public function extractObjectKeys()
{
$this->taxonomies = is_array($taxonomy) ? $taxonomy : [$taxonomy];

return $this;
return array_column($this->items, 'ID');
}

/**
* Set group by taxonomy state
*
* @param $state
* @return $this
* @inheritDoc
*/
public function groupByTaxonomy($state = true)
protected function getItemFromDictionary($loadedItems, $item)
{
$this->groupBy = $state;
return $this;
if (array_key_exists($item->ID, $loadedItems)) {
return $loadedItems[$item->ID];
}

return [];
}
}
9 changes: 0 additions & 9 deletions tests/BuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@
namespace Mehedi\WPQueryBuilderTestsExt;

use Mehedi\WPQueryBuilder\Query\Builder;
use Mehedi\WPQueryBuilder\Query\Grammar;
use Mehedi\WPQueryBuilder\Query\WPDB;

trait BuilderHelper
{
public function builder()
{
return new Builder();
}

function initFakeDB()
{
Grammar::getInstance();

WPDB::set(new FakeWPDB());
}
}
18 changes: 0 additions & 18 deletions tests/FakeWPDB.php

This file was deleted.

13 changes: 7 additions & 6 deletions tests/Unit/JoinWithPostMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mehedi\WPQueryBuilderTestsExt\Unit;

use Mehedi\WPQueryBuilder\Query\Grammar;
use Mehedi\WPQueryBuilder\Query\Join;
use Mehedi\WPQueryBuilderExt\Plugins\JoinPostWithMeta;
use Mehedi\WPQueryBuilderTestsExt\BuilderHelper;
Expand Down Expand Up @@ -36,14 +37,14 @@ function it_can_apply_the_plugin()
/**
* @test
*/
function it_can_generate_currect_sql()
function it_can_generate_correct_sql()
{
$this->initFakeDB();
Grammar::getInstance()->setTablePrefix('wp_');

FakeWPDB::add('get_results', function ($sql) {
$this->assertEquals('select * from wp_posts inner join wp_postmeta on wp_posts.ID = wp_postmeta.post_id limit 10', $sql);
});
$sql = 'select * from wp_posts inner join wp_postmeta on wp_posts.ID = wp_postmeta.post_id limit 10';

$this->builder()->plugin(new JoinPostWithMeta())->limit(10)->get();
$outSql = $this->builder()->plugin(new JoinPostWithMeta())->limit(10)->toSQL();

$this->assertEquals($sql, $outSql);
}
}
62 changes: 27 additions & 35 deletions tests/Unit/TaxonomyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Mehedi\WPQueryBuilderTestsExt\Unit;

use Mehedi\WPQueryBuilder\Connection;
use Mehedi\WPQueryBuilder\Query\Builder;
use Mehedi\WPQueryBuilder\Query\Grammar;
use Mehedi\WPQueryBuilderExt\Relations\WithTaxonomy;
use Mehedi\WPQueryBuilderTestsExt\BuilderHelper;
use Mehedi\WPQueryBuilderTestsExt\FakeWPDB;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class TaxonomyTest extends TestCase
Expand All @@ -16,7 +19,7 @@ class TaxonomyTest extends TestCase
*/
function it_can_add_relation()
{
$builder = $this->builder()->addRelation(new WithTaxonomy('taxonomies', $this->builder()));
$builder = $this->builder()->withRelation(new WithTaxonomy('taxonomies', $this->builder()));

$this->assertInstanceOf(WithTaxonomy::class, $builder->with[0]);
}
Expand All @@ -26,36 +29,37 @@ function it_can_add_relation()
*/
function it_can_generate_correct_sql_query()
{
$this->initFakeDB();
FakeWPDB::add('prepare', function ($sql, ...$args) {
return sprintf(str_replace('%s', "'%s'", $sql), ...$args);
});
Grammar::getInstance()->setTablePrefix('wp_');

FakeWPDB::add('get_results', function ($sql) {
$this->assertEquals('select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (1) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id', $sql);
return [];
});
$c = m::mock(Connection::class);

(new WithTaxonomy('taxonomies'))->setItems([(object)['ID' => 1]])->load();
$c->shouldReceive('select')->andReturn([]);

FakeWPDB::add('get_results', function ($sql) {
$this->assertEquals("select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (1) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id and wp_term_taxonomy.taxonomy in ('category')", $sql);
return [];
});
$b = new Builder($c);

(new WithTaxonomy('taxonomies'))
$sql = 'select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (?) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id';

(new WithTaxonomy('taxonomies', $b))->setItems([(object)['ID' => 1]])->load();

$this->assertEquals($sql, $b->toSQL());

$sql = "select wp_terms.*, wp_term_taxonomy.count, wp_term_taxonomy.taxonomy, wp_term_relationships.object_id from wp_terms inner join wp_term_relationships on wp_terms.term_id = wp_terms.term_id and wp_term_relationships.object_id in (?) inner join wp_term_taxonomy on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id and wp_term_taxonomy.taxonomy in (?)";
$b = new Builder($c);
(new WithTaxonomy('taxonomies', $b))
->taxonomy('category')
->setItems([(object)['ID' => 1]])
->load();

$this->assertEquals($sql, $b->toSQL());

m::close();
}

/**
* @test
*/
function it_can_map_item()
{
$this->initFakeDB();

$taxonomies = [
(object)[
'term_id' => 1,
Expand All @@ -66,31 +70,19 @@ function it_can_map_item()
]
];

FakeWPDB::add('prepare', function () use (&$taxonomies) {

});
$c = m::mock(Connection::class);

FakeWPDB::add('get_results', function () use (&$taxonomies) {
return $taxonomies;
});
$c->shouldReceive('select')->andReturn($taxonomies);

$data = (new WithTaxonomy('taxonomies'))->setItems([(object)['ID' => 1]])->load();
$b = new Builder($c);

$this->assertEquals([
(object)[
'ID' => 1,
'taxonomies' => $taxonomies
]
], $data);

$data = (new WithTaxonomy('taxonomies'))->groupByTaxonomy()->setItems([(object)['ID' => 1]])->load();
$data = (new WithTaxonomy('taxonomies', $b))->setItems([(object)['ID' => 1]])->load();

$this->assertEquals([
(object)[
'ID' => 1,
'taxonomies' => [
'category' => $taxonomies
]
'taxonomies' => $taxonomies
]
], $data);
}
Expand Down

0 comments on commit 808633e

Please sign in to comment.