Skip to content

Commit

Permalink
Add Comment entity #65
Browse files Browse the repository at this point in the history
  • Loading branch information
williarin authored Jun 18, 2024
2 parents c3c58e3 + 8908374 commit 30f81bc
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 7 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
fail-fast: true
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
symfony: ['6.0.*', '6.1.*', '6.2.*' , '6.3.*', '6.4.*', '7.0.*']
symfony: ['6.0.*', '6.1.*', '6.2.*' , '6.3.*', '6.4.*', '7.0.*', '7.1.*']
experimental: [false]
exclude:
- php: '8.0'
Expand All @@ -41,8 +41,12 @@ jobs:
symfony: '6.4.*' # Requires PHP >= 8.1 for compatibility
- php: '8.0'
symfony: '7.0.*' # Requires PHP >= 8.2 for compatibility
- php: '8.0'
symfony: '7.1.*' # Requires PHP >= 8.2 for compatibility
- php: '8.1'
symfony: '7.0.*' # Requires PHP >= 8.2 for compatibility
- php: '8.1'
symfony: '7.1.*' # Requires PHP >= 8.2 for compatibility
include:
- php: '8.4'
symfony: '6.0.*'
Expand All @@ -62,6 +66,9 @@ jobs:
- php: '8.4'
symfony: '7.0.*'
experimental: true
- php: '8.4'
symfony: '7.1.*'
experimental: true

steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ reset-install:
--admin_password=admin \
[email protected] \
--skip-email
@$(CLI) plugin install woocommerce --activate --version=7.9.0
@$(CLI) plugin install woocommerce --activate --version=8.9.3
@$(CLI) plugin install wordpress-importer --activate

reset-posts:
Expand Down Expand Up @@ -57,6 +57,10 @@ reset-woocommerce:
@$(CLI) post meta set 22 related_product 18
@$(CLI) post meta set 24 related_product 26
@$(CLI) post meta set 26 related_product 37
@$(CLI) comment create --comment_post_ID=15 --comment_content="I love this hoodie" --comment_author="John" --comment_type="review"
@$(CLI) comment create --comment_post_ID=19 --comment_content="Awesome belt!" --comment_author="Jane" --comment_type="review"
@$(CLI) comment meta set 2 rating "5"
@$(CLI) comment meta set 3 rating "4"

.PHONY: test
test:
Expand Down
6 changes: 2 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
version: '3.8'

services:
mysql:
image: mysql:8
image: mysql:8.0.37
ports:
- '33306:3306'
volumes:
Expand All @@ -13,7 +11,7 @@ services:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=wp_test
- MYSQL_ROOT_HOST=%
command: --default-authentication-plugin=mysql_native_password --max_connections=10000
command: --max_connections=10000

wordpress:
image: wordpress:fpm-alpine
Expand Down
65 changes: 65 additions & 0 deletions src/Bridge/Entity/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Williarin\WordpressInterop\Bridge\Entity;

use AllowDynamicProperties;
use DateTimeInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Williarin\WordpressInterop\Attributes\Id;
use Williarin\WordpressInterop\Attributes\RepositoryClass;
use Williarin\WordpressInterop\Bridge\Repository\CommentRepository;

