|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bancer\NativeQueryMapper\Test\TestCase\Database; |
| 6 | + |
| 7 | +use Bancer\NativeQueryMapperTest\TestApp\Model\Entity\Article; |
| 8 | +use Bancer\NativeQueryMapperTest\TestApp\Model\Entity\User; |
| 9 | +use Bancer\NativeQueryMapperTest\TestApp\Model\Table\ArticlesTable; |
| 10 | +use Bancer\NativeQueryMapperTest\TestApp\Model\Table\UsersTable; |
| 11 | +use Bancer\NativeQueryMapper\Database\InPlaceholders; |
| 12 | +use Cake\ORM\Locator\LocatorAwareTrait; |
| 13 | +use InvalidArgumentException; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +class InPlaceholdersTest extends TestCase |
| 17 | +{ |
| 18 | + use LocatorAwareTrait; |
| 19 | + |
| 20 | + public function testConstructorEmptyPrefix(): void |
| 21 | + { |
| 22 | + static::expectException(InvalidArgumentException::class); |
| 23 | + static::expectExceptionMessage('IN() placeholders cannot be constructed with an empty prefix'); |
| 24 | + new InPlaceholders('', []); |
| 25 | + } |
| 26 | + |
| 27 | + public function testConstructorEmptyValues(): void |
| 28 | + { |
| 29 | + static::expectException(InvalidArgumentException::class); |
| 30 | + static::expectExceptionMessage('IN() placeholders cannot be constructed with an empty value list'); |
| 31 | + new InPlaceholders('status', []); |
| 32 | + } |
| 33 | + |
| 34 | + public function testToString(): void |
| 35 | + { |
| 36 | + $InPlaceholders = new InPlaceholders('id', [3, 5]); |
| 37 | + $expected = ':id_0, :id_1'; |
| 38 | + $actual = $InPlaceholders->__toString(); |
| 39 | + static::assertSame($expected, $actual); |
| 40 | + } |
| 41 | + |
| 42 | + public function testBindValuesToStatementInt(): void |
| 43 | + { |
| 44 | + $userIds = [2, 5]; |
| 45 | + $inPlaceholders = new InPlaceholders('user', $userIds); |
| 46 | + /** @var \Bancer\NativeQueryMapperTest\TestApp\Model\Table\ArticlesTable $ArticlesTable */ |
| 47 | + $ArticlesTable = $this->fetchTable(ArticlesTable::class); |
| 48 | + $stmt = $ArticlesTable->prepareNativeStatement(" |
| 49 | + SELECT |
| 50 | + id AS Articles__id, |
| 51 | + title AS Articles__title |
| 52 | + FROM articles AS a |
| 53 | + WHERE a.user_id IN($inPlaceholders) |
| 54 | + "); |
| 55 | + $inPlaceholders->bindValuesToStatement($stmt); |
| 56 | + $actual = $ArticlesTable->mapNativeStatement($stmt)->all(); |
| 57 | + static::assertCount(2, $actual); |
| 58 | + static::assertInstanceOf(Article::class, $actual[0]); |
| 59 | + $expected = [ |
| 60 | + 'id' => 2, |
| 61 | + 'title' => 'Article 2', |
| 62 | + ]; |
| 63 | + static::assertEquals($expected, $actual[0]->toArray()); |
| 64 | + $cakeEntities = $ArticlesTable->find() |
| 65 | + ->select(['id', 'title']) |
| 66 | + ->where(['user_id IN' => $userIds]) |
| 67 | + ->toArray(); |
| 68 | + static::assertEquals($cakeEntities, $actual); |
| 69 | + } |
| 70 | + |
| 71 | + public function testBindValuesToStatementStrings(): void |
| 72 | + { |
| 73 | + $users = ['bob', 'eve']; |
| 74 | + $inPlaceholders = new InPlaceholders('user', $users); |
| 75 | + /** @var \Bancer\NativeQueryMapperTest\TestApp\Model\Table\UsersTable $UsersTable */ |
| 76 | + $UsersTable = $this->fetchTable(UsersTable::class); |
| 77 | + $stmt = $UsersTable->prepareNativeStatement(" |
| 78 | + SELECT |
| 79 | + id AS Users__id, |
| 80 | + username AS Users__username |
| 81 | + FROM users AS u |
| 82 | + WHERE u.username IN($inPlaceholders) |
| 83 | + "); |
| 84 | + $inPlaceholders->bindValuesToStatement($stmt); |
| 85 | + $actual = $UsersTable->mapNativeStatement($stmt)->all(); |
| 86 | + static::assertCount(2, $actual); |
| 87 | + static::assertInstanceOf(User::class, $actual[0]); |
| 88 | + $expected = [ |
| 89 | + 'id' => 2, |
| 90 | + 'username' => 'bob', |
| 91 | + ]; |
| 92 | + static::assertEquals($expected, $actual[0]->toArray()); |
| 93 | + $cakeEntities = $UsersTable->find() |
| 94 | + ->select(['id', 'username']) |
| 95 | + ->where(['username IN' => $users]) |
| 96 | + ->toArray(); |
| 97 | + static::assertEquals($cakeEntities, $actual); |
| 98 | + } |
| 99 | +} |
0 commit comments