|
| 1 | +<?php |
| 2 | + |
| 3 | +use BlitzPHP\Database\Builder\BaseBuilder; |
| 4 | +use BlitzPHP\Database\Builder\BindingCollection; |
| 5 | +use BlitzPHP\Database\Spec\Mock\MockConnection; |
| 6 | + |
| 7 | +use function Kahlan\expect; |
| 8 | + |
| 9 | +describe("Database / Query Builder : Bindings", function() { |
| 10 | + |
| 11 | + beforeEach(function() { |
| 12 | + $this->builder = new BaseBuilder(new MockConnection([])); |
| 13 | + }); |
| 14 | + |
| 15 | + describe("BindingCollection simple", function() { |
| 16 | + it(": BindingCollection ajout simple", function() { |
| 17 | + $collection = new BindingCollection(); |
| 18 | + $collection->add('value1'); |
| 19 | + $collection->add(123); |
| 20 | + $collection->add(true); |
| 21 | + $collection->add(null); |
| 22 | + |
| 23 | + expect($collection->count())->toBe(4); |
| 24 | + expect($collection->getOrdered())->toBe(['value1', 123, true, null]); |
| 25 | + }); |
| 26 | + |
| 27 | + it(": BindingCollection ajout nommé", function() { |
| 28 | + $collection = new BindingCollection(); |
| 29 | + $collection->addNamed(':name', 'John'); |
| 30 | + $collection->addNamed(':age', 30); |
| 31 | + |
| 32 | + expect($collection->get('where', ':name'))->toBe('John'); |
| 33 | + expect($collection->get('where', ':age'))->toBe(30); |
| 34 | + }); |
| 35 | + |
| 36 | + it(": BindingCollection types", function() { |
| 37 | + $collection = new BindingCollection(); |
| 38 | + $collection->add('string'); |
| 39 | + $collection->add(123); |
| 40 | + $collection->add(true); |
| 41 | + $collection->add(null); |
| 42 | + |
| 43 | + $types = $collection->getTypesOrdered(); |
| 44 | + expect($types[0])->toBe(PDO::PARAM_STR); |
| 45 | + expect($types[1])->toBe(PDO::PARAM_INT); |
| 46 | + expect($types[2])->toBe(PDO::PARAM_BOOL); |
| 47 | + expect($types[3])->toBe(PDO::PARAM_NULL); |
| 48 | + }); |
| 49 | + |
| 50 | + it(": BindingCollection merge", function() { |
| 51 | + $col1 = new BindingCollection(); |
| 52 | + $col1->add('a')->add('b'); |
| 53 | + |
| 54 | + $col2 = new BindingCollection(); |
| 55 | + $col2->add('c')->add('d'); |
| 56 | + |
| 57 | + $col1->merge($col2); |
| 58 | + |
| 59 | + expect($col1->count())->toBe(4); |
| 60 | + expect($col1->getOrdered())->toBe(['a', 'b', 'c', 'd']); |
| 61 | + }); |
| 62 | + |
| 63 | + it(": BindingCollection clear", function() { |
| 64 | + $collection = new BindingCollection(); |
| 65 | + $collection->add('test'); |
| 66 | + expect($collection->isEmpty())->toBe(false); |
| 67 | + |
| 68 | + $collection->clear(); |
| 69 | + expect($collection->isEmpty())->toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it(": Les bindings sont correctement transmis dans la requête", function() { |
| 73 | + $builder = $this->builder->testMode() |
| 74 | + ->from('users') |
| 75 | + ->where('id', 5) |
| 76 | + ->where('name', 'John') |
| 77 | + ->whereIn('status', [1, 2, 3]); |
| 78 | + |
| 79 | + expect($builder->bindings->count())->toBe(5); |
| 80 | + expect($builder->bindings->getOrdered())->toBe([5, 'John', 1, 2, 3]); |
| 81 | + }); |
| 82 | + |
| 83 | + it(": Les bindings sont réinitialisés après exécution", function() { |
| 84 | + $builder = $this->builder->from('users')->where('id', 5); |
| 85 | + |
| 86 | + expect($builder->bindings->isEmpty())->toBe(false); |
| 87 | + |
| 88 | + try { |
| 89 | + $sql = $builder->get(); |
| 90 | + } catch(Exception) { |
| 91 | + // l'execution ne passera pas car on a pas de bd. |
| 92 | + // on veut juste se rassuer que les bindings sont reset |
| 93 | + expect($builder->bindings->isEmpty())->toBe(true); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it(": Les bindings sont préservés dans les sous-requêtes", function() { |
| 98 | + $builder = $this->builder->testMode() |
| 99 | + ->from('users') |
| 100 | + ->whereIn('id', function($q) { |
| 101 | + $q->from('profiles') |
| 102 | + ->select('user_id') |
| 103 | + ->where('active', 1) |
| 104 | + ->where('points >', 100); |
| 105 | + }); |
| 106 | + |
| 107 | + expect($builder->bindings->count())->toBe(2); |
| 108 | + expect($builder->bindings->getOrdered())->toBe([1, 100]); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("BindingCollection avec types", function() { |
| 113 | + it(": getOrdered avec types spécifiques", function() { |
| 114 | + $bindings = new BindingCollection(); |
| 115 | + $bindings->add('value1', 'values'); |
| 116 | + $bindings->add(5, 'where'); |
| 117 | + $bindings->add('join_cond', 'join'); |
| 118 | + |
| 119 | + expect($bindings->getOrdered(['values', 'where']))->toBe(['value1', 5]); |
| 120 | + expect($bindings->getOrdered(['where', 'values']))->toBe([5, 'value1']); |
| 121 | + expect($bindings->getOrdered())->toHaveLength(3); |
| 122 | + }); |
| 123 | + |
| 124 | + it(": getOrdered ignore les types vides", function() { |
| 125 | + $bindings = new BindingCollection(); |
| 126 | + $bindings->add('value1', 'values'); |
| 127 | + |
| 128 | + expect($bindings->getOrdered(['values', 'where', 'having']))->toBe(['value1']); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe("BaseBuilder::getBindings", function() { |
| 133 | + it(": UPDATE - valeurs avant where", function() { |
| 134 | + $builder = $this->builder->table('users') |
| 135 | + ->where('id', 5) |
| 136 | + ->where('active', 1) |
| 137 | + ->set(['name' => 'John']) |
| 138 | + ->pending() // pour eviter l'execution |
| 139 | + ->update(); |
| 140 | + |
| 141 | + expect($builder->getBindings())->toBe(['John', 5, 1]); |
| 142 | + }); |
| 143 | + |
| 144 | + it(": INSERT - seulement valeurs", function() { |
| 145 | + $builder = $this->builder->table('users') |
| 146 | + ->set(['name' => 'John', 'age' => 30]) |
| 147 | + ->pending() // pour eviter l'execution |
| 148 | + ->insert(); |
| 149 | + |
| 150 | + expect($builder->getBindings())->toBe(['John', 30]); |
| 151 | + }); |
| 152 | + |
| 153 | + it(": SELECT - where dans l'ordre", function() { |
| 154 | + $builder = $this->builder->table('users') |
| 155 | + ->where('id', 5) |
| 156 | + ->where('name', 'John') |
| 157 | + ->orderBy('created_at'); |
| 158 | + |
| 159 | + expect($builder->getBindings())->toBe([5, 'John']); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments