Skip to content

Commit

Permalink
Add WithMetaBuilderTest, fix join meta value
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitriBouteille committed Jan 29, 2024
1 parent 72eb1ff commit 02869df
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 3 deletions.
16 changes: 15 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
"friendsofphp/php-cs-fixer": "^3.28",
"phpstan/phpstan": "^1.10",
"rector/rector": "^0.18.4",
"phpunit/phpunit": "^10.5"
"phpunit/phpunit": "^10.5",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/extension-installer": "^1.3"
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"extra": {
"phpstan": {
"includes": [
"extension.neon"
]
}
}
}
4 changes: 2 additions & 2 deletions src/Builders/AbstractWithMetaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function addMetaToSelect(string $metaKey, ?string $alias = null): self
{
$this->joinToMeta($metaKey);
if ($alias === null || $alias === '') {
$alias = $metaKey;
$alias = sprintf('%s_value', $metaKey);
}

$column = sprintf('%s.%s AS %s', $metaKey, $this->metaConfig?->columnValue, $alias);
Expand Down Expand Up @@ -128,7 +128,7 @@ public function joinToMeta(string $metaKey, string $joinType = 'inner'): self
$join->on(
sprintf('%s.%s', $metaKey, $this->metaConfig?->columnKey),
'=',
$join->raw(sprintf("'%s'", $metaKey))
"$metaKey"
)->on(
sprintf('%s.%s', $metaKey, $this->metaConfig?->foreignKey),
'=',
Expand Down
166 changes: 166 additions & 0 deletions src/Tests/Unit/Builders/WithMetaBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php
/**
* Copyright (c) 2024 Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Tests\Unit\Builders;

use Dbout\WpOrm\Builders\PostBuilder;
use Dbout\WpOrm\Exceptions\WpOrmException;
use Dbout\WpOrm\Models\Post;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @since 3.0.0
* @coversDefaultClass \Dbout\WpOrm\Builders\AbstractWithMetaBuilder
*/
class WithMetaBuilderTest extends TestCase
{
private PostBuilder $builder;

private Post&MockObject $post;

/**
* @inheritDoc
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
protected function setUp(): void
{
$queryBuilder = new \Illuminate\Database\Query\Builder(
$this->createMock(\Illuminate\Database\MySqlConnection::class),
new \Illuminate\Database\Query\Grammars\Grammar(),
new \Illuminate\Database\Query\Processors\Processor()
);

$post = $this->createPartialMock(Post::class, ['getTable']);
$post->method('getTable')->willReturn('posts');
$this->post = $post;
$builder = new PostBuilder($queryBuilder);
$builder->setModel($this->post);
$this->builder = $builder;
}

/**
* @param string $metaKey
* @param string|null $alias
* @param string $expectedQuery
* @throws WpOrmException
* @return void
* @covers ::addMetaToSelect
* @dataProvider providerTestAddMetaToSelect
*/
public function testAddMetaToSelect(string $metaKey, ?string $alias, string $expectedQuery): void
{
$this->builder->addMetaToSelect($metaKey, $alias);
$query = $this->builder->toSql();
$this->assertEquals($expectedQuery, $query);
}

/**
* @return array
*/
public static function providerTestAddMetaToSelect(): array
{
return [
'Without alias' => [
'my_meta',
null,
'select "posts".*, "my_meta"."meta_value" as "my_meta_value" from "posts" inner join "postmeta" as "my_meta" on "my_meta"."meta_key" = "my_meta" and "my_meta"."post_id" = "posts"."ID"',
],
'With alias' => [
'first_name',
'my_custom_alias',
'select "posts".*, "first_name"."meta_value" as "my_custom_alias" from "posts" inner join "postmeta" as "first_name" on "first_name"."meta_key" = "first_name" and "first_name"."post_id" = "posts"."ID"',
],
];
}

/**
* @param array $metas
* @param string $expectedQuery
* @throws WpOrmException
* @return void
* @covers ::addMetasToSelect
* @dataProvider providerTestAddMetasToSelect
*/
public function testAddMetasToSelect(array $metas, string $expectedQuery): void
{
$this->builder->addMetasToSelect($metas);
$query = $this->builder->toSql();
$this->assertEquals($expectedQuery, $query);
}

/**
* @return array
*/
public static function providerTestAddMetasToSelect(): array
{
return [
'Without alias' => [
[
'firstname',
'lastname',
],
'select "posts".*, "firstname"."meta_value" as "firstname_value", "lastname"."meta_value" as "lastname_value" from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = "firstname" and "firstname"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = "lastname" and "lastname"."post_id" = "posts"."ID"',
],
'With alias' => [
[
'my_meta' => 'firstname',
'second_meta' => 'lastname',
],
'select "posts".*, "firstname"."meta_value" as "my_meta", "lastname"."meta_value" as "second_meta" from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = "firstname" and "firstname"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = "lastname" and "lastname"."post_id" = "posts"."ID"',
],
'On meta with alias on another one without alias' => [
[
'my_meta' => 'street_1',
'lastname',
],
'select "posts".*, "street_1"."meta_value" as "my_meta", "lastname"."meta_value" as "lastname_value" from "posts" inner join "postmeta" as "street_1" on "street_1"."meta_key" = "street_1" and "street_1"."post_id" = "posts"."ID" inner join "postmeta" as "lastname" on "lastname"."meta_key" = "lastname" and "lastname"."post_id" = "posts"."ID"',
],
];
}

/**
* @throws WpOrmException
* @return void
* @covers ::joinToMeta
*/
public function testJoinToMeta(): void
{
$this->post->expects($this->once())->method('getTable');
$this->builder->joinToMeta('my_meta');
$query = $this->builder->toSql();
$this->assertEquals(
'select "posts".* from "posts" inner join "postmeta" as "my_meta" on "my_meta"."meta_key" = "my_meta" and "my_meta"."post_id" = "posts"."ID"',
$query
);
}

/**
* @throws WpOrmException
* @return void
* @covers ::addMetaToFilter
*/
public function testAddMetaToFilter(): void
{
$this->builder->addMetaToFilter('firstname', 'Dimitri');
$query = $this->builder->toSql();

$this->assertEquals(
'select "posts".* from "posts" inner join "postmeta" as "firstname" on "firstname"."meta_key" = "firstname" and "firstname"."post_id" = "posts"."ID" where "firstname"."meta_value" = ?',
$query,
);

$this->assertEquals(
[
'Dimitri',
],
$this->builder->getBindings(),
);
}
}

0 comments on commit 02869df

Please sign in to comment.