|
| 1 | +<?php namespace DustinGraham\ReactMysql\Tests; |
| 2 | + |
| 3 | +use DustinGraham\ReactMysql\Command; |
| 4 | +use DustinGraham\ReactMysql\ConnectionFactory; |
| 5 | + |
| 6 | +class CommandTest extends TestCase |
| 7 | +{ |
| 8 | + public function testComplexBind() |
| 9 | + { |
| 10 | + $command = new Command(" |
| 11 | + INSERT INTO simple_table ( |
| 12 | + `id`, |
| 13 | + `name`, |
| 14 | + `value`, |
| 15 | + `created_at` |
| 16 | + ) VALUES ( |
| 17 | + :id, |
| 18 | + :name, |
| 19 | + :num, |
| 20 | + :datetime |
| 21 | + ); |
| 22 | + ", [ |
| 23 | + ':id' => null, |
| 24 | + ':name' => 'John\'s Name', |
| 25 | + ':num' => 7, |
| 26 | + ':datetime' => 'NOW()', |
| 27 | + ]); |
| 28 | + |
| 29 | + $connection = ConnectionFactory::createConnection(); |
| 30 | + |
| 31 | + $query = $command->getPreparedQuery($connection); |
| 32 | + |
| 33 | + $this->assertStringEqualsIgnoreSpacing( |
| 34 | + "INSERT INTO simple_table ( `id`, `name`, `value`, `created_at` ) VALUES ( NULL, 'John\'s Name', 7, NOW() );", |
| 35 | + $query |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public function testAssertStrings() |
| 40 | + { |
| 41 | + $this->assertStringEqualsIgnoreSpacing('yes no ', 'yes no'); |
| 42 | + } |
| 43 | + |
| 44 | + public function testSingleBind() |
| 45 | + { |
| 46 | + $command = new Command(" |
| 47 | + SELECT * FROM simple_table WHERE id = :id |
| 48 | + "); |
| 49 | + |
| 50 | + $command->bind(':id', 1); |
| 51 | + |
| 52 | + $connection = ConnectionFactory::createConnection(); |
| 53 | + |
| 54 | + $command->getPreparedQuery($connection); |
| 55 | + } |
| 56 | + |
| 57 | + public function testParameterReplacing() |
| 58 | + { |
| 59 | + $command = new Command; |
| 60 | + $command->sql = 'SELECT * FROM simple_table WHERE id = :id'; |
| 61 | + $command->bind(':id', 2); |
| 62 | + |
| 63 | + $connection = ConnectionFactory::createConnection(); |
| 64 | + |
| 65 | + $query = $command->getPreparedQuery($connection); |
| 66 | + |
| 67 | + $this->assertStringEqualsIgnoreSpacing( |
| 68 | + 'SELECT * FROM simple_table WHERE id = 2', |
| 69 | + $query |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * TODO: This test is still todo. |
| 75 | + * |
| 76 | + * @throws \Exception |
| 77 | + */ |
| 78 | + public function testParamCounting() |
| 79 | + { |
| 80 | + // Note: Used a comma rather than => so it was failing. |
| 81 | + // param count would detect this sooner. |
| 82 | + |
| 83 | + // Intentionally bad parameters to ensure check. |
| 84 | + $badParams = [':test', 1,]; |
| 85 | + // The programmer's intent was: |
| 86 | + // $goodParams = [ ':test' => 1, ] |
| 87 | + |
| 88 | + $command = new Command( |
| 89 | + 'SELECT * FROM simple_table WHERE id = :test', |
| 90 | + $badParams |
| 91 | + ); |
| 92 | + |
| 93 | + $connection = ConnectionFactory::createConnection(); |
| 94 | + |
| 95 | + $query = $command->getPreparedQuery($connection); |
| 96 | + |
| 97 | + // TODO: Here is the bad result, :test should have been 1 |
| 98 | + // TODO: GetPreparedQuery should error on param mismatch |
| 99 | + $this->assertStringEqualsIgnoreSpacing( |
| 100 | + 'SELECT * FROM simple_table WHERE id = :test', |
| 101 | + $query |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments