-
Notifications
You must be signed in to change notification settings - Fork 418
/
ModelCriteriaTest.php
2548 lines (2221 loc) · 117 KB
/
ModelCriteriaTest.php
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
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreDataPopulator.php';
/**
* Test class for ModelCriteria.
*
* @author Francois Zaninotto
* @version $Id$
* @package runtime.query
*/
class ModelCriteriaTest extends BookstoreTestBase
{
protected function assertCriteriaTranslation($criteria, $expectedSql, $expectedParams, $message = '')
{
$params = array();
$result = BasePeer::createSelectSql($criteria, $params);
$this->assertEquals($expectedSql, $result, $message);
$this->assertEquals($expectedParams, $params, $message);
}
public function testGetModelName()
{
$c = new ModelCriteria('bookstore', 'Book');
$this->assertEquals('Book', $c->getModelName(), 'getModelName() returns the name of the class associated to the model class');
}
public function testGetModelPeerName()
{
$c = new ModelCriteria('bookstore', 'Book');
$this->assertEquals('BookPeer', $c->getModelPeerName(), 'getModelPeerName() returns the name of the Peer class associated to the model class');
}
public function testFormatter()
{
$c = new ModelCriteria('bookstore', 'Book');
$this->assertTrue($c->getFormatter() instanceof PropelFormatter, 'getFormatter() returns a PropelFormatter instance');
$c = new ModelCriteria('bookstore', 'Book');
$c->setFormatter(ModelCriteria::FORMAT_STATEMENT);
$this->assertTrue($c->getFormatter() instanceof PropelStatementFormatter, 'setFormatter() accepts the name of a PropelFormatter class');
try {
$c->setFormatter('Book');
$this->fail('setFormatter() throws an exception when passed the name of a class not extending PropelFormatter');
} catch (PropelException $e) {
$this->assertTrue(true, 'setFormatter() throws an exception when passed the name of a class not extending PropelFormatter');
}
$c = new ModelCriteria('bookstore', 'Book');
$formatter = new PropelStatementFormatter();
$c->setFormatter($formatter);
$this->assertTrue($c->getFormatter() instanceof PropelStatementFormatter, 'setFormatter() accepts a PropelFormatter instance');
try {
$formatter = new Book();
$c->setFormatter($formatter);
$this->fail('setFormatter() throws an exception when passed an object not extending PropelFormatter');
} catch (PropelException $e) {
$this->assertTrue(true, 'setFormatter() throws an exception when passedan object not extending PropelFormatter');
}
}
public static function conditionsForTestReplaceNames()
{
return array(
array('Book.Title = ?', 'Title', 'book.TITLE = ?'), // basic case
array('Book.Title=?', 'Title', 'book.TITLE=?'), // without spaces
array('Book.Id<= ?', 'Id', 'book.ID<= ?'), // with non-equal comparator
array('Book.AuthorId LIKE ?', 'AuthorId', 'book.AUTHOR_ID LIKE ?'), // with SQL keyword separator
array('(Book.AuthorId) LIKE ?', 'AuthorId', '(book.AUTHOR_ID) LIKE ?'), // with parenthesis
array('(Book.Id*1.5)=1', 'Id', '(book.ID*1.5)=1'), // ignore numbers
// dealing with quotes
array("Book.Id + ' ' + Book.AuthorId", null, "book.ID + ' ' + book.AUTHOR_ID"),
array("'Book.Id' + Book.AuthorId", null, "'Book.Id' + book.AUTHOR_ID"),
array("Book.Id + 'Book.AuthorId'", null, "book.ID + 'Book.AuthorId'"),
array('1=1', null, '1=1'), // with no name
array('', null, '') // with empty string
);
}
/**
* @dataProvider conditionsForTestReplaceNames
*/
public function testReplaceNames($origClause, $columnPhpName = false, $modifiedClause)
{
$c = new TestableModelCriteria('bookstore', 'Book');
$this->doTestReplaceNames($c, BookPeer::getTableMap(), $origClause, $columnPhpName = false, $modifiedClause);
}
public function doTestReplaceNames($c, $tableMap, $origClause, $columnPhpName = false, $modifiedClause)
{
$c->replaceNames($origClause);
$columns = $c->replacedColumns;
if ($columnPhpName) {
$this->assertEquals(array($tableMap->getColumnByPhpName($columnPhpName)), $columns);
}
$this->assertEquals($modifiedClause, $origClause);
}
public static function conditionsForTestReplaceMultipleNames()
{
return array(
array('(Book.Id+Book.Id)=1', array('Id', 'Id'), '(book.ID+book.ID)=1'), // match multiple names
array('CONCAT(Book.Title,"Book.Id")= ?', array('Title', 'Id'), 'CONCAT(book.TITLE,"Book.Id")= ?'), // ignore names in strings
array('CONCAT(Book.Title," Book.Id ")= ?', array('Title', 'Id'), 'CONCAT(book.TITLE," Book.Id ")= ?'), // ignore names in strings
array('MATCH (Book.Title,Book.ISBN) AGAINST (?)', array('Title', 'ISBN'), 'MATCH (book.TITLE,book.ISBN) AGAINST (?)'),
);
}
/**
* @dataProvider conditionsForTestReplaceMultipleNames
*/
public function testReplaceMultipleNames($origClause, $expectedColumns, $modifiedClause)
{
$c = new TestableModelCriteria('bookstore', 'Book');
$c->replaceNames($origClause);
$foundColumns = $c->replacedColumns;
foreach ($foundColumns as $column) {
$expectedColumn = BookPeer::getTableMap()->getColumnByPhpName(array_shift($expectedColumns));
$this->assertEquals($expectedColumn, $column);
}
$this->assertEquals($modifiedClause, $origClause);
}
public function testTableAlias()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->setModelAlias('b');
$c->where('b.Title = ?', 'foo');
$sql = "SELECT FROM `book` WHERE book.TITLE = :p1";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'setModelAlias() allows the definition of the alias after constrution');
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->where('b.Title = ?', 'foo');
$sql = "SELECT FROM `book` WHERE book.TITLE = :p1";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'A ModelCriteria accepts a model name with an alias');
}
public function testTrueTableAlias()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->setModelAlias('b', true);
$c->where('b.Title = ?', 'foo');
$c->join('b.Author a');
$c->where('a.FirstName = ?', 'john');
$sql = "SELECT FROM `book` `b` INNER JOIN `author` `a` ON (b.AUTHOR_ID=a.ID) WHERE b.TITLE = :p1 AND a.FIRST_NAME = :p2";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
array('table' => 'author', 'column' => 'FIRST_NAME', 'value' => 'john'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'setModelAlias() allows the definition of a true SQL alias after constrution');
}
public function testCondition()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->condition('cond1', 'Book.Title <> ?', 'foo');
$c->condition('cond2', 'Book.Title like ?', '%bar%');
$c->combine(array('cond1', 'cond2'), 'or');
$sql = "SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
array('table' => 'book', 'column' => 'TITLE', 'value' => '%bar%'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'condition() can store condition for later combination');
}
public function testConditionCustomOperator()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->withColumn('SUBSTRING(Book.Title, 1, 4)', 'title_start');
$c->condition('cond1', 'Book.Title <> ?', 'foo');
$c->condition('cond2', 'title_start like ?', '%bar%', PDO::PARAM_STR);
$c->combine(array('cond1', 'cond2'), 'or');
$sql = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` WHERE (book.TITLE <> :p1 OR title_start like :p2)";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
array('table' => null, 'type' => PDO::PARAM_STR, 'value' => '%bar%'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'condition() accepts RAW sql parameters');
}
public static function conditionsForTestWhere()
{
return array(
array('Book.Title = ?', 'foo', 'book.TITLE = :p1', array(array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'))),
array('Book.AuthorId = ?', 12, 'book.AUTHOR_ID = :p1', array(array('table' => 'book', 'column' => 'AUTHOR_ID', 'value' => 12))),
array('Book.AuthorId IS NULL', null, 'book.AUTHOR_ID IS NULL', array()),
array('Book.Id BETWEEN ? AND ?', array(3, 4), 'book.ID BETWEEN :p1 AND :p2', array(array('table' => 'book', 'column' => 'ID', 'value' => 3), array('table' => 'book', 'column' => 'ID', 'value' => 4))),
array('Book.Id betWEen ? and ?', array(3, 4), 'book.ID betWEen :p1 and :p2', array(array('table' => 'book', 'column' => 'ID', 'value' => 3), array('table' => 'book', 'column' => 'ID', 'value' => 4))),
array('Book.Id IN ?', array(1, 2, 3), 'book.ID IN (:p1,:p2,:p3)', array(array('table' => 'book', 'column' => 'ID', 'value' => 1), array('table' => 'book', 'column' => 'ID', 'value' => 2), array('table' => 'book', 'column' => 'ID', 'value' => 3))),
array('Book.Id in ?', array(1, 2, 3), 'book.ID in (:p1,:p2,:p3)', array(array('table' => 'book', 'column' => 'ID', 'value' => 1), array('table' => 'book', 'column' => 'ID', 'value' => 2), array('table' => 'book', 'column' => 'ID', 'value' => 3))),
array('Book.Id IN ?', array(), '1<>1', array()),
array('Book.Id not in ?', array(), '1=1', array()),
array('UPPER(Book.Title) = ?', 'foo', 'UPPER(book.TITLE) = :p1', array(array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'))),
array('MATCH (Book.Title,Book.ISBN) AGAINST (?)', 'foo', 'MATCH (book.TITLE,book.ISBN) AGAINST (:p1)', array(array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'))),
);
}
/**
* @dataProvider conditionsForTestWhere
*/
public function testWhere($clause, $value, $sql, $params)
{
$c = new ModelCriteria('bookstore', 'Book');
$c->where($clause, $value);
$sql = 'SELECT FROM `book` WHERE ' . $sql;
$this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts a string clause');
}
public function testWhereUsesDefaultOperator()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->where('Book.Id = ?', 12);
$c->_or();
$c->where('Book.Title = ?', 'foo');
$sql = 'SELECT FROM `book` WHERE (book.ID = :p1 OR book.TITLE = :p2)';
$params = array(
array('table' => 'book', 'column' => 'ID', 'value' => '12'),
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'where() uses the default operator');
}
public function testWhereTwiceSameColumn()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->where('Book.Id IN ?', array(1, 2, 3));
$c->where('Book.Id <> ?', 5);
$params = array(
array('table' => 'book', 'column' => 'ID', 'value' => '1'),
array('table' => 'book', 'column' => 'ID', 'value' => '2'),
array('table' => 'book', 'column' => 'ID', 'value' => '3'),
array('table' => 'book', 'column' => 'ID', 'value' => '5'),
);
$sql = 'SELECT FROM `book` WHERE (book.ID IN (:p1,:p2,:p3) AND book.ID <> :p4)';
$this->assertCriteriaTranslation($c, $sql, $params, 'where() adds clauses on the same column correctly');
}
public function testWhereConditions()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->condition('cond1', 'Book.Title <> ?', 'foo');
$c->condition('cond2', 'Book.Title like ?', '%bar%');
$c->where(array('cond1', 'cond2'));
$sql = "SELECT FROM `book` WHERE (book.TITLE <> :p1 AND book.TITLE like :p2)";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
array('table' => 'book', 'column' => 'TITLE', 'value' => '%bar%'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts an array of named conditions');
$c = new ModelCriteria('bookstore', 'Book');
$c->condition('cond1', 'Book.Title <> ?', 'foo');
$c->condition('cond2', 'Book.Title like ?', '%bar%');
$c->where(array('cond1', 'cond2'), Criteria::LOGICAL_OR);
$sql = "SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)";
$this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts an array of named conditions with operator');
}
public function testWhereNoReplacement()
{
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->where('b.Title = ?', 'foo');
$c->where('1=1');
$sql = "SELECT FROM `book` WHERE book.TITLE = :p1 AND 1=1";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'where() results in a Criteria::CUSTOM if no column name is matched');
$c = new ModelCriteria('bookstore', 'Book');
try {
$c->where('b.Title = ?', 'foo');
$this->fail('where() throws an exception when it finds a ? but cannot determine a column');
} catch (PropelException $e) {
$this->assertTrue(true, 'where() throws an exception when it finds a ? but cannot determine a column');
}
}
public function testWhereFunction()
{
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->where('UPPER(b.Title) = ?', 'foo');
$sql = "SELECT FROM `book` WHERE UPPER(book.TITLE) = :p1";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts a complex calculation');
}
public function testWhereTypeValue()
{
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->where('LOCATE(\'foo\', b.Title) = ?', true, PDO::PARAM_BOOL);
$sql = "SELECT FROM `book` WHERE LOCATE('foo', book.TITLE) = :p1";
$params = array(
array('table' => null, 'type' => PDO::PARAM_BOOL, 'value' => true),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts a complex calculation');
$c->find($this->con);
$expected = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE LOCATE('foo', book.TITLE) = true";
$this->assertEquals($expected, $this->con->getLastExecutedQuery());
}
public function testOrWhere()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->where('Book.Title <> ?', 'foo');
$c->orWhere('Book.Title like ?', '%bar%');
$sql = "SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
array('table' => 'book', 'column' => 'TITLE', 'value' => '%bar%'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'orWhere() combines the clause with the previous one using OR');
}
public function testOrWhereConditions()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->where('Book.Id = ?', 12);
$c->condition('cond1', 'Book.Title <> ?', 'foo');
$c->condition('cond2', 'Book.Title like ?', '%bar%');
$c->orWhere(array('cond1', 'cond2'));
$sql = "SELECT FROM `book` WHERE (book.ID = :p1 OR (book.TITLE <> :p2 AND book.TITLE like :p3))";
$params = array(
array('table' => 'book', 'column' => 'ID', 'value' => 12),
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
array('table' => 'book', 'column' => 'TITLE', 'value' => '%bar%'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'orWhere() accepts an array of named conditions');
$c = new ModelCriteria('bookstore', 'Book');
$c->where('Book.Id = ?', 12);
$c->condition('cond1', 'Book.Title <> ?', 'foo');
$c->condition('cond2', 'Book.Title like ?', '%bar%');
$c->orWhere(array('cond1', 'cond2'), Criteria::LOGICAL_OR);
$sql = "SELECT FROM `book` WHERE (book.ID = :p1 OR (book.TITLE <> :p2 OR book.TITLE like :p3))";
$this->assertCriteriaTranslation($c, $sql, $params, 'orWhere() accepts an array of named conditions with operator');
}
public function testMixedCriteria()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->where('Book.Title = ?', 'foo');
$c->add(BookPeer::ID, array(1, 2), Criteria::IN);
$sql = 'SELECT FROM `book` WHERE book.TITLE = :p1 AND book.ID IN (:p2,:p3)';
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
array('table' => 'book', 'column' => 'ID', 'value' => 1),
array('table' => 'book', 'column' => 'ID', 'value' => 2)
);
$this->assertCriteriaTranslation($c, $sql, $params, 'ModelCriteria accepts Criteria operators');
}
public function testFilterBy()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->filterBy('Title', 'foo');
$sql = 'SELECT FROM `book` WHERE book.TITLE=:p1';
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'filterBy() accepts a simple column name');
$c = new ModelCriteria('bookstore', 'Book');
$c->filterBy('Title', 'foo', Criteria::NOT_EQUAL);
$sql = 'SELECT FROM `book` WHERE book.TITLE<>:p1';
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'filterBy() accepts a sicustom comparator');
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->filterBy('Title', 'foo');
$sql = 'SELECT FROM `book` WHERE book.TITLE=:p1';
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'filterBy() accepts a simple column name, even if initialized with an alias');
}
public function testHaving()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->having('Book.Title <> ?', 'foo');
$sql = "SELECT FROM HAVING book.TITLE <> :p1";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'having() accepts a string clause');
}
public function testHavingConditions()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->condition('cond1', 'Book.Title <> ?', 'foo');
$c->condition('cond2', 'Book.Title like ?', '%bar%');
$c->having(array('cond1', 'cond2'));
$sql = "SELECT FROM HAVING (book.TITLE <> :p1 AND book.TITLE like :p2)";
$params = array(
array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'),
array('table' => 'book', 'column' => 'TITLE', 'value' => '%bar%'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'having() accepts an array of named conditions');
$c = new ModelCriteria('bookstore', 'Book');
$c->condition('cond1', 'Book.Title <> ?', 'foo');
$c->condition('cond2', 'Book.Title like ?', '%bar%');
$c->having(array('cond1', 'cond2'), Criteria::LOGICAL_OR);
$sql = "SELECT FROM HAVING (book.TITLE <> :p1 OR book.TITLE like :p2)";
$this->assertCriteriaTranslation($c, $sql, $params, 'having() accepts an array of named conditions with an operator');
}
public function testHavingWithColumn()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->withColumn('SUBSTRING(Book.Title, 1, 4)', 'title_start');
$c->having('title_start = ?', 'foo', PDO::PARAM_STR);
$sql = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` HAVING title_start = :p1';
$params = array(
array('table' => null, 'type' => 2, 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'having() accepts a string clause');
$c->find($this->con);
$expected = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` HAVING title_start = \'foo\'';
$this->assertEquals($expected, $this->con->getLastExecutedQuery());
}
public function testOrderBy()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->orderBy('Book.Title');
$sql = 'SELECT FROM ORDER BY book.TITLE ASC';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'orderBy() accepts a column name and adds an ORDER BY clause');
$c = new ModelCriteria('bookstore', 'Book');
$c->orderBy('Book.Title', 'desc');
$sql = 'SELECT FROM ORDER BY book.TITLE DESC';
$this->assertCriteriaTranslation($c, $sql, $params, 'orderBy() accepts an order parameter');
$c = new ModelCriteria('bookstore', 'Book');
try {
$c->orderBy('Book.Foo');
$this->fail('orderBy() throws an exception when called with an unknown column name');
} catch (PropelException $e) {
$this->assertTrue(true, 'orderBy() throws an exception when called with an unknown column name');
}
$c = new ModelCriteria('bookstore', 'Book');
try {
$c->orderBy('Book.Title', 'foo');
$this->fail('orderBy() throws an exception when called with an unknown order');
} catch (PropelException $e) {
$this->assertTrue(true, 'orderBy() throws an exception when called with an unknown order');
}
}
public function testOrderBySimpleColumn()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->orderBy('Title');
$sql = 'SELECT FROM ORDER BY book.TITLE ASC';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'orderBy() accepts a simple column name and adds an ORDER BY clause');
}
public function testOrderByAlias()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->addAsColumn('t', BookPeer::TITLE);
$c->orderBy('t');
$sql = 'SELECT book.TITLE AS t FROM ORDER BY t ASC';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'orderBy() accepts a column alias and adds an ORDER BY clause');
}
public function testGroupBy()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->groupBy('Book.AuthorId');
$sql = 'SELECT FROM GROUP BY book.AUTHOR_ID';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'groupBy() accepts a column name and adds a GROUP BY clause');
$c = new ModelCriteria('bookstore', 'Book');
try {
$c->groupBy('Book.Foo');
$this->fail('groupBy() throws an exception when called with an unknown column name');
} catch (PropelException $e) {
$this->assertTrue(true, 'groupBy() throws an exception when called with an unknown column name');
}
}
public function testGroupBySimpleColumn()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->groupBy('AuthorId');
$sql = 'SELECT FROM GROUP BY book.AUTHOR_ID';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'groupBy() accepts a simple column name and adds a GROUP BY clause');
}
public function testGroupByAlias()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->addAsColumn('t', BookPeer::TITLE);
$c->groupBy('t');
$sql = 'SELECT book.TITLE AS t FROM GROUP BY t';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'groupBy() accepts a column alias and adds a GROUP BY clause');
}
/**
* @expectedException PropelException
*/
public function testGroupByClassThrowsExceptionOnUnknownClass()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->groupByClass('Author');
}
public function testGroupByClass()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->groupByClass('Book');
$sql = 'SELECT FROM GROUP BY book.ID,book.TITLE,book.ISBN,book.PRICE,book.PUBLISHER_ID,book.AUTHOR_ID';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'groupByClass() accepts a class name and adds a GROUP BY clause for all columns of the class');
}
public function testGroupByClassAlias()
{
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->groupByClass('b');
$sql = 'SELECT FROM GROUP BY book.ID,book.TITLE,book.ISBN,book.PRICE,book.PUBLISHER_ID,book.AUTHOR_ID';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'groupByClass() accepts a class alias and adds a GROUP BY clause for all columns of the class');
}
public function testGroupByClassTrueAlias()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->setModelAlias('b', true);
$c->groupByClass('b');
$sql = 'SELECT FROM GROUP BY b.ID,b.TITLE,b.ISBN,b.PRICE,b.PUBLISHER_ID,b.AUTHOR_ID';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'groupByClass() accepts a true class alias and adds a GROUP BY clause for all columns of the class');
}
public function testGroupByClassJoinedModel()
{
$c = new ModelCriteria('bookstore', 'Author');
$c->join('Author.Book');
$c->groupByClass('Book');
$sql = 'SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID) GROUP BY book.ID,book.TITLE,book.ISBN,book.PRICE,book.PUBLISHER_ID,book.AUTHOR_ID';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'groupByClass() accepts the class name of a joined model');
}
public function testGroupByClassJoinedModelWithAlias()
{
$c = new ModelCriteria('bookstore', 'Author');
$c->join('Author.Book b');
$c->groupByClass('b');
$sql = 'SELECT FROM `author` INNER JOIN `book` `b` ON (author.ID=b.AUTHOR_ID) GROUP BY b.ID,b.TITLE,b.ISBN,b.PRICE,b.PUBLISHER_ID,b.AUTHOR_ID';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'groupByClass() accepts the alias of a joined model');
}
public function testDistinct()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->distinct();
$sql = 'SELECT DISTINCT FROM ';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'distinct() adds a DISTINCT clause');
}
public function testLimit()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->limit(10);
$sql = 'SELECT FROM LIMIT 10';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'limit() adds a LIMIT clause');
}
public function testOffset()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->limit(50);
$c->offset(10);
$sql = 'SELECT FROM LIMIT 10, 50';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'offset() adds an OFFSET clause');
}
public function testAddJoin()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->addJoin(BookPeer::AUTHOR_ID, AuthorPeer::ID);
$c->addJoin(BookPeer::PUBLISHER_ID, PublisherPeer::ID);
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) INNER JOIN `publisher` ON (book.PUBLISHER_ID=publisher.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'addJoin() works the same as in Criteria');
}
public function testJoin()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author');
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() uses a relation to guess the columns');
$c = new ModelCriteria('bookstore', 'Book');
try {
$c->join('Book.Foo');
$this->fail('join() throws an exception when called with a non-existing relation');
} catch (PropelException $e) {
$this->assertTrue(true, 'join() throws an exception when called with a non-existing relation');
}
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author');
$c->where('Author.FirstName = ?', 'Leo');
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = :p1';
$params = array(
array('table' => 'author', 'column' => 'FIRST_NAME', 'value' => 'Leo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'join() uses a relation to guess the columns');
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Author');
$c->where('Author.FirstName = ?', 'Leo');
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = :p1';
$params = array(
array('table' => 'author', 'column' => 'FIRST_NAME', 'value' => 'Leo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'join() uses the current model name when given a simple relation name');
}
public function testJoinQuery()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
BookstoreDataPopulator::depopulate($con);
BookstoreDataPopulator::populate($con);
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author');
$c->where('Author.FirstName = ?', 'Neal');
$books = BookPeer::doSelect($c);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() issues a real JOIN query');
$this->assertEquals(1, count($books), 'join() issues a real JOIN query');
}
public function testJoinRelationName()
{
$c = new ModelCriteria('bookstore', 'BookstoreEmployee');
$c->join('BookstoreEmployee.Supervisor');
$sql = 'SELECT FROM INNER JOIN `bookstore_employee` ON (bookstore_employee.SUPERVISOR_ID=bookstore_employee.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() uses relation names as defined in schema.xml');
}
public function testJoinComposite()
{
$c = new ModelCriteria('bookstore', 'ReaderFavorite');
$c->join('ReaderFavorite.BookOpinion');
$sql = 'SELECT FROM `reader_favorite` INNER JOIN `book_opinion` ON (reader_favorite.BOOK_ID=book_opinion.BOOK_ID AND reader_favorite.READER_ID=book_opinion.READER_ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() knows how to create a JOIN clause for relationships with composite fkeys');
}
public function testJoinType()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author');
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() adds an INNER JOIN by default');
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::INNER_JOIN);
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() adds an INNER JOIN by default');
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::LEFT_JOIN);
$sql = 'SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() can add a LEFT JOIN');
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::RIGHT_JOIN);
$sql = 'SELECT FROM `book` RIGHT JOIN `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() can add a RIGHT JOIN');
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', 'incorrect join');
$sql = 'SELECT FROM `book` incorrect join `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() accepts any join string');
}
public function testJoinDirection()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author');
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() adds a JOIN clause correctly for many to one relationship');
$c = new ModelCriteria('bookstore', 'Author');
$c->join('Author.Book');
$sql = 'SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() adds a JOIN clause correctly for one to many relationship');
$c = new ModelCriteria('bookstore', 'BookstoreEmployee');
$c->join('BookstoreEmployee.BookstoreEmployeeAccount');
$sql = 'SELECT FROM `bookstore_employee` INNER JOIN `bookstore_employee_account` ON (bookstore_employee.ID=bookstore_employee_account.EMPLOYEE_ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() adds a JOIN clause correctly for one to one relationship');
$c = new ModelCriteria('bookstore', 'BookstoreEmployeeAccount');
$c->join('BookstoreEmployeeAccount.BookstoreEmployee');
$sql = 'SELECT FROM `bookstore_employee_account` INNER JOIN `bookstore_employee` ON (bookstore_employee_account.EMPLOYEE_ID=bookstore_employee.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() adds a JOIN clause correctly for one to one relationship');
}
public function testJoinSeveral()
{
$c = new ModelCriteria('bookstore', 'Author');
$c->join('Author.Book');
$c->join('Book.Publisher');
$c->where('Publisher.Name = ?', 'foo');
$sql = 'SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID) INNER JOIN `publisher` ON (book.PUBLISHER_ID=publisher.ID) WHERE publisher.NAME = :p1';
$params = array(
array('table' => 'publisher', 'column' => 'NAME', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'join() can guess relationships from related tables');
}
public function testJoinAlias()
{
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->join('b.Author');
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation on main alias');
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->join('Author');
$sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() can use a simple relation name when the model has an alias');
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author a');
$sql = 'SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation alias');
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->join('b.Author a');
$sql = 'SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation alias on main alias');
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->join('b.Author a');
$c->where('a.FirstName = ?', 'Leo');
$sql = 'SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = :p1';
$params = array(
array('table' => 'author', 'column' => 'FIRST_NAME', 'value' => 'Leo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'join() allows the use of relation alias in where()');
$c = new ModelCriteria('bookstore', 'Author', 'a');
$c->join('a.Book b');
$c->join('b.Publisher p');
$c->where('p.Name = ?', 'foo');
$sql = 'SELECT FROM `author` INNER JOIN `book` `b` ON (author.ID=b.AUTHOR_ID) INNER JOIN `publisher` `p` ON (b.PUBLISHER_ID=p.ID) WHERE p.NAME = :p1';
$params = array(
array('table' => 'publisher', 'column' => 'NAME', 'value' => 'foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'join() allows the use of relation alias in further join()');
}
public function testJoinTrueTableAlias()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->setModelAlias('b', true);
$c->join('b.Author');
$sql = 'SELECT FROM `book` `b` INNER JOIN `author` ON (b.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation on true table alias');
$c = new ModelCriteria('bookstore', 'Book');
$c->setModelAlias('b', true);
$c->join('Author');
$sql = 'SELECT FROM `book` `b` INNER JOIN `author` ON (b.AUTHOR_ID=author.ID)';
$params = array();
$this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation without alias name on true table alias');
}
public function testJoinOnSameTable()
{
$c = new ModelCriteria('bookstore', 'BookstoreEmployee', 'be');
$c->join('be.Supervisor sup');
$c->join('sup.Subordinate sub');
$c->where('sub.Name = ?', 'Foo');
$sql = 'SELECT FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.SUPERVISOR_ID=sup.ID) INNER JOIN `bookstore_employee` `sub` ON (sup.ID=sub.SUPERVISOR_ID) WHERE sub.NAME = :p1';
$params = array(
array('table' => 'bookstore_employee', 'column' => 'NAME', 'value' => 'Foo'),
);
$this->assertCriteriaTranslation($c, $sql, $params, 'join() allows two joins on the same table thanks to aliases');
}
public function testJoinAliasQuery()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book', 'b');
$c->join('b.Author a');
$c->where('a.FirstName = ?', 'Leo');
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo'";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in where()');
$c = new ModelCriteria('bookstore', 'BookstoreEmployee', 'be');
$c->join('be.Supervisor sup');
$c->join('sup.Subordinate sub');
$c->where('sub.Name = ?', 'Foo');
$employees = BookstoreEmployeePeer::doSelect($c, $con);
$expectedSQL = "SELECT bookstore_employee.ID, bookstore_employee.CLASS_KEY, bookstore_employee.NAME, bookstore_employee.JOB_TITLE, bookstore_employee.SUPERVISOR_ID FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.SUPERVISOR_ID=sup.ID) INNER JOIN `bookstore_employee` `sub` ON (sup.ID=sub.SUPERVISOR_ID) WHERE sub.NAME = 'Foo'";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in further joins()');
}
public function testAddJoinConditionSimple()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::INNER_JOIN);
$c->addJoinCondition('Author', 'Book.Title IS NOT NULL');
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE IS NOT NULL)";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions');
}
public function testAddJoinConditionBinding()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::INNER_JOIN);
$c->addJoinCondition('Author', 'Book.Title = ?', 'foo');
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE = 'foo')";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions with values to bind');
}
public function testAddJoinConditionSeveral()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::INNER_JOIN);
$c->addJoinCondition('Author', 'Book.Title = ?', 'foo');
$c->addJoinCondition('Author', 'Book.ISBN IS NOT NULL');
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON ((book.AUTHOR_ID=author.ID AND book.TITLE = 'foo') AND book.ISBN IS NOT NULL)";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of several custom conditions');
}
public function testAddJoinConditionBindingAndWhere()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book');
$c->where('Book.Title LIKE ?', 'foo%');
$c->join('Book.Author', Criteria::INNER_JOIN);
$c->addJoinCondition('Author', 'Book.Title = ?', 'foo');
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE = 'foo') WHERE book.TITLE LIKE 'foo%'";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions with values and lives well with WHERE conditions');
}
public function testAddJoinConditionAlias()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author a', Criteria::INNER_JOIN);
$c->addJoinCondition('a', 'Book.Title IS NOT NULL');
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID AND book.TITLE IS NOT NULL)";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions even on aliased relations');
}
public function testAddJoinConditionOperator()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::INNER_JOIN);
$c->addJoinCondition('Author', 'Book.Title IS NOT NULL', null, Criteria::LOGICAL_OR);
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID OR book.TITLE IS NOT NULL)";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions with a custom operator');
}
public function testSetJoinConditionCriterion()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::INNER_JOIN);
$criterion = $c->getNewCriterion(BookPeer::TITLE, BookPeer::TITLE . ' = ' . AuthorPeer::FIRST_NAME, Criteria::CUSTOM);
$c->setJoinCondition('Author', $criterion);
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON book.TITLE = author.FIRST_NAME";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'setJoinCondition() can override a previous join condition with a Criterion');
}
public function testSetJoinConditionNamedCondition()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author', Criteria::INNER_JOIN);
$c->condition('cond1', 'Book.Title = Author.FirstName');
$c->setJoinCondition('Author', 'cond1');
$books = BookPeer::doSelect($c, $con);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON book.TITLE = author.FIRST_NAME";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'setJoinCondition() can override a previous join condition with a named condition');
}
public function testGetJoin()
{
$c = new ModelCriteria('bookstore', 'Book');
$c->join('Book.Author');
$joins = $c->getJoins();
$this->assertEquals($joins['Author'], $c->getJoin('Author'), "getJoin() returns a specific Join from the ModelCriteria");
}
public function testWith()
{
$c = new TestableModelCriteria('bookstore', 'Book');