tbolier /
php-rethink-ql
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | |||
| 4 | namespace TBolier\RethinkQL\IntegrationTest; |
||
| 5 | |||
| 6 | use ArrayObject; |
||
| 7 | use Mockery; |
||
| 8 | use PHPUnit\Framework\TestCase; |
||
| 9 | use Symfony\Component\Serializer\Encoder\JsonEncoder; |
||
| 10 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
||
| 11 | use Symfony\Component\Serializer\Serializer; |
||
| 12 | use TBolier\RethinkQL\Connection\Connection; |
||
| 13 | use TBolier\RethinkQL\Connection\ConnectionInterface; |
||
| 14 | use TBolier\RethinkQL\Connection\Options; |
||
| 15 | use TBolier\RethinkQL\Connection\Socket\Exception; |
||
| 16 | use TBolier\RethinkQL\Connection\Socket\Handshake; |
||
| 17 | use TBolier\RethinkQL\Connection\Socket\Socket; |
||
| 18 | use TBolier\RethinkQL\Response\ResponseInterface; |
||
| 19 | use TBolier\RethinkQL\Rethink; |
||
| 20 | use TBolier\RethinkQL\RethinkInterface; |
||
| 21 | use TBolier\RethinkQL\Serializer\QueryNormalizer; |
||
| 22 | use TBolier\RethinkQL\Types\VersionDummy\Version; |
||
| 23 | |||
| 24 | abstract class AbstractTestCase extends TestCase |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var RethinkInterface |
||
| 28 | */ |
||
| 29 | private $r; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var ConnectionInterface[] |
||
| 33 | */ |
||
| 34 | private $connections = []; |
||
| 35 | |||
| 36 | protected function setUp() |
||
| 37 | { |
||
| 38 | Mockery::getConfiguration()->allowMockingNonExistentMethods(false); |
||
| 39 | |||
| 40 | parent::setUp(); |
||
| 41 | } |
||
| 42 | |||
| 43 | protected function r() |
||
| 44 | { |
||
| 45 | if ($this->r !== null) { |
||
| 46 | return $this->r; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** @var ConnectionInterface $connection */ |
||
| 50 | $connection = $this->createConnection('phpunit_default')->connect(); |
||
| 51 | $connection->connect()->use('test'); |
||
| 52 | |||
| 53 | $this->r = new Rethink($connection); |
||
| 54 | |||
| 55 | /** @var ResponseInterface $res */ |
||
| 56 | $res = $this->r->dbList()->run(); |
||
| 57 | if (\is_array($res->getData()) && !\in_array('test', $res->getData(), true)) { |
||
| 58 | $this->r->dbCreate('test')->run(); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $this->r; |
||
| 62 | } |
||
| 63 | |||
| 64 | protected function tearDown() |
||
| 65 | { |
||
| 66 | if ($this->r === null) { |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** @var ResponseInterface $res */ |
||
| 71 | $res = $this->r->dbList()->run(); |
||
| 72 | if (\is_array($res->getData()) && \in_array('test', $res->getData(), true)) { |
||
| 73 | $this->r->dbDrop('test')->run(); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $name |
||
| 79 | * @return ConnectionInterface |
||
| 80 | * @throws Exception |
||
| 81 | */ |
||
| 82 | protected function createConnection(string $name): ConnectionInterface |
||
| 83 | { |
||
| 84 | $options = new Options(PHPUNIT_CONNECTIONS[$name]); |
||
| 85 | |||
| 86 | $connection = new Connection( |
||
| 87 | function() use ($options) { |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 88 | return new Socket( |
||
| 89 | $options |
||
| 90 | ); |
||
| 91 | }, |
||
| 92 | new Handshake($options->getUser(), $options->getPassword(), Version::V1_0), |
||
| 93 | $options->getDbName(), |
||
| 94 | new Serializer( |
||
| 95 | [new QueryNormalizer()], |
||
| 96 | [new JsonEncoder()] |
||
| 97 | ), |
||
| 98 | new Serializer( |
||
| 99 | [new ObjectNormalizer()], |
||
| 100 | [new JsonEncoder()] |
||
| 101 | ) |
||
| 102 | ); |
||
| 103 | |||
| 104 | $this->connections[] = $connection; |
||
| 105 | |||
| 106 | return $connection; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function __destruct() |
||
| 110 | { |
||
| 111 | foreach ($this->connections as $connection) { |
||
| 112 | $connection->close(); |
||
| 113 | } |
||
| 114 | |||
| 115 | Mockery::close(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $status |
||
| 120 | * @param $data |
||
| 121 | * @throws \Exception |
||
| 122 | */ |
||
| 123 | protected function assertObStatus($status, $data) |
||
| 124 | { |
||
| 125 | $res = []; |
||
| 126 | $statuses = [ |
||
| 127 | 'tables_created', |
||
| 128 | 'tables_dropped', |
||
| 129 | 'unchanged', |
||
| 130 | 'skipped', |
||
| 131 | 'replaced', |
||
| 132 | 'inserted', |
||
| 133 | 'errors', |
||
| 134 | 'deleted', |
||
| 135 | ]; |
||
| 136 | $data = new ArrayObject($data); |
||
| 137 | |||
| 138 | foreach ($statuses as $s) { |
||
| 139 | $status[$s] = $status[$s] ?? 0; |
||
| 140 | } |
||
| 141 | |||
| 142 | $data->setFlags($data::ARRAY_AS_PROPS); |
||
| 143 | |||
| 144 | foreach ($statuses as $s) { |
||
| 145 | $res[$s] = $data[$s] ?? 0; |
||
| 146 | } |
||
| 147 | |||
| 148 | $this->assertEquals($status, $res); |
||
| 149 | } |
||
| 150 | } |
||
| 151 |