Skip to content

Commit

Permalink
feat: users querying
Browse files Browse the repository at this point in the history
  • Loading branch information
williarin committed Mar 5, 2022
1 parent c82db24 commit 32716d5
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ $repository->updateSingleField(4, 'post_status', 'publish');
* `Option` and `OptionRepository`
* `PostMeta` and `PostMetaRepository`
* `Term` and `TermRepository`
* `User` and `UserRepository`
* `Product` and `ProductRepository` (WooCommerce)
* `ShopOrder` and `ShopOrderRepository` (WooCommerce)
* `ShopOrderItem` and `ShopOrderItemRepository` (WooCommerce)
Expand Down
69 changes: 69 additions & 0 deletions src/Bridge/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Williarin\WordpressInterop\Bridge\Entity;

use Symfony\Component\Serializer\Annotation\Groups;
use Williarin\WordpressInterop\Attributes\Id;
use Williarin\WordpressInterop\Attributes\RepositoryClass;
use Williarin\WordpressInterop\Bridge\Repository\UserRepository;
use Williarin\WordpressInterop\Bridge\Type\GenericData;

#[RepositoryClass(UserRepository::class)]
class User
{
#[Id]
#[Groups('base')]
public ?int $id = null;

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

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

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

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

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

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

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

public ?string $nickname = null;
public ?string $firstName = null;
public ?string $lastName = null;
public ?string $description = null;
public ?string $locale = null;
public ?GenericData $capabilities = null;
public ?string $lastUpdate = null;
public ?string $billingFirstName = null;
public ?string $billingLastName = null;
public ?string $billingCompany = null;
public ?string $billingAddress1 = null;
public ?string $billingAddress2 = null;
public ?string $billingCity = null;
public ?string $billingState = null;
public ?string $billingPostcode = null;
public ?string $billingCountry = null;
public ?string $billingEmail = null;
public ?string $billingPhone = null;
public ?string $shippingFirstName = null;
public ?string $shippingLastName = null;
public ?string $shippingCompany = null;
public ?string $shippingAddress1 = null;
public ?string $shippingAddress2 = null;
public ?string $shippingCity = null;
public ?string $shippingState = null;
public ?string $shippingPostcode = null;
public ?string $shippingCountry = null;
public ?\DateTimeInterface $lastActive = null;
}
56 changes: 56 additions & 0 deletions src/Bridge/Repository/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Williarin\WordpressInterop\Bridge\Repository;

use DateTimeInterface;
use Williarin\WordpressInterop\Bridge\Entity\User;
use Williarin\WordpressInterop\Criteria\Operand;

