forked from slimphp/Slim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlimTest.php
1777 lines (1676 loc) · 54.6 KB
/
SlimTest.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
/**
* Slim - a micro PHP 5 framework
*
* @author Josh Lockhart <[email protected]>
* @copyright 2011 Josh Lockhart
* @link http://www.slimframework.com
* @license http://www.slimframework.com/license
* @version 1.5.0
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
set_include_path(dirname(__FILE__) . '/../' . PATH_SEPARATOR . get_include_path());
//Start session before PHPUnit sends output. This only prevents us from using
//the default Slim Session cookie store.
session_start();
require_once 'Slim/Slim.php';
//Prepare mock HTTP request
$_SERVER['REDIRECT_STATUS'] = "200";
$_SERVER['HTTP_HOST'] = "slim";
$_SERVER['HTTP_CONNECTION'] = "keep-alive";
$_SERVER['HTTP_CACHE_CONTROL'] = "max-age=0";
$_SERVER['HTTP_ACCEPT'] = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$_SERVER['HTTP_USER_AGENT'] = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3";
$_SERVER['HTTP_ACCEPT_ENCODING'] = "gzip,deflate,sdch";
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en-US,en;q=0.8";
$_SERVER['HTTP_ACCEPT_CHARSET'] = "ISO-8859-1,utf-8;q=0.7,*;q=0.3";
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = "Sun, 03 Oct 2010 17:00:52 -0400";
$_SERVER['HTTP_IF_NONE_MATCH'] = '"abc123"';
$_SERVER['HTTP_COOKIE'] = 'foo=bar; foo2=bar2';
$_SERVER['PATH'] = "/usr/bin:/bin:/usr/sbin:/sbin";
$_SERVER['SERVER_SIGNATURE'] = "";
$_SERVER['SERVER_SOFTWARE'] = "Apache";
$_SERVER['SERVER_NAME'] = "slim";
$_SERVER['SERVER_ADDR'] = "127.0.0.1";
$_SERVER['SERVER_PORT'] = "80";
$_SERVER['REMOTE_ADDR'] = "127.0.0.1";
$_SERVER['DOCUMENT_ROOT'] = '/home/account/public';
$_SERVER['SERVER_ADMIN'] = "[email protected]";
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
$_SERVER['REMOTE_PORT'] = "55426";
$_SERVER['REDIRECT_URL'] = "/";
$_SERVER['GATEWAY_INTERFACE'] = "CGI/1.1";
$_SERVER['SERVER_PROTOCOL'] = "HTTP/1.1";
$_SERVER['REQUEST_METHOD'] = "GET";
$_SERVER['QUERY_STRING'] = "";
$_SERVER['REQUEST_URI'] = "/";
$_SERVER['SCRIPT_NAME'] = '/bootstrap.php';
$_SERVER['PHP_SELF'] = '/bootstrap.php';
$_SERVER['REQUEST_TIME'] = "1285647051";
$_SERVER['argv'] = array();
$_SERVER['argc'] = 0;
//Register non-Slim autoloader
function customAutoLoader( $class ) {
$file = rtrim(dirname(__FILE__), '/') . '/' . $class . '.php';
if ( file_exists($file) ) {
require $file;
} else {
return;
}
}
spl_autoload_register('customAutoLoader');
//Mock custom view
class CustomView extends Slim_View {
function render($template) { echo "Custom view"; }
}
//Mock custom Logger
class CustomLogger{
public function debug( $var ) {
print_r($var);
}
public function info( $var ) {
print_r($var);
}
public function warn( $var ) {
print_r($var);
}
public function error( $var ) {
print_r($var);
}
public function fatal( $var ) {
print_r($var);
}
}
class SlimTest extends PHPUnit_Framework_TestCase {
public function setUp() {
$_SERVER['REQUEST_METHOD'] = "GET";
$_ENV['SLIM_MODE'] = null;
$_COOKIE['foo'] = 'bar';
$_COOKIE['foo2'] = 'bar2';
$_SERVER['REQUEST_URI'] = "/";
}
/************************************************
* SLIM INITIALIZATION
************************************************/
/**
* Test Slim default View
*
* Pre-conditions:
* Slim app instantiated with default View;
*
* Post-conditions:
* Slim app has default View of class Slim_View;
*/
public function testSlimDefaultView() {
$app = new Slim();
$this->assertTrue($app->view() instanceof Slim_View);
}
/**
* Test Slim custom View
*
* Pre-conditions:
* Case A: Slim app instantiated with View setting as string;
* Case B: Slim app instantiated with View as instance;
*
* Post-conditions:
* Case A: View is instance of CustomView
* Case B: View is instance of CustomView
*/
public function testSlimInitWithCustomView(){
//Case A
$app1 = new Slim(array('view' => 'CustomView'));
$this->assertTrue($app1->view() instanceof CustomView);
//Case B
$app2 = new Slim(array('view' => new CustomView()));
$this->assertTrue($app2->view() instanceOf CustomView);
}
/**
* Test Slim default Logger
*
* Pre-conditions:
* Slim app instantiated;
* Logging enabled;
* Default Logger used;
*
* Post-conditions:
* Slim app has default Logger of class Slim_Logger;
*/
public function testSlimInitWithDefaultLogger() {
$app = new Slim(array(
'log.path' => dirname(__FILE__) . '/logs',
'log.enable' => true
));
$this->assertTrue($app->getLog()->getLogger() instanceof Slim_Logger);
}
/**
* Test Slim custom Logger
*
* Pre-conditions:
* Slim app instantiated;
* Logging enabled;
* Custom Logger used;
*
* Post-conditions:
* Slim app has custom Logger of class CustomLogger;
*/
public function testSlimInitWithCustomLogger() {
$app = new Slim(array(
'log.enable' => true,
'log.logger' => new CustomLogger()
));
$this->assertTrue($app->getLog()->getLogger() instanceof CustomLogger);
}
/**
* Test Slim autoloader ignores non-Slim classes
*
* Pre-conditions:
* Instantiate a non-Slim class;
*
* Post-conditions:
* Slim autoloader returns without requiring a class file;
*/
public function testSlimAutoloaderIgnoresNonSlimClass() {
$foo = new Foo();
}
/**
* Test Slim get instance
*
* Pre-conditions:
* Slim app instantiated;
* Set app name;
*
* Post-conditions:
* A default app exists;
* The instantiated app is returned by the name assigned to it;
*/
public function testGetInstance() {
$app = new Slim();
$app->setName('foo');
$this->assertTrue(Slim::getInstance() instanceof Slim);
$this->assertEquals('foo', $app->getName());
$this->assertSame($app, Slim::getInstance('foo'));
}
/**
* Test Slim does not affect default Response HTTP status
*
* Pre-conditions:
* Slim app instantiated;
* Case A: Use default settings;
* Case B: Set to "1.0";
*
* Post-conditions:
* Case A: Response HTTP version is "1.1";
* Case B: Response HTTP version is "1.0";
*/
public function testSlimSetsResponseHttpVersion() {
$app1 = new Slim();
$app2 = new Slim(array(
'http.version' => '1.0'
));
$this->assertEquals('1.1', $app1->response()->httpVersion());
$this->assertEquals('1.0', $app2->response()->httpVersion());
}
/************************************************
* SLIM SETTINGS
************************************************/
/**
* Test Slim mode with ENV[SLIM_MODE]
*
* Pre-conditions:
* SLIM_MODE environment variable set;
* Slim app instantiated with config mode;
*
* Post-conditions:
* Only the production configuration is called;
*/
public function testSlimModeEnvironment() {
$this->expectOutputString('production mode');
$_ENV['SLIM_MODE'] = 'production';
$app = new Slim(array(
'mode' => 'test'
));
$app->configureMode('test', function () {
echo "test mode";
});
$app->configureMode('production', function () {
echo "production mode";
});
}
/**
* Test Slim mode with Config
*
* Pre-conditions:
* ENV[SLIM_MODE] not set;
* Slim app instantiated with config mode;
*
* Post-conditions:
* Only the test configuration is called;
*/
public function testSlimModeConfig() {
$this->expectOutputString('test mode');
$app = new Slim(array(
'mode' => 'test'
));
$app->configureMode('test', function () {
echo "test mode";
});
$app->configureMode('production', function () {
echo "production mode";
});
}
/**
* Test Slim mode with default
*
* Pre-conditions:
* ENV[SLIM_MODE] not set;
* Slim app instantiated without config mode;
*
* Post-conditions:
* Only the development configuration is called;
*/
public function testSlimModeDefault() {
$this->expectOutputString('dev mode');
$app = new Slim();
$app->configureMode('development', function () {
echo "dev mode";
});
$app->configureMode('production', function () {
echo "production mode";
});
}
/**
* Test Slim Logging for given mode
*
* Pre-conditions:
* Slim app instantiated;
* Set custom Logger for current app mode;
*
* Post-conditions:
* Slim app Logger correct based on mode;
*/
public function testSlimLoggerInMode() {
$app = new Slim(array(
'mode' => 'test'
));
$app->configureMode('test', function () use ($app) {
$app->config(array(
'log.enable' => true,
'log.logger' => new CustomLogger()
));
});
$app->configureMode('development', function () use ($app) {
$app->config(array(
'log.enable' => true
));
});
$this->assertTrue($app->getLog()->getLogger() instanceof CustomLogger);
}
/**
* Test Slim defines one application setting
*
* Pre-conditions:
* Slim app instantiated;
* One configuration setting is set;
*
* Post-conditions:
* Configuration setting `foo` === `bar`;
*/
public function testSlimConfigSetsOneSetting(){
$app = new Slim();
$app->config('foo', 'bar');
$this->assertEquals('bar', $app->config('foo'));
}
/**
* Test Slim setting is NULL if non-existant
*
* Pre-conditions:
* Slim app instantiated;
* Fetch non-existing configuration setting;
*
* Post-conditions:
* NULL is returned for the value of the setting;
*/
public function testSlimConfigIfSettingDoesNotExist(){
$app = new Slim();
$this->assertNull($app->config('foo'));
}
/**
* Test Slim defines multiple settings with array
*
* Pre-conditions:
* Slim app instantiated;
* Batch-define multiple configuration settings with associative array;
*
* Post-conditions:
* Multiple settings are set correctly;
*/
public function testSlimCongfigurationWithArray(){
$app = new Slim();
$app->config(array(
'one' => 'A',
'two' => 'B',
'three' => 'C'
));
$this->assertEquals('A', $app->config('one'));
$this->assertEquals('B', $app->config('two'));
$this->assertEquals('C', $app->config('three'));
}
/************************************************
* SLIM ROUTING
************************************************/
/**
* Test Slim GET route
*
* Pre-conditions:
* Slim app instantiated;
* One GET route defined;
*
* Post-conditions:
* The GET route is returned;
* The GET route's pattern and callable are set correctly;
*/
public function testSlimGetRoute(){
$app = new Slim();
$callable = function () { echo "foo"; };
$route = $app->get('/foo/bar', $callable);
$this->assertEquals('/foo/bar', $route->getPattern());
$this->assertSame($callable, $route->getCallable());
}
/**
* Test Slim GET route with middleware
*
* Pre-conditions:
* Slim app instantiated and run;
* One GET route defined with middleware;
*
* Post-conditions:
* The GET route and its middleware are invoked in sequence;
*/
public function testSlimGetRouteWithMiddleware(){
$app = new Slim();
$mw1 = function () { echo "foo"; };
$mw2 = function () { echo "bar"; };
$callable = function () { echo "foo"; };
$app->get('/', $mw1, $mw2, $callable);
$this->expectOutputString('foobarfoo');
$app->run();
}
/**
* Test Slim sets POST route
*
* Pre-conditions:
* Slim app instantiated;
* One POST route defined;
*
* Post-conditions:
* The POST route is returned;
* The POST route's pattern and callable are set correctly;
*/
public function testSlimPostRoute(){
$app = new Slim();
$callable = function () { echo "foo"; };
$route = $app->post('/foo/bar', $callable);
$this->assertEquals('/foo/bar', $route->getPattern());
$this->assertSame($callable, $route->getCallable());
}
/**
* Test Slim POST route with middleware
*
* Pre-conditions:
* Slim app instantiated and run;
* One POST route defined with middleware;
*
* Post-conditions:
* The POST route and its middleware are invoked in sequence;
*/
public function testSlimPostRouteWithMiddleware(){
$_SERVER['REQUEST_METHOD'] = 'POST';
$app = new Slim();
$mw1 = function () { echo "foo"; };
$mw2 = function () { echo "bar"; };
$callable = function () { echo "foo"; };
$route = $app->post('/', $mw1, $mw2, $callable);
$this->expectOutputString('foobarfoo');
$app->run();
}
/**
* Test Slim sets PUT route
*
* Pre-conditions:
* Slim app instantiated;
* One PUT request defined;
*
* Post-conditions:
* The PUT route is returned;
* The PUT route's pattern and callable are set correctly;
*/
public function testSlimPutRoute(){
$app = new Slim();
$callable = function () { echo "foo"; };
$route = $app->put('/foo/bar', $callable);
$this->assertEquals('/foo/bar', $route->getPattern());
$this->assertSame($callable, $route->getCallable());
}
/**
* Test Slim PUT route with middleware
*
* Pre-conditions:
* Slim app instantiated and run;
* One PUT request defined with middleware;
*
* Post-conditions:
* The PUT route and its middleware are invoked in sequence;
*/
public function testSlimPutRouteWithMiddleware(){
$_SERVER['REQUEST_METHOD'] = 'PUT';
$app = new Slim();
$mw1 = function () { echo "foo"; };
$mw2 = function () { echo "bar"; };
$callable = function () { echo "foo"; };
$route = $app->put('/', $mw1, $mw2, $callable);
$this->expectOutputString('foobarfoo');
$app->run();
}
/**
* Test Slim sets DELETE route
*
* Pre-conditions:
* Slim app instantiated;
* One DELETE route defined;
*
* Post-conditions:
* The DELETE route is returned;
* The DELETE route's pattern and callable are set correctly;
*/
public function testSlimDeleteRoute(){
$app = new Slim();
$callable = function () { echo "foo"; };
$route = $app->delete('/foo/bar', $callable);
$this->assertEquals('/foo/bar', $route->getPattern());
$this->assertSame($callable, $route->getCallable());
}
/**
* Test Slim DELETE route with middleware
*
* Pre-conditions:
* Slim app instatiated and run;
* One DELETE route defined with middleware;
*
* Post-conditions:
* The DELETE route and its middleware are invoked in sequence;
*/
public function testSlimDeleteRouteWithMiddleware(){
$_SERVER['REQUEST_METHOD'] = 'DELETE';
$app = new Slim();
$mw1 = function () { echo "foo"; };
$mw2 = function () { echo "bar"; };
$callable = function () { echo "foo"; };
$route = $app->delete('/', $mw1, $mw2, $callable);
$this->expectOutputString('foobarfoo');
$app->run();
}
/**
* Test Slim sets DELETE route
*
* Pre-conditions:
* Slim app instantiated;
* One OPTIONS route defined;
*
* Post-conditions:
* The OPTIONS route is returned;
* The OPTIONS route's pattern and callable are set correctly;
*/
public function testSlimOptionsRoute(){
$app = new Slim();
$callable = function () { echo "foo"; };
$route = $app->options('/foo/bar', $callable);
$this->assertEquals('/foo/bar', $route->getPattern());
$this->assertSame($callable, $route->getCallable());
}
/**
* Test Slim DELETE route with middleware
*
* Pre-conditions:
* Slim app instatiated and run;
* One OPTIONS route defined with middleware;
*
* Post-conditions:
* The OPTIONS route and its middleware are invoked in sequence;
*/
public function testSlimOptionsRouteWithMiddleware(){
$_SERVER['REQUEST_METHOD'] = 'OPTIONS';
$app = new Slim();
$mw1 = function () { echo "foo"; };
$mw2 = function () { echo "bar"; };
$callable = function () { echo "foo"; };
$route = $app->options('/', $mw1, $mw2, $callable);
$this->expectOutputString('foobarfoo');
$app->run();
}
/**
* Test Slim routing and trailing slashes
*
* Pre-conditions:
* A route is defined that expects a trailing slash, but
* the resource URI does not have a trailing slash - but
* otherwise matches the route pattern.
*
* Post-conditions:
* Slim will send a 301 redirect response to the same
* resource URI but with a trailing slash.
*/
public function testRouteWithSlashAndUrlWithout() {
$_SERVER['REQUEST_URI'] = '/foo/bar/bob';
$this->expectOutputString('/foo/bar/bob/');
$app = new Slim();
$app->get('/foo/bar/:name/', function ($name) {});
$app->run();
$this->assertEquals(301, $app->response()->status());
$this->assertEquals('/foo/bar/bob/', $app->response()->header('Location'));
}
/**
* Test Slim routing and trailing slashes
*
* Pre-conditions:
* Slim app instantiated;
* Route defined that matches current HTTP request;
* Route does NOT expect trailing slash;
* HTTP request DOES have trailing slash;
*
* Post-conditions:
* Slim response status is 404;
*/
public function testRouteWithoutSlashAndUrlWith() {
$_SERVER['REQUEST_URI'] = '/foo/bar/bob/';
$this->expectOutputRegex('@404 Page Not Found@');
$app = new Slim();
$app->get('/foo/bar/:name', function ($name) {});
$app->run();
$this->assertEquals(404, $app->response()->status());
}
/**
* Test Slim routing with URL encoded characters
*
* Pre-conditions:
* Slim initialized;
* Route defined and matches current request;
* URL encoded spaces in URL;
*
* Post-conditions:
* Route matched;
* Route callable invoked;
* Route callable arguments are URL decoded;
*/
public function testRouteWithUrlEncodedParameters() {
$_SERVER['REQUEST_URI'] = '/foo/jo%20hn/smi%20th';
$app = new Slim();
$app->get('/foo/:one/:two', function ($one, $two) {
echo "$one and $two";
});
$app->run();
$this->expectOutputString('jo hn and smi th');
}
/************************************************
* SLIM ACCESSORS
************************************************/
public function testSlimAccessors() {
$app = new Slim();
$this->assertTrue($app->request() instanceof Slim_Http_Request);
$this->assertTrue($app->response() instanceof Slim_Http_Response);
$this->assertTrue($app->router() instanceof Slim_Router);
}
/************************************************
* SLIM VIEW
************************************************/
/**
* Test Slim copies data from old View to new View
*
* Pre-conditions:
* Slim app instantiated;
* Data set in default View;
* New View is defined;
*
* Post-conditions:
* The data from the original View is accessible in the new View;
*/
public function testSlimCopiesViewData(){
$data = array('foo' => 'bar');
$app = new Slim();
$app->view()->setData($data);
$this->assertTrue($app->view() instanceof Slim_View);
$this->assertEquals($data, $app->view()->getData());
$app->view('CustomView');
$this->assertTrue($app->view() instanceof CustomView);
$this->assertEquals($data, $app->view()->getData());
}
/************************************************
* SLIM RENDERING
************************************************/
/**
* Test Slim rendering with custom status
*
* Pre-conditions:
* Slim app instantiated;
* Render an existing template with custom data and status;
*
* Post-conditions:
* The response status is 404;
* The response body is correct;
*/
public function testSlimRenderSetsResponseStatusOk(){
$this->expectOutputString('test output bar');
$app = new Slim(array(
'templates.path' => dirname(__FILE__) . '/templates'
));
$app->render('test.php', array('foo' => 'bar'), 404);
$this->assertEquals(404, $app->response()->status());
}
/**
* Test Slim rendering
*
* Pre-conditions:
* Slim app instantiated;
* Render an existing template with custom data;
*
* Post-conditions:
* The response body is correct;
*/
public function testSlimRender(){
$this->expectOutputString('test output bar');
$app = new Slim(array(
'templates.path' => dirname(__FILE__) . '/templates'
));
$app->render('test.php', array('foo' => 'bar'));
}
/************************************************
* SLIM HTTP CACHING
************************************************/
/**
* Test Slim HTTP caching if ETag match
*
* Pre-conditions:
* Slim app instantiated;
* Define route that matches current HTTP request;
* Route sets ETag header, matches request's `If-None-Match` header;
*
* Post-conditions:
* Slim app response status is 304;
*/
public function testSlimEtagMatches(){
$app = new Slim();
$app->get('/', function () use ($app) {
$app->etag('abc123');
});
$app->run();
$this->assertEquals(304, $app->response()->status());
}
/**
* Test Slim HTTP caching if ETag does not match
*
* Pre-conditions:
* Slim app instantiated;
* Define route that matches current HTTP request;
* Route sets ETag header, does not match request's `If-None-Match` header;
*
* Post-conditions:
* Slim app response status is 200;
*/
public function testSlimEtagDoesNotMatch(){
$app = new Slim();
$app->get('/', function () use ($app) {
$app->etag('xyz789');
});
$app->run();
$this->assertEquals(200, $app->response()->status());
}
/**
* Test Slim::etag only accepts 'strong' or 'weak' types
*
* Pre-conditions:
* Slim app instantiated;
* Define route that matches current HTTP request;
* Route sets ETag header with an invalid argument;
*
* Post-conditions:
* Slim app response status is 500;
*/
public function testSlimETagThrowsExceptionForInvalidType(){
$this->expectOutputRegex('@Invalid Slim::etag type. Expected "strong" or "weak"@');
$app = new Slim();
$app->get('/', function () use ($app) {
$app->etag('123','foo');
});
$app->run();
$this->assertEquals(500, $app->response()->status());
}
/**
* Test Slim HTTP caching with Last Modified match
*
* Pre-conditions:
* Slim app instantiated;
* Define route that matches current HTTP request;
* Route correctly sets a Last-Modified header;
*
* Post-conditions:
* Slim app response status is 304 Not Modified;
*/
public function testSlimLastModifiedDateMatches(){
$app = new Slim();
$app->get('/', function () use ($app) {
$app->lastModified(1286139652);
});
$app->run();
$this->assertEquals(304, $app->response()->status());
}
/**
* Test Slim HTTP caching if Last Modified does not match
*
* Pre-conditions:
* Slim app instantiated;
* Define route that matches current HTTP request;
* Route sets `Last-Modified` header;
* The HTTP `If-Modified-Since` header does not match the `Last-Modified` date;
*
* Post-conditions:
* Slim app response status is 200;
*/
public function testSlimLastModifiedDateDoesNotMatch(){
$app = new Slim();
$app->get('/', function () use ($app) {
$app->lastModified(1286139250);
});
$app->run();
$this->assertEquals(200, $app->response()->status());
}
/**
* Test Slim Last Modified only accepts integer values
*
* Pre-conditions:
* Slim app instantiated;
* Define route that matches current HTTP request;
* Route sets LastModified header value incorrectly;
*
* Post-conditions:
* Slim app response status is 500;
*/
public function testSlimLastModifiedOnlyAcceptsIntegers(){
$this->expectOutputRegex('@Slim::lastModified only accepts an integer UNIX timestamp value@');
$app = new Slim();
$app->get('/', function () use ($app) {
$app->lastModified('Test');
});
$app->run();
$this->assertEquals(500, $app->response()->status());
}
/************************************************
* SLIM COOKIES
************************************************/
/**
* Test Slim gets cookie
*
* Pre-conditions:
* Slim app instantiated;
* Case A: Cookie `foo` exists, available in HTTP request;
* Case B: Cookie `bad` does not exist;
*
* Post-conditions:
* Case A: Cookie `foo` value is "bar";
* Case B: Cooke `bad` value is NULL;
*/
public function testSlimGetsCookie() {
$app = new Slim();
//Case A
$this->assertEquals('bar', $app->getCookie('foo'));
//Case B
$this->assertNull($app->getCookie('doesNotExist'));
}
/**
* Test Slim sets cookie with default time
*
* Pre-conditions:
* Slim app instantiated;
* Case A: Cookie time not set;
* Case B: Cookie time set as seconds from now (integer);
* Case C: Cookie time set as string;
* Case D: Cookie time is set to 0;
*
* Post-conditions:
* Cookie available in response;
* Case A: Cookie time set using default value;
* Case C: Cookie time set using `strtotime()`;
* Case D: Cookie time is 0;
*/
public function testSlimSetsCookie() {
$app = new Slim();
$cj = $app->response()->getCookieJar();
//Case A
$timeA = time();
$app->setCookie('myCookie1', 'myValue1');
$cookieA = $cj->getResponseCookie('myCookie1');
$this->assertEquals('myCookie1', $cookieA->getName());
$this->assertEquals('myValue1', $cookieA->getValue());
$this->assertEquals($timeA + 1200, $cookieA->getExpires()); //default duration is 20 minutes
$this->assertEquals('/', $cookieA->getPath());
$this->assertEquals('', $cookieA->getDomain());
$this->assertFalse($cookieA->getSecure());
$this->assertFalse($cookieA->getHttpOnly());
//Case C
$timeC = time();
$app->setCookie('myCookie3', 'myValue3', '1 hour');
$cookieC = $cj->getResponseCookie('myCookie3');
$this->assertEquals($timeC + 3600, $cookieC->getExpires());
//Case D
$timeD = time();
$app->setCookie('myCookie4', 'myValue4', 0);
$cookieD = $cj->getResponseCookie('myCookie4');
$this->assertEquals(0, $cookieD->getExpires());
}
/**
* Test Slim sets encrypted cookie
*
* Pre-conditions:
* Slim app instantiated;
* Case A: Cookie time not set;
* Case B: Cookie time set as seconds from now (integer);
* Case C: Cookie time set as string;
* Case D: Cookie time is set to 0;
*
* Post-conditions:
* Cookie available in response;
* Case A: Cookie time set using default value;
* Case C: Cookie time set using `strtotime()`;
* Case D: Cookie time is 0;
*/
public function testSlimSetsEncryptedCookie() {
$app = new Slim();
$cj = $app->response()->getCookieJar();
//Case A
$timeA = time();
$app->setEncryptedCookie('myCookie1', 'myValue1');
$cookieA = $cj->getResponseCookie('myCookie1');
$this->assertEquals('myCookie1', $cookieA->getName());
$this->assertEquals($timeA + 1200, $cookieA->getExpires()); //default duration is 20 minutes
$this->assertEquals('/', $cookieA->getPath());
$this->assertEquals('', $cookieA->getDomain());
$this->assertFalse($cookieA->getSecure());
$this->assertFalse($cookieA->getHttpOnly());
//Case C
$timeC = time();
$app->setEncryptedCookie('myCookie3', 'myValue3', '1 hour');
$cookieC = $cj->getResponseCookie('myCookie3');
$this->assertEquals($timeC + 3600, $cookieC->getExpires());
//Case D
$timeD = time();
$app->setEncryptedCookie('myCookie4', 'myValue4', 0);
$cookieD = $cj->getResponseCookie('myCookie4');
$this->assertEquals(0, $cookieD->getExpires());
}
/**
* Test Slim deletes cookies
*
* Pre-conditions:
* Case A: Classic cookie
* Case B: Encrypted cookie
*
* Post-conditions:
* Response Cookies replaced with empty, auto-expiring Cookies
*/
public function testSlimDeletesCookies() {
$app = new Slim();
$cj = $app->response()->getCookieJar();
//Case A
$app->setCookie('foo1', 'bar1');