silverstripe-framework/tests/control/PjaxResponseNegotiatorTest.php

63 lines
2.3 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\PjaxResponseNegotiator;
2016-09-09 08:43:05 +02:00
use SilverStripe\Control\HTTPRequest;
class PjaxResponseNegotiatorTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
public function testDefaultCallbacks() {
$negotiator = new PjaxResponseNegotiator(array(
'default' => function() {return 'default response';},
));
2016-09-09 08:43:05 +02:00
$request = new HTTPRequest('GET', '/'); // not setting pjax header
2012-06-05 14:22:00 +02:00
$response = $negotiator->respond($request);
$this->assertEquals('default response', $response->getBody());
}
public function testSelectsFragmentByHeader() {
$negotiator = new PjaxResponseNegotiator(array(
'default' => function() {return 'default response';},
'myfragment' => function() {return 'myfragment response';},
));
2016-09-09 08:43:05 +02:00
$request = new HTTPRequest('GET', '/');
$request->addHeader('X-Pjax', 'myfragment');
2012-06-05 14:22:00 +02:00
$response = $negotiator->respond($request);
$this->assertEquals('{"myfragment":"myfragment response"}', $response->getBody());
}
public function testMultipleFragments() {
$negotiator = new PjaxResponseNegotiator(array(
'default' => function() {return 'default response';},
'myfragment' => function() {return 'myfragment response';},
'otherfragment' => function() {return 'otherfragment response';},
));
2016-09-09 08:43:05 +02:00
$request = new HTTPRequest('GET', '/');
$request->addHeader('X-Pjax', 'myfragment,otherfragment');
$request->addHeader('Accept', 'text/json');
2012-06-05 14:22:00 +02:00
$response = $negotiator->respond($request);
$json = json_decode( $response->getBody());
$this->assertObjectHasAttribute('myfragment', $json);
$this->assertEquals('myfragment response', $json->myfragment);
$this->assertObjectHasAttribute('otherfragment', $json);
$this->assertEquals('otherfragment response', $json->otherfragment);
}
public function testFragmentsOverride() {
$negotiator = new PjaxResponseNegotiator(array(
'alpha' => function() {return 'alpha response';},
'beta' => function() {return 'beta response';}
));
2016-09-09 08:43:05 +02:00
$request = new HTTPRequest('GET', '/');
$request->addHeader('X-Pjax', 'alpha');
$request->addHeader('Accept', 'text/json');
$response = $negotiator->setFragmentOverride(array('beta'))->respond($request);
$json = json_decode( $response->getBody());
$this->assertFalse(isset($json->alpha));
$this->assertObjectHasAttribute('beta', $json);
}
}