/**
* @method User find(int $id)
* @method User findOneByUserLogin(string|Operand $newValue, array $orderBy = null)
* @method User findOneByUserNicename(string|Operand $newValue, array $orderBy = null)
* @method User findOneByUserEmail(string|Operand $newValue, array $orderBy = null)
* @method User findOneByUserUrl(string|Operand $newValue, array $orderBy = null)
* @method User findOneByUserRegistered(DateTimeInterface|Operand $newValue, array $orderBy = null)
* @method User findOneByUserStatus(int|Operand $newValue, array $orderBy = null)
* @method User findOneByDisplayName(string|Operand $newValue, array $orderBy = null)
* @method User[] findByUserLogin(string|Operand $newValue, array $orderBy = null, ?int $limit = null, int $offset = null)
* @method User[] findByUserNicename(string|Operand $newValue, array $orderBy = null, ?int $limit = null, int $offset = null)
* @method User[] findByUserEmail(string|Operand $newValue, array $orderBy = null, ?int $limit = null, int $offset = null)
* @method User[] findByUserUrl(string|Operand $newValue, array $orderBy = null, ?int $limit = null, int $offset = null)
* @method User[] findByUserRegistered(DateTimeInterface|Operand $newValue, array $orderBy = null, ?int $limit = null, int $offset = null)
* @method User[] findByUserStatus(int|Operand $newValue, array $orderBy = null, ?int $limit = null, int $offset = null)
* @method User[] findByDisplayName(string|Operand $newValue, array $orderBy = null, ?int $limit = null, int $offset = null)
* @method bool updateUserLogin(int $id, string|Operand $newValue)
* @method bool updateUserNicename(int $id, string|Operand $newValue)
* @method bool updateUserEmail(int $id, string|Operand $newValue)
* @method bool updateUserUrl(int $id, string|Operand $newValue)
* @method bool updateUserRegistered(int $id, DateTimeInterface|Operand $newValue)
* @method bool updateUserStatus(int $id, int|Operand $newValue)
* @method bool updateDisplayName(int $id, string|Operand $newValue)
*/
class UserRepository extends AbstractEntityRepository
{
protected const TABLE_NAME = 'users';
protected const TABLE_META_NAME = 'usermeta';
protected const TABLE_IDENTIFIER = 'id';
protected const TABLE_META_IDENTIFIER = 'user_id';
protected const FALLBACK_ENTITY = User::class;

protected const MAPPED_FIELDS = [
'billing_address_1' => 'billing_address_1',
'billing_address_2' => 'billing_address_2',
'shipping_address_1' => 'shipping_address_1',
'shipping_address_2' => 'shipping_address_2',
'wp_capabilities' => 'capabilities',
'wp_last_active' => 'last_active',
];

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

declare(strict_types=1);

namespace Williarin\WordpressInterop\Test\Bridge\Repository;

use Williarin\WordpressInterop\Bridge\Entity\User;
use Williarin\WordpressInterop\Bridge\Repository\UserRepository;
use Williarin\WordpressInterop\Exception\EntityNotFoundException;
use Williarin\WordpressInterop\Test\TestCase;

class UserRepositoryTest extends TestCase
{
private UserRepository $repository;

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

public function testFindReturnsCorrectUser(): void
{
$user = $this->repository->find(3);
self::assertInstanceOf(User::class, $user);

$actual = $this->serializer->normalize($user);
unset($actual['user_registered'], $actual['last_update']);

self::assertEquals([
'id' => 3,
'user_login' => 'justin',
'user_nicename' => 'justin',
'user_email' => '[email protected]',
'user_url' => '',
'user_status' => 0,
'display_name' => 'justin',
'nickname' => 'justin',
'first_name' => '',
'last_name' => '',
'description' => '',
'locale' => '',
'capabilities' => [
'data' => [
'customer' => true,
],
],
'billing_first_name' => 'Justin',
'billing_last_name' => 'Hills',
'billing_company' => 'Google',
'billing_address1' => null,
'billing_address2' => null,
'billing_city' => 'Dallas',
'billing_state' => 'Texas',
'billing_postcode' => '75204',
'billing_country' => 'United States',
'billing_email' => '[email protected]',
'billing_phone' => '214-927-9108',
'shipping_first_name' => 'Justin',
'shipping_last_name' => 'Hills',
'shipping_company' => 'Google',
'shipping_address1' => null,
'shipping_address2' => null,
'shipping_city' => 'Dallas',
'shipping_state' => 'Texas',
'shipping_postcode' => '75204',
'shipping_country' => 'United States',
'last_active' => null,
], $actual);
}

public function testFindThrowsExceptionIfNotFound(): void
{
$this->expectException(EntityNotFoundException::class);
$this->repository->find(150);
}

public function testFindAllReturnsCorrectNumberOfUsers(): void
{
$users = $this->repository->findAll();
self::assertContainsOnlyInstancesOf(User::class, $users);
self::assertCount(4, $users);
}

public function testFindOneBy(): void
{
$user = $this->repository->findOneByDisplayName('Shop Manager');
self::assertSame(2, $user->id);
}

public function testUpdateField(): void
{
$this->repository->updateDisplayName(4, 'Elon Musk');
$user = $this->repository->find(4);
self::assertSame('Elon Musk', $user->displayName);
}

public function testFindOneByEavAttribute(): void
{
$user = $this->repository->findOneByShippingState('North Dakota');
self::assertSame(4, $user->id);
}
}

0 comments on commit 32716d5

Please sign in to comment.