input.test.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class InputTest extends PHPUnit_Framework_TestCase {
  3. /**
  4. * Setup the testing environment.
  5. */
  6. public function setUp()
  7. {
  8. Config::set('application.key', 'foo');
  9. }
  10. /**
  11. * Tear down the testing environemnt.
  12. */
  13. public function tearDown()
  14. {
  15. Input::$input = array();
  16. Config::set('application.key', '');
  17. Session::$instance = null;
  18. }
  19. /**
  20. * Test the Input::all method.
  21. *
  22. * @group laravel
  23. */
  24. public function testAllMethodReturnsInputAndFiles()
  25. {
  26. Input::$input = array('name' => 'Taylor');
  27. $_FILES = array('age' => 25);
  28. $this->assertEquals(Input::all(), array('name' => 'Taylor', 'age' => 25));
  29. }
  30. /**
  31. * Test the Input::has method.
  32. *
  33. * @group laravel
  34. */
  35. public function testHasMethodIndicatesTheExistenceOfInput()
  36. {
  37. $this->assertFalse(Input::has('foo'));
  38. Input::$input = array('name' => 'Taylor');
  39. $this->assertTrue(Input::has('name'));
  40. }
  41. /**
  42. * Test the Input::get method.
  43. *
  44. * @group laravel
  45. */
  46. public function testGetMethodReturnsInputValue()
  47. {
  48. Input::$input = array('name' => 'Taylor');
  49. $this->assertEquals('Taylor', Input::get('name'));
  50. $this->assertEquals('Default', Input::get('foo', 'Default'));
  51. }
  52. /**
  53. * Test the Input::only method.
  54. *
  55. * @group laravel
  56. */
  57. public function testOnlyMethodReturnsSubsetOfInput()
  58. {
  59. Input::$input = array('name' => 'Taylor', 'age' => 25);
  60. $this->assertEquals(array('name' => 'Taylor'), Input::only(array('name')));
  61. }
  62. /**
  63. * Test the Input::except method.
  64. *
  65. * @group laravel
  66. */
  67. public function testExceptMethodReturnsSubsetOfInput()
  68. {
  69. Input::$input = array('name' => 'Taylor', 'age' => 25);
  70. $this->assertEquals(array('age' => 25), Input::except(array('name')));
  71. }
  72. /**
  73. * Test the Input::old method.
  74. *
  75. * @group laravel
  76. */
  77. public function testOldInputCanBeRetrievedFromSession()
  78. {
  79. $this->setSession();
  80. Session::$instance->session['data']['laravel_old_input'] = array('name' => 'Taylor');
  81. $this->assertNull(Input::old('foo'));
  82. $this->assertTrue(Input::had('name'));
  83. $this->assertFalse(Input::had('foo'));
  84. $this->assertEquals('Taylor', Input::old('name'));
  85. }
  86. /**
  87. * Test the Input::file method.
  88. *
  89. * @group laravel
  90. */
  91. public function testFileMethodReturnsFromFileArray()
  92. {
  93. $_FILES['foo'] = array('name' => 'Taylor', 'size' => 100);
  94. $this->assertEquals('Taylor', Input::file('foo.name'));
  95. $this->assertEquals(array('name' => 'Taylor', 'size' => 100), Input::file('foo'));
  96. }
  97. /**
  98. * Test the Input::flash method.
  99. *
  100. * @group laravel
  101. */
  102. public function testFlashMethodFlashesInputToSession()
  103. {
  104. $this->setSession();
  105. Input::$input = $input = array('name' => 'Taylor', 'age' => 25);
  106. Input::flash();
  107. $this->assertEquals($input, Session::$instance->session['data'][':new:']['laravel_old_input']);
  108. Input::flash('only', array('name'));
  109. $this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['laravel_old_input']);
  110. Input::flash('except', array('name'));
  111. $this->assertEquals(array('age' => 25), Session::$instance->session['data'][':new:']['laravel_old_input']);
  112. }
  113. /**
  114. * Test the Input::flush method.
  115. *
  116. * @group laravel
  117. */
  118. public function testFlushMethodClearsFlashedInput()
  119. {
  120. $this->setSession();
  121. Input::$input = $input = array('name' => 'Taylor');
  122. Input::flash();
  123. $this->assertEquals($input, Session::$instance->session['data'][':new:']['laravel_old_input']);
  124. Input::flush();
  125. $this->assertEquals(array(), Session::$instance->session['data'][':new:']['laravel_old_input']);
  126. }
  127. /**
  128. * Set the session payload instance.
  129. */
  130. protected function setSession()
  131. {
  132. $driver = $this->getMock('Laravel\\Session\\Drivers\\Driver');
  133. Session::$instance = new Laravel\Session\Payload($driver);
  134. }
  135. }