Skip to content

Commit

Permalink
test: add some tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
williarin committed Jul 14, 2022
1 parent 020d04f commit 8886776
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# WordPress Interop

[![Github Workflow](https://github.com/williarin/wordpress-interop/workflows/Test/badge.svg)](https://github.com/williarin/wordpress-interop/actions)

## Introduction

This library aims to simplify the interaction with WordPress databases through third-party applications.
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Williarin\WordpressInterop;

use InvalidArgumentException;
use Williarin\WordpressInterop\Bridge\Repository\RepositoryInterface;
use Williarin\WordpressInterop\Exception\InvalidArgumentException;

abstract class AbstractManagerRegistry implements ManagerRegistryInterface
{
Expand Down
30 changes: 21 additions & 9 deletions src/Exception/EntityNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ private function implodeCriteria(array $criteria): string
{
return implode(', ', array_filter(array_map(
function (mixed $value, int|string $field): string {
$field = $value instanceof RelationshipCondition
? sprintf('relationship ID "%s"', $value->getRelationshipIdOrOperand())
: $field;
if ($value instanceof RelationshipCondition) {
if (is_int($value->getRelationshipIdOrOperand())) {
$field = sprintf('relationship ID "%s"', $value->getRelationshipIdOrOperand());
} else {
$field = sprintf(
'relationship ID %s',
$this->getOperandAsString($value->getRelationshipIdOrOperand()),
);
}
}

if ($value instanceof Operand) {
$value = sprintf(
'"%s"',
is_array($value->getOperand())
? $this->implodeCriteria($value->getOperand())
: $value->getOperand(),
);
$value = $this->getOperandAsString($value);
} elseif ($value instanceof RelationshipCondition) {
$value = sprintf('having a field "%s"', $value->getRelationshipFieldName());
} elseif ($value instanceof TermRelationshipCondition) {
Expand All @@ -57,4 +59,14 @@ function (mixed $value, int|string $field): string {
array_keys($criteria),
)));
}

private function getOperandAsString(Operand $operand): string
{
return sprintf(
'%s',
is_array($operand->getOperand())
? $this->implodeCriteria($operand->getOperand())
: sprintf('"%s"', $operand->getOperand()),
);
}
}
6 changes: 2 additions & 4 deletions test/Fixture/Repository/BarRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

namespace Williarin\WordpressInterop\Test\Fixture\Repository;

use Symfony\Component\Serializer\SerializerInterface;
use Williarin\WordpressInterop\Bridge\Repository\AbstractEntityRepository;
use Williarin\WordpressInterop\EntityManagerInterface;
use Williarin\WordpressInterop\Test\Fixture\Entity\Bar;

final class BarRepository extends AbstractEntityRepository
{
public function __construct(protected EntityManagerInterface $entityManager, SerializerInterface $serializer)
public function __construct()
{
parent::__construct($entityManager, $serializer, Bar::class);
parent::__construct(Bar::class);
}

public function getBarTerms(): array
Expand Down
11 changes: 11 additions & 0 deletions test/Test/Bridge/Repository/OptionRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Williarin\WordpressInterop\Bridge\Entity\Option;
use Williarin\WordpressInterop\Bridge\Repository\RepositoryInterface;
use Williarin\WordpressInterop\Exception\MethodNotFoundException;
use Williarin\WordpressInterop\Exception\OptionAlreadyExistsException;
use Williarin\WordpressInterop\Exception\OptionNotFoundException;
use Williarin\WordpressInterop\Test\TestCase;
Expand Down Expand Up @@ -54,6 +55,16 @@ public function testCallMagicGetterForNonExistentOptionThrowsException(): void
$this->repository->getSomeNonExistentOption();
}

public function testCallMagicGetterForNonExistentMethodThrowsException(): void
{
$this->expectException(MethodNotFoundException::class);
$this->repository->someNonExistentMethod();
}

public function testGetEntityClassName(): void
{
self::assertSame(Option::class, $this->repository->getEntityClassName());
}

public function testCreateNewStringValue(): void
{
Expand Down
5 changes: 5 additions & 0 deletions test/Test/Bridge/Repository/PostMetaRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ protected function setUp(): void
$this->repository = $this->manager->getRepository(PostMeta::class);
}

public function testGetEntityClassName(): void
{
self::assertSame(PostMeta::class, $this->repository->getEntityClassName());
}

public function testFindReturnsCorrectValue(): void
{
self::assertEquals('value3', $this->repository->find(11, 'key1'));
Expand Down
7 changes: 7 additions & 0 deletions test/Test/Bridge/Repository/PostRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Williarin\WordpressInterop\Exception\InvalidEntityException;
use Williarin\WordpressInterop\Exception\InvalidFieldNameException;
use Williarin\WordpressInterop\Exception\InvalidOrderByOrientationException;
use Williarin\WordpressInterop\Exception\MethodNotFoundException;
use Williarin\WordpressInterop\Test\TestCase;

class PostRepositoryTest extends TestCase
Expand Down Expand Up @@ -107,6 +108,12 @@ public function testFindOneByPostStatusOrderByDescUsingMagicCall(): void
self::assertEquals(10, $post->id);
}

public function testMagicCallNonExistentMethod(): void
{
$this->expectException(MethodNotFoundException::class);
$this->repository->nothingLikeThis();
}

public function testOrderByFieldValidationWithFindOneByUsingMagicCall(): void
{
$this->expectException(InvalidFieldNameException::class);
Expand Down
18 changes: 18 additions & 0 deletions test/Test/Bridge/Repository/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ public function testOverrideSelectClauseWithinCriteria(): void
self::assertEquals([$product1, $product2], $products);
}

public function testSelectColumnsWithManualEav(): void
{
$products = $this->repository->findBy([
new SelectColumns(['post_title', select_from_eav('sku')]),
'sku' => new Operand('hoodie.*logo|zipper', Operand::OPERATOR_REGEXP),
]);

$product1 = new Product();
$product1->postTitle = 'Hoodie with Logo';
$product1->sku = 'woo-hoodie-with-logo';

$product2 = new Product();
$product2->sku = 'woo-hoodie-with-zipper';
$product2->postTitle = 'Hoodie with Zipper';

self::assertEquals([$product1, $product2], $products);
}

public function testOperatorInWithEavAttribute(): void
{
$products = $this->repository->findBySku(
Expand Down
39 changes: 39 additions & 0 deletions test/Test/Bridge/Repository/TermRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,43 @@ public function testAddTermsToEntity(): void
self::assertEquals(['external', 'Hoodies', 'Decor', 'MegaBrand'], array_column($termsProduct, 'name'));
self::assertEquals([1, 6, 1, 2], array_column($termsProduct, 'count'));
}

public function testAddTermsToEntityWithoutTermTaxonomyIdAreIgnored(): void
{
$hoodieTerms = array_map(
static function (Term $term) {
unset($term->termTaxonomyId);
return $term;
},
$this->repository->findBy([
new PostRelationshipCondition(Product::class, [
'post_status' => new Operand(['publish', 'private'], Operand::OPERATOR_IN),
'sku' => 'super-forces-hoodie',
]),
'taxonomy' => new Operand(['product_tag', 'product_type', 'product_visibility'], Operand::OPERATOR_NOT_IN),
]),
);

$product = $this->manager->getRepository(Product::class)
->find(37)
;

$termsProduct = $this->repository->findBy([
new PostRelationshipCondition(Product::class, [
'id' => $product->id,
]),
]);

self::assertEquals(['external', 'Decor'], array_column($termsProduct, 'name'));

$this->repository->addTermsToEntity($product, $hoodieTerms);

$termsProduct = $this->repository->findBy([
new PostRelationshipCondition(Product::class, [
'id' => $product->id,
]),
]);

self::assertEquals(['external', 'Decor'], array_column($termsProduct, 'name'));
}
}
17 changes: 17 additions & 0 deletions test/Test/Bridge/Type/GenericDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Williarin\WordpressInterop\Bridge\Type\GenericData;
use PHPUnit\Framework\TestCase;
use Williarin\WordpressInterop\Exception\InvalidArgumentException;
use Williarin\WordpressInterop\Exception\MethodNotFoundException;

class GenericDataTest extends TestCase
Expand Down Expand Up @@ -66,4 +67,20 @@ public function testCallRandomMethod(): void
$this->expectException(MethodNotFoundException::class);
$genericData->randomMethod();
}

public function testGetInvalidProperty(): void
{
$genericData = new GenericData();
$genericData->data = ['test' => 'this is a test'];
$this->expectException(InvalidArgumentException::class);
$genericData->invalidProperty;
}

public function testGetInvalidPropertyGetter(): void
{
$genericData = new GenericData();
$genericData->data = ['test' => 'this is a test'];
$this->expectException(InvalidArgumentException::class);
$genericData->getInvalidMethod();
}
}
38 changes: 38 additions & 0 deletions test/Test/Criteria/RelationshipCriteriaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Williarin\WordpressInterop\Test\Criteria;

use PHPUnit\Framework\TestCase;
use Williarin\WordpressInterop\Criteria\Operand;
use Williarin\WordpressInterop\Criteria\RelationshipCondition;
use Williarin\WordpressInterop\Exception\RelationshipIdDeprecationException;

final class RelationshipCriteriaTest extends TestCase
{
public function testGetRelationshipIdWithIdGiven(): void
{
$condition = new RelationshipCondition(12, '_thumbnail_id');
self::assertSame(12, $condition->getRelationshipId());
}

public function testGetRelationshipIdWithOperandGiven(): void
{
$condition = new RelationshipCondition(new Operand('sku', Operand::OPERATOR_EQUAL), '_thumbnail_id');
$this->expectException(RelationshipIdDeprecationException::class);
$condition->getRelationshipId();
}

public function testGetRelationshipIdOrOperandWithIdGiven(): void
{
$condition = new RelationshipCondition(12, '_thumbnail_id');
self::assertSame(12, $condition->getRelationshipIdOrOperand());
}

public function testGetRelationshipIdOrOperandWithOperandGiven(): void
{
$condition = new RelationshipCondition(new Operand('sku', Operand::OPERATOR_EQUAL), '_thumbnail_id');
self::assertInstanceOf(Operand::class, $condition->getRelationshipIdOrOperand());
}
}
16 changes: 16 additions & 0 deletions test/Test/EntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,20 @@ public function testGetRepositoryReturnsDefaultEntityRepository(): void
self::assertInstanceOf(AbstractEntityRepository::class, $repository);
self::assertNotInstanceOf(BarRepository::class, $repository);
}

public function testAddRepository(): void
{
$this->manager->addRepository(new BarRepository());
self::assertInstanceOf(BarRepository::class, $this->manager->getRepository(Bar::class));
}

public function testGetRepositories(): void
{
$repositories = $this->manager->getRepositories();
self::assertEquals([], $repositories);

$this->manager->addRepository(new BarRepository());
$repositories = $this->manager->getRepositories();
self::assertCount(1, $repositories);
}
}
Loading

0 comments on commit 8886776

Please sign in to comment.