/var/www/hkosl.com/b2b2c/webadmin/libraies/klein/klein/tests/Klein/Tests/ServiceProviderTest.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
<?php
/**
 * Klein (klein.php) - A fast & flexible router for PHP
 *
 * @author      Chris O'Hara <cohara87@gmail.com>
 * @author      Trevor Suarez (Rican7) (contributor and v2 refactorer)
 * @copyright   (c) Chris O'Hara
 * @link        https://github.com/klein/klein.php
 * @license     MIT
 */

namespace Klein\Tests;

use 
Klein\DataCollection\DataCollection;
use 
Klein\Klein;
use 
Klein\Request;
use 
Klein\Response;
use 
Klein\ServiceProvider;
use 
Klein\Validator;

/**
 * ServiceProviderTest
 */
class ServiceProviderTest extends AbstractKleinTest
{

    protected function 
getBasicServiceProvider()
    {
        return new 
ServiceProvider(
            
$request = new Request(),
            
$response = new Response()
        );
    }

    public function 
testConstructor()
    {
        
$service = new ServiceProvider();

        
// Make sure our attributes are first null
        
$this->assertAttributeEquals(null'request'$service);
        
$this->assertAttributeEquals(null'response'$service);

        
// New service with injected dependencies
        
$service = new ServiceProvider(
            
$request = new Request(),
            
$response = new Response()
        );

        
// Make sure our attributes are set
        
$this->assertAttributeEquals($request'request'$service);
        
$this->assertAttributeEquals($response'response'$service);
    }

    public function 
testBinder()
    {
        
$service = new ServiceProvider();

        
// Make sure our attributes are first null
        
$this->assertAttributeEquals(null'request'$service);
        
$this->assertAttributeEquals(null'response'$service);

        
// New service with injected dependencies
        
$return_val $service->bind(
            
$request = new Request(),
            
$response = new Response()
        );

        
// Make sure our attributes are set
        
$this->assertAttributeEquals($request'request'$service);
        
$this->assertAttributeEquals($response'response'$service);

        
// Make sure we're chainable
        
$this->assertEquals($service$return_val);
        
$this->assertSame($service$return_val);
    }

    public function 
testSharedDataGetter()
    {
        
$service = new ServiceProvider();

        
$this->assertInternalType('object'$service->sharedData());
        
$this->assertTrue($service->sharedData() instanceof DataCollection);
    }

    public function 
testStartSession()
    {
        
$service = new ServiceProvider();

        
$returned $service->startSession();

        
$this->assertSame(session_id(), $returned);

        
// Clean up
        
session_destroy();
    }

    public function 
testStartSessionFails()
    {
        
// Only care about some errors, and keep the old value
        
$old_error_val error_reporting();
        
error_reporting(E_ALL E_NOTICE E_WARNING);

        
session_start();
        
session_id('');

        
$service = new ServiceProvider();

        
$returned $service->startSession();

        
$this->assertFalse($returned);

        
// Clean up
        
session_destroy();
        
error_reporting($old_error_val);
    }

    public function 
testFlash()
    {
        
// Test data
        
$test_session_key '__flashes';
        
$test_flashes = array(
            array(
                
'message' => 'Test info message',
                
'type' => 'info',
            ),
            array(
                
'message' => 'Test error message',
                
'type' => 'error',
            ),
        );

        
$service = new ServiceProvider();

        
$this->assertEmpty($_SESSION);

        
$service->flash($test_flashes[0]['message'], $test_flashes[0]['type']);
        
$service->flash($test_flashes[1]['message'], $test_flashes[1]['type']);

        
$this->assertNotEmpty($_SESSION);
        
$this->assertSame($test_flashes[0]['message'], $_SESSION[$test_session_key][$test_flashes[0]['type']][0]);
        
$this->assertSame($test_flashes[1]['message'], $_SESSION[$test_session_key][$test_flashes[1]['type']][0]);

        
// Clean up
        
session_destroy();
        
$_SESSION = array();
    }

    public function 
testFlashWithMarkdown()
    {
        
// Test data
        
$test_session_key '__flashes';
        
$test_type 'info';
        
$test_message 'Test message by %s %s';
        
$test_params = array(
            
'Trevor',
            
'Suarez',
        );
        
$test_processed 'Test message by ' $test_params[0] . ' ' $test_params[1];

        
$service = new ServiceProvider();

        
$this->assertEmpty($_SESSION);

        
$service->flash($test_message$test_params);

        
$this->assertNotEmpty($_SESSION);
        
$this->assertSame($test_processed$_SESSION[$test_session_key][$test_type][0]);

        
// Clean up
        
session_destroy();
        
$_SESSION = array();
    }

