123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Stream\Test\Unit;
  36. use Hoa\Stream\Bucket as SUT;
  37. use Hoa\Test;
  38. /**
  39. * Class \Hoa\Stream\Test\Unit\Bucket.
  40. *
  41. * Test suite of the stream bucket.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Bucket extends Test\Unit\Suite
  47. {
  48. public function case_constants()
  49. {
  50. $this
  51. ->boolean(SUT::IS_A_BRIGADE)
  52. ->isTrue()
  53. ->boolean(SUT::IS_A_STREAM)
  54. ->isFalse();
  55. }
  56. public function case_construct_a_brigade()
  57. {
  58. $this
  59. ->given($brigade = 'foo')
  60. ->when($result = new SUT($brigade, SUT::IS_A_BRIGADE))
  61. ->then
  62. ->boolean($result->getType())
  63. ->isEqualTo(SUT::IS_A_BRIGADE)
  64. ->variable($result->getBrigade())
  65. ->isIdenticalTo($brigade)
  66. ->isIdenticalTo('foo');
  67. }
  68. public function case_construct_a_stream()
  69. {
  70. $this
  71. ->given(
  72. $stream = fopen(__FILE__, 'r'),
  73. $buffer = 'bar'
  74. )
  75. ->when($result = new SUT($stream, SUT::IS_A_STREAM, $buffer))
  76. ->then
  77. ->boolean($result->getType())
  78. ->isEqualTo(SUT::IS_A_STREAM)
  79. ->let($bucket = $this->invoke($result)->getBucket())
  80. ->object($bucket)
  81. ->isInstanceOf(\StdClass::class)
  82. ->resource($bucket->bucket)
  83. ->string($bucket->data)
  84. ->isEqualTo($buffer)
  85. ->integer($bucket->datalen)
  86. ->isEqualTo(strlen($buffer))
  87. ->object($result->getBrigade())
  88. ->isIdenticalTo($bucket);
  89. }
  90. public function case_eob()
  91. {
  92. $this
  93. ->given(
  94. $stream = fopen(__FILE__, 'r'),
  95. $bucket = new SUT($stream, SUT::IS_A_STREAM)
  96. )
  97. ->when($result = $bucket->eob())
  98. ->then
  99. ->boolean($result)
  100. ->isTrue();
  101. }
  102. public function case_set_data()
  103. {
  104. $this
  105. ->given(
  106. $stream = fopen(__FILE__, 'r'),
  107. $oldBuffer = 'bar',
  108. $bucket = new SUT($stream, SUT::IS_A_STREAM, $oldBuffer),
  109. $buffer = 'bazqux'
  110. )
  111. ->when($result = $bucket->setData('bazqux'))
  112. ->then
  113. ->string($result)
  114. ->isEqualTo($oldBuffer)
  115. ->let($_bucket = $this->invoke($bucket)->getBucket())
  116. ->object($_bucket)
  117. ->isInstanceOf(\StdClass::class)
  118. ->resource($_bucket->bucket)
  119. ->string($_bucket->data)
  120. ->isEqualTo($buffer)
  121. ->integer($_bucket->datalen)
  122. ->isEqualTo(strlen($buffer))
  123. ->object($bucket->getBrigade())
  124. ->isIdenticalTo($_bucket);
  125. }
  126. public function case_get_data()
  127. {
  128. $this
  129. ->given(
  130. $stream = fopen(__FILE__, 'r'),
  131. $buffer = 'bar',
  132. $bucket = new SUT($stream, SUT::IS_A_STREAM, $buffer)
  133. )
  134. ->when($result = $bucket->getData())
  135. ->then
  136. ->string($result)
  137. ->isEqualTo($buffer)
  138. ->isEqualTo($this->invoke($bucket)->getBucket()->data);
  139. }
  140. public function case_get_length()
  141. {
  142. $this
  143. ->given(
  144. $stream = fopen(__FILE__, 'r'),
  145. $buffer = 'bar',
  146. $bucket = new SUT($stream, SUT::IS_A_STREAM, $buffer)
  147. )
  148. ->when($result = $bucket->getLength())
  149. ->then
  150. ->integer($result)
  151. ->isEqualTo(strlen($buffer))
  152. ->isEqualTo($this->invoke($bucket)->getBucket()->datalen);
  153. }
  154. }