-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathDatabaseQueryTest.php
More file actions
1415 lines (1210 loc) · 42.8 KB
/
DatabaseQueryTest.php
File metadata and controls
1415 lines (1210 loc) · 42.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Database\Tests;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\DatabaseQuery;
use Joomla\Database\Exception\QueryTypeAlreadyDefinedException;
use Joomla\Database\Exception\UnknownTypeException;
use Joomla\Database\ParameterType;
use Joomla\Database\Tests\Stubs\TestDatabaseQuery;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Test class for Joomla\Database\DatabaseQuery
*/
class DatabaseQueryTest extends TestCase
{
/**
* Object being tested
*
* @var MockObject|DatabaseQuery
*/
private $query;
/**
* Mock database driver
*
* @var MockObject|DatabaseInterface
*/
private $db;
/**
* Sets up the fixture.
*
* This method is called before a test is executed.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->db = $this->createMock(DatabaseInterface::class);
$this->query = new TestDatabaseQuery($this->db);
}
/**
* @testdox The call method correctly creates and manages a CALL query element
*/
public function testCall()
{
$this->assertSame($this->query, $this->query->call('foo'), 'The query builder supports method chaining');
$this->query->call('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->call->getElements()
);
}
/**
* @testdox The call method raises an exception if changing the query type
*/
public function testCallChangeQueryType()
{
$this->expectException(QueryTypeAlreadyDefinedException::class);
$this->query->select('foo')
->from('bar')
->call('foo');
}
/**
* @testdox A string is cast as a character string for the driver
*/
public function testCastAs()
{
$this->assertSame('123', $this->query->castAs('CHAR', '123'));
}
/**
* @testdox The length param is ignored for castAs when the sql driver doesn't support it
*/
public function testCastAsLengthParamIgnoredWhenNotSupported()
{
$this->assertSame('123', $this->query->castAs('CHAR', '123', 2));
}
/**
* @testdox Test an unknown type case return an unknown type exception
*/
public function testCastAsWithUnknownType()
{
$this->expectException(UnknownTypeException::class);
$this->query->castAs('INT', '123');
}
/**
* Data provider for character length test cases
*
* @return array
*/
public static function dataCharLength(): array
{
return [
'field without comparison' => ['a.title', null, null, 'CHAR_LENGTH(a.title)'],
'field with comparison' => ['a.title', '!=', '0', 'CHAR_LENGTH(a.title) != 0'],
];
}
/**
* @testdox A SQL statement for checking the character length of a field is generated
*
* @param string $field A value.
* @param string|null $operator Comparison operator between charLength integer value and $condition
* @param string|null $condition Integer value to compare charLength with.
* @param string $expected The expected query string.
*/
#[DataProvider('dataCharLength')]
public function testCharLength(string $field, ?string $operator, ?string $condition, string $expected)
{
$this->assertSame(
$expected,
$this->query->charLength($field, $operator, $condition)
);
}
/**
* @testdox The columns method correctly creates and manages a list of columns
*/
public function testColumns()
{
$this->assertSame($this->query, $this->query->columns('foo'), 'The query builder supports method chaining');
$this->query->columns('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->columns->getElements()
);
}
/**
* Data provider for concatenate test cases
*
* @return array
*/
public static function dataConcatenate(): array
{
return [
'values without separator' => [['foo', 'bar'], null, 'CONCATENATE(foo || bar)'],
'values with separator' => [['foo', 'bar'], ' and ', "CONCATENATE(foo || ' and ' || bar)"],
];
}
/**
* @testdox A SQL statement for concatenating values is generated
*
* @param string[] $values An array of values to concatenate.
* @param string|null $separator As separator to place between each value.
* @param string $expected The expected query string.
*/
#[DataProvider('dataConcatenate')]
public function testConcatenate(array $values, ?string $separator, string $expected)
{
$this->db->expects($this->any())
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
return "'" . $text . "'";
});
$this->assertSame(
$expected,
$this->query->concatenate($values, $separator)
);
}
/**
* @testdox A SQL statement for the current timestamp is generated
*/
public function testCurrentTimestamp()
{
$this->assertSame(
'CURRENT_TIMESTAMP()',
$this->query->currentTimestamp()
);
}
/**
* Data provider for dateAdd test cases
*
* @return array
*/
public static function dataDateAdd(): array
{
return [
'date with positive interval' => ["'2019-10-13'", '1', 'DAY', "DATE_ADD('2019-10-13', INTERVAL 1 DAY)"],
'date with negative interval' => ["'2019-10-13'", '-1', 'DAY', "DATE_ADD('2019-10-13', INTERVAL -1 DAY)"],
];
}
/**
* @testdox A SQL statement for adding date values is generated
*
* @param string $date The db quoted string representation of the date to add to. May be date or datetime
* @param string $interval The string representation of the appropriate number of units
* @param string $datePart The part of the date to perform the addition on
* @param string $expected The expected query string.
*/
#[DataProvider('dataDateAdd')]
public function testDateAdd(string $date, string $interval, string $datePart, string $expected)
{
$this->assertSame(
$expected,
$this->query->dateAdd($date, $interval, $datePart)
);
}
/**
* @testdox The delete method correctly creates a DELETE query element without a table name
*/
public function testDeleteWithoutTable()
{
$this->assertSame($this->query, $this->query->delete(), 'The query builder supports method chaining');
$this->assertNotNull($this->query->delete);
$this->assertNull($this->query->from);
}
/**
* @testdox The delete method correctly creates a DELETE and FROM query element with a table name
*/
public function testDeleteWithTable()
{
$this->assertSame($this->query, $this->query->delete('#__content'), 'The query builder supports method chaining');
$this->assertNotNull($this->query->delete);
$this->assertNotNull($this->query->from);
}
/**
* @testdox The delete method raises an exception if changing the query type
*/
public function testDeleteChangeQueryType()
{
$this->expectException(QueryTypeAlreadyDefinedException::class);
$this->query->select('foo')
->from('bar')
->delete('foo');
}
/**
* @testdox The exec method correctly creates and manages a EXEC query element
*/
public function testExec()
{
$this->assertSame($this->query, $this->query->exec('foo'), 'The query builder supports method chaining');
$this->query->exec('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->exec->getElements()
);
}
/**
* @testdox The exec method raises an exception if changing the query type
*/
public function testExecChangeQueryType()
{
$this->expectException(QueryTypeAlreadyDefinedException::class);
$this->query->select('foo')
->from('bar')
->exec('foo');
}
/**
* @testdox A SQL statement for the MySQL find_in_set() function is generated
*/
public function testFindInSet()
{
$this->assertSame(
'',
$this->query->findInSet('foo', 'a.data')
);
}
/**
* @testdox The from method correctly creates and manages a FROM query element
*/
public function testFrom()
{
$this->assertSame($this->query, $this->query->from('foo'), 'The query builder supports method chaining');
$this->query->from('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->from->getElements()
);
}
/**
* @testdox The query can be aliased
*/
public function testAlias()
{
$this->assertSame($this->query, $this->query->alias('foo'), 'The query builder supports method chaining');
$this->assertSame(
'foo',
$this->query->alias
);
}
/**
* @testdox A SQL statement to extract the year from a date is generated
*/
public function testYear()
{
$this->assertSame(
'YEAR(a.created)',
$this->query->year('a.created')
);
}
/**
* @testdox A SQL statement to extract the month from a date is generated
*/
public function testMonth()
{
$this->assertSame(
'MONTH(a.created)',
$this->query->month('a.created')
);
}
/**
* @testdox A SQL statement to extract the day from a date is generated
*/
public function testDay()
{
$this->assertSame(
'DAY(a.created)',
$this->query->day('a.created')
);
}
/**
* @testdox A SQL statement to extract the hour from a date is generated
*/
public function testHour()
{
$this->assertSame(
'HOUR(a.created)',
$this->query->hour('a.created')
);
}
/**
* @testdox A SQL statement to extract the minute from a date is generated
*/
public function testMinute()
{
$this->assertSame(
'MINUTE(a.created)',
$this->query->minute('a.created')
);
}
/**
* @testdox A SQL statement to extract the second from a date is generated
*/
public function testSecond()
{
$this->assertSame(
'SECOND(a.created)',
$this->query->second('a.created')
);
}
/**
* @testdox The group method correctly creates and manages a GROUP BY query element
*/
public function testGroup()
{
$this->assertSame($this->query, $this->query->group('foo'), 'The query builder supports method chaining');
$this->query->group('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->group->getElements()
);
}
/**
* @testdox The having method correctly creates and manages a HAVING query element
*/
public function testHaving()
{
$this->assertSame($this->query, $this->query->having('foo'), 'The query builder supports method chaining');
$this->query->having('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->having->getElements()
);
}
/**
* @testdox The insert method correctly creates a INSERT query element
*/
public function testInsert()
{
$this->assertSame($this->query, $this->query->insert('foo'), 'The query builder supports method chaining');
$this->assertNotNull($this->query->insert);
}
/**
* @testdox The insert method raises an exception if changing the query type
*/
public function testInsertChangeQueryType()
{
$this->expectException(QueryTypeAlreadyDefinedException::class);
$this->query->select('foo')
->from('bar')
->insert('foo');
}
/**
* @testdox The join method correctly creates a JOIN query element
*/
public function testJoin()
{
$this->assertSame($this->query, $this->query->join('inner', 'foo'), 'The query builder supports method chaining');
$this->query->join('inner', 'bar');
$this->assertCount(
2,
$this->query->join
);
}
/**
* @testdox The innerJoin method correctly creates a INNER JOIN query element
*/
public function testInnerJoin()
{
$this->assertSame($this->query, $this->query->innerJoin('foo'), 'The query builder supports method chaining');
$this->query->innerJoin('bar');
$this->assertCount(
2,
$this->query->join
);
}
/**
* @testdox The outerJoin method correctly creates a OUTER JOIN query element
*/
public function testOuterJoin()
{
$this->assertSame($this->query, $this->query->outerJoin('foo'), 'The query builder supports method chaining');
$this->query->outerJoin('bar');
$this->assertCount(
2,
$this->query->join
);
}
/**
* @testdox The leftJoin method correctly creates a LEFT JOIN query element
*/
public function testLeftJoin()
{
$this->assertSame($this->query, $this->query->leftJoin('foo'), 'The query builder supports method chaining');
$this->query->leftJoin('bar');
$this->assertCount(
2,
$this->query->join
);
}
/**
* @testdox The rightJoin method correctly creates a RIGHT JOIN query element
*/
public function testRightJoin()
{
$this->assertSame($this->query, $this->query->rightJoin('foo'), 'The query builder supports method chaining');
$this->query->rightJoin('bar');
$this->assertCount(
2,
$this->query->join
);
}
/**
* @testdox A SQL statement to get the length of a field is generated
*/
public function testLength()
{
$this->assertSame(
'LENGTH(a.created)',
$this->query->length('a.created')
);
}
/**
* Data provider for null date test cases
*
* @return array
*/
public static function dataNullDate(): array
{
return [
'null date with quote' => [true, "'0000-00-00 00:00:00'"],
'null date without quote' => [false, '0000-00-00 00:00:00'],
];
}
/**
* @testdox The null date from the database driver is retrieved
*
* @param boolean $quoted Optionally wraps the null date in database quotes (true by default).
* @param string $expected The expected query string.
*/
#[DataProvider('dataNullDate')]
public function testNullDate(bool $quoted, string $expected)
{
$this->db->expects($this->once())
->method('getNullDate')
->willReturn('0000-00-00 00:00:00');
$this->db->expects($this->any())
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
return "'" . $text . "'";
});
$this->assertSame(
$expected,
$this->query->nullDate($quoted)
);
}
/**
* @testdox The null date cannot be retrieved from the database driver if no driver is present
*/
public function testNullDateException()
{
$this->expectException(\RuntimeException::class);
$query = new TestDatabaseQuery();
$query->nullDate();
}
/**
* @testdox A SQL statement to determine if a field contains a null date is generated when the query has no known null dates
*/
public function testIsNullDatetimeNoDates()
{
$this->assertSame(
'a.created IS NULL',
$this->query->isNullDatetime('a.created')
);
}
/**
* @testdox A SQL statement to determine if a field contains a null date is generated when the query has known null dates
*/
public function testIsNullDatetimeWithDates()
{
$this->db->expects($this->any())
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
foreach ($text as $k => $v) {
$text[$k] = "'" . $v . "'";
}
return $text;
});
$query = new class ($this->db) extends DatabaseQuery {
protected $nullDatetimeList = ['0000-00-00 00:00:00', '1000-01-01 00:00:00'];
public function groupConcat($expression, $separator = ',')
{
return '';
}
public function processLimit($query, $limit, $offset = 0)
{
return $query;
}
};
$this->assertSame(
"(a.created IN ('0000-00-00 00:00:00', '1000-01-01 00:00:00') OR a.created IS NULL)",
$query->isNullDatetime('a.created')
);
}
/**
* @testdox A SQL statement to determine if a field contains a null date cannot be retrieved from the database driver if no driver is present
*/
public function testIsNullDatetimeException()
{
$this->expectException(\RuntimeException::class);
$query = new TestDatabaseQuery();
$query->isNullDatetime('a.created');
}
/**
* @testdox The order method correctly creates and manages a ORDER BY query element
*/
public function testOrder()
{
$this->assertSame($this->query, $this->query->order('foo'), 'The query builder supports method chaining');
$this->query->order('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->order->getElements()
);
}
/**
* @testdox A string can be quoted
*/
public function testQuote()
{
$this->db->expects($this->any())
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
return "'" . $text . "'";
});
$this->assertSame(
"'foo'",
$this->query->quote('foo')
);
}
/**
* @testdox A string cannot be quoted if no database driver is present
*/
public function testQuoteException()
{
$this->expectException(\RuntimeException::class);
$query = new TestDatabaseQuery();
$query->quote('foo');
}
/**
* @testdox A string can be quoted as a field identifier
*/
public function testQuoteName()
{
$this->db->expects($this->any())
->method('quoteName')
->willReturnCallback(function ($text, $escape = true) {
return "`" . $text . "`";
});
$this->assertSame(
"`foo`",
$this->query->quoteName('foo')
);
}
/**
* @testdox A string cannot be quoted as a field identifier if no database driver is present
*/
public function testQuoteNameException()
{
$this->expectException(\RuntimeException::class);
$query = new TestDatabaseQuery();
$query->quoteName('foo');
}
/**
* @testdox A SQL statement to get a random floating point value is generated
*/
public function testRand()
{
$this->assertSame(
'',
$this->query->rand()
);
}
/**
* @testdox A SQL statement to prepend a string with a regex operator is generated
*/
public function testRegexp()
{
$this->assertSame(
' foo',
$this->query->regexp('foo')
);
}
/**
* @testdox The select method correctly creates and manages a SELECT query element
*/
public function testSelect()
{
$this->assertSame($this->query, $this->query->select('foo'), 'The query builder supports method chaining');
$this->query->select('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->select->getElements()
);
}
/**
* @testdox The select method raises an exception if changing the query type
*/
public function testSelectChangeQueryType()
{
$this->expectException(QueryTypeAlreadyDefinedException::class);
$this->query->delete('foo')
->select('foo');
}
/**
* @testdox The set method correctly creates and manages a SET query element
*/
public function testSet()
{
$this->assertSame($this->query, $this->query->set('foo'), 'The query builder supports method chaining');
$this->query->set('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->set->getElements()
);
}
/**
* @testdox The setLimit method correctly manages the limit and offset for a query
*/
public function testSetLimit()
{
$this->assertSame($this->query, $this->query->setLimit(10, 25), 'The query builder supports method chaining');
$this->assertSame(
10,
$this->query->limit
);
$this->assertSame(
25,
$this->query->offset
);
}
/**
* @testdox The setQuery method correctly manages an injected SQL query
*/
public function testSetQuery()
{
$query = 'SELECT foo FROM bar';
$this->assertSame($this->query, $this->query->setQuery($query), 'The query builder supports method chaining');
$this->assertSame(
$query,
$this->query->sql
);
}
/**
* @testdox The update method correctly creates a UPDATE query element
*/
public function testUpdate()
{
$this->assertSame($this->query, $this->query->update('foo'), 'The query builder supports method chaining');
$this->assertNotNull($this->query->update);
}
/**
* @testdox The update method raises an exception if changing the query type
*/
public function testUpdateChangeQueryType()
{
$this->expectException(QueryTypeAlreadyDefinedException::class);
$this->query->select('foo')
->from('bar')
->update('foo');
}
/**
* @testdox The values method correctly creates and manages a list of values
*/
public function testValues()
{
$this->assertSame($this->query, $this->query->values('foo'), 'The query builder supports method chaining');
$this->query->values('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->values->getElements()
);
}
/**
* @testdox The where method correctly creates and manages a WHERE query element
*/
public function testWhere()
{
$this->assertSame($this->query, $this->query->where('foo'), 'The query builder supports method chaining');
$this->query->where('bar');
$this->assertSame(
['foo', 'bar'],
$this->query->where->getElements()
);
}
/**
* @testdox The whereIn method correctly creates and manages a WHERE query element with parameter binding
*/
public function testWhereIn()
{
$this->assertSame($this->query, $this->query->whereIn('foo', [1, 2]), 'The query builder supports method chaining');
$this->query->whereIn('bar', [3, 4]);
$this->assertSame(
['foo IN (:preparedArray1,:preparedArray2)', 'bar IN (:preparedArray3,:preparedArray4)'],
$this->query->where->getElements()
);
}
/**
* @testdox The whereNotIn method correctly creates and manages a WHERE query element with parameter binding
*/
public function testWhereNotIn()
{
$this->assertSame($this->query, $this->query->whereNotIn('foo', [1, 2]), 'The query builder supports method chaining');
$this->query->whereNotIn('bar', [3, 4]);
$this->assertSame(
['foo NOT IN (:preparedArray1,:preparedArray2)', 'bar NOT IN (:preparedArray3,:preparedArray4)'],
$this->query->where->getElements()
);
}
/**
* @testdox The extendWhere method correctly overrides a WHERE query element
*/
public function testExtendWhere()
{
$this->query->where('foo');
$this->assertSame($this->query, $this->query->extendWhere('OR', 'bar'), 'The query builder supports method chaining');
$this->assertCount(
2,
$this->query->where->getElements()
);
}
/**
* @testdox The orWhere method correctly overrides a WHERE query element
*/
public function testOrWhere()
{
$this->query->where('foo');
$this->assertSame($this->query, $this->query->orWhere('bar'), 'The query builder supports method chaining');
$this->assertCount(
2,
$this->query->where->getElements()
);
}
/**
* @testdox The andWhere method correctly overrides a WHERE query element
*/
public function testAndWhere()
{
$this->query->where('foo');
$this->assertSame($this->query, $this->query->andWhere('bar'), 'The query builder supports method chaining');
$this->assertCount(
2,
$this->query->where->getElements()
);
}
/**
* Data provider for bind test cases
*
* @return array
*/
public static function dataBind(): array
{
return [
'string field' => ['foo', 'bar', ParameterType::STRING, [
'foo' => (object) ['value' => 'bar', 'dataType' => 'string', 'length' => 0, 'driverOptions' => []],
]],
'numeric field' => ['foo', 42, ParameterType::INTEGER, [
'foo' => (object) ['value' => 42, 'dataType' => 'int', 'length' => 0, 'driverOptions' => []],
]],
'numeric key' => [1, 'bar', ParameterType::STRING, [
1 => (object) ['value' => 'bar', 'dataType' => 'string', 'length' => 0, 'driverOptions' => []],
]],
'array of data' => [[1, 'foo'], [42, 'bar'], [ParameterType::INTEGER, ParameterType::STRING], [
1 => (object) ['value' => 42, 'dataType' => 'int', 'length' => 0, 'driverOptions' => []],
'foo' => (object) ['value' => 'bar', 'dataType' => 'string', 'length' => 0, 'driverOptions' => []],
]],
'key array, single data value' => [[1, 2, 3], 'bar', ParameterType::STRING, [
1 => (object) ['value' => 'bar', 'dataType' => 'string', 'length' => 0, 'driverOptions' => []],
2 => (object) ['value' => 'bar', 'dataType' => 'string', 'length' => 0, 'driverOptions' => []],
3 => (object) ['value' => 'bar', 'dataType' => 'string', 'length' => 0, 'driverOptions' => []],
]],
];
}
/**
* @testdox The bind method records a bound parameter for the query
*
* @param array|string|integer $key The key that will be used in your SQL query to reference the value. Usually of
* the form ':key', but can also be an integer.
* @param mixed $value The value that will be bound. It can be an array, in this case it has to be
* same length of $key; The value is passed by reference to support output
* parameters such as those possible with stored procedures.
* @param array|string $dataType Constant corresponding to a SQL datatype. It can be an array, in this case it
* has to be same length of $key
* @param array $expected The expected structure of `$bounded`
*/
#[DataProvider('dataBind')]
public function testBind($key, $value, $dataType, $expected)
{
$this->assertSame($this->query, $this->query->bind($key, $value, $dataType), 'The query builder supports method chaining');
$this->assertEquals(
$expected,
$this->query->bounded
);
}
/**
* @testdox The bind method records a bound parameter for the query
*
* @param array|string|integer $key The key that will be used in your SQL query to reference the value. Usually of
* the form ':key', but can also be an integer.
* @param mixed $value The value that will be bound. It can be an array, in this case it has to be
* same length of $key; The value is passed by reference to support output
* parameters such as those possible with stored procedures.
* @param array|string $dataType Constant corresponding to a SQL datatype. It can be an array, in this case it
* has to be same length of $key
* @param array $expected The expected structure of `$bounded`
*/
public function testBindValue()
{
$this->assertSame($this->query, $this->query->bindValue(':bindValue', 'JustValueNoReference', ParameterType::STRING), 'The query builder supports method chaining');
$this->assertEquals(
[
':bindValue' => (object) ['value' => 'JustValueNoReference', 'dataType' => 'string', 'length' => 0, 'driverOptions' => []],
],
$this->query->bounded
);
}
/**
* @testdox The bind method does not record bound parameters when the keys and values are an unbalanced number of items
*/
public function testBindUnbalancedKeyValue()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Array length of $key and $value are not equal');
$keys = [1, 2, 3];
$values = ['bar'];
$dataTypes = [ParameterType::STRING, ParameterType::STRING, ParameterType::STRING];