    public function 
testFlashes()
    {
        
// Test data
        
$test_session_key '__flashes';
        
$test_flashes = array(
            array(
                
'message' => 'Test info message',
                
'type' => 'info',
            ),
            array(
                
'message' => 'Test error message',
                
'type' => 'error',
            ),
            array(
                
'message' => 'Test second error message',
                
'type' => 'error',
            ),
            array(
                
'message' => 'Test whatever message',
                
'type' => 'whatever',
            ),
        );
        
$test_error_flashes = array(
            
$test_flashes[1]['message'],
            
$test_flashes[2]['message'],
        );

        
$service = new ServiceProvider();

        
$this->assertEmpty($_SESSION);
        
$this->assertEmpty($service->flashes());

        
$service->flash($test_flashes[0]['message'], $test_flashes[0]['type']);
        
$service->flash($test_flashes[1]['message'], $test_flashes[1]['type']);
        
$service->flash($test_flashes[2]['message'], $test_flashes[2]['type']);
        
$service->flash($test_flashes[3]['message'], $test_flashes[3]['type']);

        
// Test error flashes only
        
$error_flashes $service->flashes('error');
        
$this->assertCount(2$error_flashes);
        
$this->assertSame($test_error_flashes$error_flashes);

        
// Test the rest
        
$all_flashes $service->flashes();
        
$this->assertCount(
            
count($test_flashes) - count($error_flashes),
            
$all_flashes
        
);

        
// Clean up
        
session_destroy();
        
$_SESSION = array();
    }

    public function 
testMarkdownParser()
    {
        
// Test basic markdown conversion
        
$this->assertSame(
            
'<strong>dog</strong> <em>cat</em> <a href="src">name</a>',
            
ServiceProvider::markdown('**dog** *cat* [name](src)')
        );

        
// Test array arguments
        
$this->assertSame(
            
'<strong>huh</strong> <em>12</em> <strong>CD</strong>',
            
ServiceProvider::markdown('**%s** *%d* **%X**', array('huh''12'205))
        );

        
// Test variable number of arguments
        
$this->assertSame(
            
'<strong>huh</strong> <em>12</em> <strong>CD</strong>',
            
ServiceProvider::markdown('**%s** *%d* **%X**''huh''12'205)
        );

        
// Test second array argument overrides other arguments
        
$this->assertSame(
            
'<strong>huh</strong> <em>12</em> <strong>CD</strong>',
            
ServiceProvider::markdown('**%s** *%d* **%X**', array('huh''12'205), 'dog''cheese')
        );
    }

    public function 
testEscapeCharacters()
    {
        
$this->assertSame(
            
'H&egrave;&egrave;&egrave;llo! A&amp;W root beer is now 20% off!!',
            
ServiceProvider::escape('Hèèèllo! A&W root beer is now 20% off!!')
        );
    }

    public function 
testRefresh()
    {
        
$this->klein_app->respond(
            function (
$request$response$service) {
                
$service->refresh();
            }
        );

        
$this->klein_app->dispatch();

        
$this->assertSame(
            
$this->klein_app->request()->uri(),
            
$this->klein_app->response()->headers()->get('location')
        );
        
$this->assertTrue($this->klein_app->response()->isLocked());

        
// Make sure we got a 3xx response code
        
$this->assertGreaterThan(299$this->klein_app->response()->code());
        
$this->assertLessThan(400$this->klein_app->response()->code());
    }

    public function 
testBack()
    {
        
$url 'http://google.com/';

        
$request = new Request();
        
$request->server()->set('HTTP_REFERER'$url);

        
$this->klein_app->respond(
            function (
$request$response$service) {
                
$service->back();
            }
        );

        
$this->klein_app->dispatch($request);

        
$this->assertSame(
            
$url,
            
$this->klein_app->response()->headers()->get('location')
        );
        
$this->assertTrue($this->klein_app->response()->isLocked());

        
// Make sure we got a 3xx response code
        
$this->assertGreaterThan(299$this->klein_app->response()->code());
        
$this->assertLessThan(400$this->klein_app->response()->code());
    }

    public function 
testBackWithoutRefererSet()
    {
        
$request = new Request();

        
$this->klein_app->respond(
            function (
$request$response$service) {
                
$service->back();
            }
        );

        
$this->klein_app->dispatch($request);

        
$this->assertTrue($this->klein_app->response()->isLocked());

        
// Make sure we got a 3xx response code
        
$this->assertGreaterThan(299$this->klein_app->response()->code());
        
$this->assertLessThan(400$this->klein_app->response()->code());
    }

