Blame view

framework/tests/control/PjaxResponseNegotiatorTest.php 2.16 KB
0084d336   Administrator   Importers CRUD
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
  <?php
  class PjaxResponseNegotiatorTest extends SapphireTest {
  	
  	public function testDefaultCallbacks() {
  		$negotiator = new PjaxResponseNegotiator(array(
  			'default' => function() {return 'default response';},
  		));
  		$request = new SS_HTTPRequest('GET', '/'); // not setting pjax header
  		$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';},
  		));
  		$request = new SS_HTTPRequest('GET', '/');
  		$request->addHeader('X-Pjax', 'myfragment');
  		$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';},
  		));
  		$request = new SS_HTTPRequest('GET', '/');
  		$request->addHeader('X-Pjax', 'myfragment,otherfragment');
  		$request->addHeader('Accept', 'text/json');
  		$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';}
  		));
  
  		$request = new SS_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);
  	}
  
  }