#[AllowDynamicProperties]
#[RepositoryClass(CommentRepository::class)]
class Comment
{
use DynamicPropertiesTrait;

#[Id]
#[Groups('base')]
public ?int $commentId = null;

#[Groups('base')]
public ?int $commentPostId = null;

#[Groups('base')]
public ?string $commentAuthor = null;

#[Groups('base')]
public ?string $commentAuthorEmail = null;

#[Groups('base')]
public ?string $commentAuthorUrl = null;

#[Groups('base')]
public ?string $commentAuthorIp = null;

#[Groups('base')]
public ?DateTimeInterface $commentDate = null;

#[Groups('base')]
public ?DateTimeInterface $commentDateGmt = null;

#[Groups('base')]
public ?string $commentContent = null;

#[Groups('base')]
public ?int $commentKarma = null;

#[Groups('base')]
public ?string $commentApproved = null;

#[Groups('base')]
public ?string $commentAgent = null;

#[Groups('base')]
public ?string $commentType = null;

#[Groups('base')]
public ?int $commentParent = null;

#[Groups('base')]
public ?int $userId = null;
}
1 change: 1 addition & 0 deletions src/Bridge/Repository/AbstractEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ private function selectColumns(QueryBuilder $queryBuilder, array $extraFields, S
} elseif (str_starts_with($column, 'MAX(')) {
$selects[] = $column;
$hasExtraFields = true;
$this->joinSelfMetaTable($queryBuilder);
} else {
foreach ($this->tableAliases as $alias => $fields) {
if (in_array($column, $fields, true)) {
Expand Down
21 changes: 21 additions & 0 deletions src/Bridge/Repository/CommentRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Williarin\WordpressInterop\Bridge\Repository;

use Williarin\WordpressInterop\Bridge\Entity\Comment;

class CommentRepository extends AbstractEntityRepository
{
protected const TABLE_NAME = 'comments';
protected const TABLE_META_NAME = 'commentmeta';
protected const TABLE_IDENTIFIER = 'comment_id';
protected const TABLE_META_IDENTIFIER = 'comment_id';
protected const FALLBACK_ENTITY = Comment::class;

public function __construct()
{
parent::__construct(Comment::class);
}
}
67 changes: 67 additions & 0 deletions test/Test/Bridge/Repository/CommentRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Test\Bridge\Repository;

use Williarin\WordpressInterop\Bridge\Entity\Comment;
use Williarin\WordpressInterop\Bridge\Repository\RepositoryInterface;
use Williarin\WordpressInterop\Bridge\Repository\CommentRepository;
use Williarin\WordpressInterop\Criteria\SelectColumns;
use Williarin\WordpressInterop\Test\TestCase;

use function Williarin\WordpressInterop\Util\String\select_from_eav;

class CommentRepositoryTest extends TestCase
{
/** @var CommentRepository */
private RepositoryInterface $repository;

protected function setUp(): void
{
parent::setUp();
$this->repository = $this->manager->getRepository(Comment::class);
}

public function testFind(): void
{
$term = $this->repository->find(1);

self::assertInstanceOf(Comment::class, $term);
}

public function testFindByAttribute(): void
{
$comments = $this->repository
->setOptions([
'allow_extra_properties' => true,
])
->findBy([
new SelectColumns(['comment_id', 'comment_post_id', select_from_eav('rating', 'rating')]),
'rating' => '5',
]);

self::assertContainsOnlyInstancesOf(Comment::class, $comments);
self::assertCount(1, $comments);
self::assertSame(2, $comments[0]->commentId);
self::assertSame('5', $comments[0]->rating);
self::assertSame(15, $comments[0]->commentPostId);
}

public function testFindOneByWithSelectedAttribute(): void
{
$comments = $this->repository
->setOptions([
'allow_extra_properties' => true,
])
->findBy([
new SelectColumns(['comment_id', 'comment_post_id', select_from_eav('rating', 'rating')]),
]);

self::assertContainsOnlyInstancesOf(Comment::class, $comments);
self::assertCount(3, $comments);
self::assertNull($comments[0]->rating);
self::assertSame('5', $comments[1]->rating);
self::assertSame('4', $comments[2]->rating);
}
}
2 changes: 1 addition & 1 deletion test/Test/Bridge/Repository/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function testFindByCriteriaOr(): void
'stock_status' => 'instock',
]);

self::assertEquals([15, 16, 21, 22, 23, 26], array_column($products, 'id'));
self::assertEquals([16, 21, 22, 23, 26], array_column($products, 'id'));
}

public function testFindOneByWithLooseOperatorDoesNotThrowException(): void
Expand Down

0 comments on commit 30f81bc

Please sign in to comment.