    public function 
testLayoutGetSet()
    {
        
$test_layout 'boom!! :D';

        
$service = new ServiceProvider();

        
$this->assertEmpty($service->layout());

        
$service->layout($test_layout);

        
$this->assertSame($test_layout$service->layout());
    }

    
/**
     * NOTE: Also tests "yield()"
     */
    
public function testRender()
    {
        
$test_data = array(
            
'name' => 'trevor suarez',
            
'title' => 'about',
            
'verb' => 'woot',
        );

        
$this->klein_app->respond(
            function (
$request$response$service) use ($test_data) {
                
// Set some data manually
                
$service->sharedData()->set('name''should be overwritten');

                
// Set our layout
                
$service->layout(__DIR__.'/views/layout.php');

                
// Render our view, and pass some MORE data
                
$service->render(
                    
__DIR__.'/views/test.php',
                    
$test_data
                
);
            }
        );

        
$this->klein_app->dispatch();

        
$this->expectOutputString(
            
'<h1>About</h1>' PHP_EOL
            
.'My name is Trevor Suarez.' PHP_EOL
            
.'WOOT!' PHP_EOL
            
.'<div>footer</div>' PHP_EOL
        
);
    }

    public function 
testRenderChunked()
    {
        
$test_data = array(
            
'name' => 'trevor suarez',
            
'title' => 'about',
            
'verb' => 'woot',
        );

        
$response = new Response();
        
$response->chunk();

        
$this->klein_app->respond(
            function (
$request$response$service) use ($test_data) {
                
// Set some data manually
                
$service->sharedData()->set('name''should be overwritten');

                
// Set our layout
                
$service->layout(__DIR__.'/views/layout.php');

                
// Render our view, and pass some MORE data
                
$service->render(
                    
__DIR__.'/views/test.php',
                    
$test_data
                
);
            }
        );

        
$this->klein_app->dispatch(null$response);

        
$this->expectOutputString(
            
'<h1>About</h1>' PHP_EOL
            
.'My name is Trevor Suarez.' PHP_EOL
            
.'WOOT!' PHP_EOL
            
.'<div>footer</div>' PHP_EOL
        
);
    }

    public function 
testPartial()
    {
        
$test_data = array(
            
'name' => 'trevor suarez',
            
'title' => 'about',
            
'verb' => 'woot',
        );

        
$this->klein_app->respond(
            function (
$request$response$service) use ($test_data) {
                
// Set our layout
                
$service->layout(__DIR__.'/views/layout.php');

                
// Render our view, and pass some MORE data
                
$service->partial(
                    
__DIR__.'/views/test.php',
                    
$test_data
                
);
            }
        );

        
$this->klein_app->dispatch();

        
// Make sure the layout doesn't get included
        
$this->expectOutputString(
            
'My name is Trevor Suarez.' PHP_EOL
            
.'WOOT!' PHP_EOL
        
);
    }

    public function 
testAddValidator()
    {
        
$service = new ServiceProvider();

        
// Initially empty
        
$this->assertEmpty(Validator::$methods);

        
$test_callback = function () {
            echo 
'test';
        };

        
$service->addValidator('awesome'$test_callback);

        
$this->assertNotEmpty(Validator::$methods);
        
$this->assertArrayHasKey('awesome'Validator::$methods);
        
$this->assertContains($test_callbackValidator::$methods);
    }

    
/**
     * @expectedException \Klein\Exceptions\ValidationException
     */
    
public function testValidate()
    {
        
$this->klein_app->onError(
            function (
$a$b$c$exception) {
                throw 
$exception;
            }
        );

        
$this->klein_app->respond(
            function (
$request$response$service) {
                
$service->validate('thing')->isLen(3);
            }
        );

        
$this->klein_app->dispatch();
    }

    
/**
     * @expectedException \Klein\Exceptions\ValidationException
     */
    
public function testValidateParam()
    {
        
$this->klein_app->onError(
            function (
$a$b$c$exception) {
                throw 
$exception;
            }
        );

        
$this->klein_app->respond(
            function (
$request$response$service) {
                
// Set a test param
                
$request->paramsNamed()->set('name''trevor');

                
$service->validateParam('name')->notNull()->isLen(3);
            }
        );

        
$this->klein_app->dispatch();
    }

    
// Test ALL of the magic setter, getter, exists, and removal methods
    
public function testMagicGetSetExistsRemove()
    {
        
$test_data = array(
            
'name' => 'huh?',
        );

        
$service = new ServiceProvider();

        
$this->assertEmpty($service->sharedData()->all());
        
$this->assertNull($service->sharedData()->get('test_data'));
        
$this->assertNull($service->name);
        
$this->assertFalse(isset($service->name));

        
$service->name $test_data['name'];

        
$this->assertTrue(isset($service->name));
        
$this->assertSame($test_data['name'], $service->name);

        unset(
$service->name);

        
$this->assertEmpty($service->sharedData()->all());
        
$this->assertNull($service->sharedData()->get('test_data'));
        
$this->assertNull($service->name);
        
$this->assertFalse(isset($service->name));
    }
}