InputTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. class InputTest extends PHPUnit_Framework_TestCase {
  3. public static function setUpBeforeClass()
  4. {
  5. System\Input::$input = null;
  6. }
  7. public static function tearDownAfterClass()
  8. {
  9. System\Config::set('session.driver', '');
  10. System\Session::$session = array();
  11. }
  12. public function setUp()
  13. {
  14. $_SERVER['REQUEST_METHOD'] = 'GET';
  15. }
  16. public function tearDown()
  17. {
  18. System\Input::$input = null;
  19. }
  20. /**
  21. * @dataProvider inputByMethodProvider
  22. */
  23. public function testInputShouldHydrateBasedOnRequestMethod($method, $data)
  24. {
  25. $_SERVER['REQUEST_METHOD'] = $method;
  26. $_GET = $data;
  27. $_POST = $data;
  28. $this->assertEquals(System\Input::get(), $data);
  29. }
  30. public function inputByMethodProvider()
  31. {
  32. return array(
  33. array('GET', array('method' => 'GET')),
  34. array('POST', array('method' => 'POST')),
  35. );
  36. }
  37. /**
  38. * @dataProvider inputBySpoofedMethodProvider
  39. */
  40. public function testInputShouldHydrateBasedOnSpoofedRequestMethod($method, $data)
  41. {
  42. $_SERVER['REQUEST_METHOD'] = 'POST';
  43. $_POST = $data;
  44. $this->assertEquals(System\Input::get(), $data);
  45. }
  46. public function inputBySpoofedMethodProvider()
  47. {
  48. return array(
  49. array('PUT', array('request_method' => 'PUT', 'method' => 'PUT')),
  50. array('DELETE', array('request_method' => 'DELETE', 'method' => 'DELETE')),
  51. );
  52. }
  53. public function testHasMethodReturnsTrueIfItemIsPresentInInputData()
  54. {
  55. System\Input::$input = array('name' => 'taylor');
  56. $this->assertTrue(System\Input::has('name'));
  57. }
  58. public function testHasMethodReturnsFalseIfItemIsNotPresentInInputData()
  59. {
  60. System\Input::$input = array();
  61. $this->assertFalse(System\Input::has('name'));
  62. }
  63. public function testGetMethodReturnsItemByInputKey()
  64. {
  65. System\Input::$input = array('name' => 'taylor');
  66. $this->assertEquals(System\Input::get('name'), 'taylor');
  67. }
  68. public function testGetMethodReturnsDefaultValueWhenItemDoesntExist()
  69. {
  70. System\Input::$input = array();
  71. $this->assertNull(System\Input::get('name'));
  72. $this->assertEquals(System\Input::get('name', 'test'), 'test');
  73. $this->assertTrue(is_array(System\Input::get()) and count(System\Input::get()) == 0);
  74. }
  75. public function testGetMethodReturnsEntireInputArrayWhenNoKeyGiven()
  76. {
  77. System\Input::$input = array('name' => 'taylor', 'age' => 25);
  78. $this->assertEquals(System\Input::get(), System\Input::$input);
  79. }
  80. public function testFileMethodReturnsItemFromGlobalFilesArray()
  81. {
  82. $_FILES['test'] = array('name' => 'taylor');
  83. $this->assertEquals(System\Input::file('test'), $_FILES['test']);
  84. }
  85. public function testFileMethodReturnsSpecificItemFromFileArrayWhenSpecified()
  86. {
  87. $_FILES['test'] = array('size' => 500);
  88. $this->assertEquals(System\Input::file('test.size'), 500);
  89. }
  90. /**
  91. * @expectedException Exception
  92. */
  93. public function testOldMethodShouldThrowExceptionWhenSessionsArentEnabled()
  94. {
  95. System\Input::old();
  96. }
  97. /**
  98. * @expectedException Exception
  99. */
  100. public function testHadMethodShouldThrowExceptionWhenSessionsArentEnabled()
  101. {
  102. System\Input::has();
  103. }
  104. public function testOldMethodShouldReturnOldInputDataFromSession()
  105. {
  106. System\Config::set('session.driver', 'test');
  107. System\Session::$session['data']['laravel_old_input'] = array('name' => 'taylor');
  108. $this->assertEquals(System\Input::old('name'), 'taylor');
  109. }
  110. public function testHadMethodReturnsTrueIfItemIsPresentInOldInputData()
  111. {
  112. System\Config::set('session.driver', 'test');
  113. System\Session::$session['data']['laravel_old_input'] = array('name' => 'taylor');
  114. $this->assertTrue(System\Input::had('name'));
  115. }
  116. public function testHadMethodReturnsFalseIfItemIsNotPresentInOldInputData()
  117. {
  118. System\Config::set('session.driver', 'test');
  119. System\Session::$session['data']['laravel_old_input'] = array();
  120. $this->assertFalse(System\Input::had('name'));
  121. }
  122. }