Stream.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Integration;
  36. use Hoa\Event;
  37. use Hoa\Stream as LUT;
  38. use Hoa\Test;
  39. /**
  40. * Class \Hoa\Stream\Test\Integration\Stream.
  41. *
  42. * Test suite of the stream class.
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Stream extends Test\Integration\Suite
  48. {
  49. public function case_notifications()
  50. {
  51. $self = $this;
  52. $this
  53. ->given(
  54. $port = mt_rand(10000, 12000),
  55. exec(
  56. sprintf(
  57. 'php -S 127.0.0.1:%d -t %s > /dev/null 2>&1 & echo $! && sleep 0.2',
  58. $port,
  59. dirname(__DIR__) . DS . 'Fixtures'
  60. ),
  61. $outputs
  62. ),
  63. $pid = $outputs[0],
  64. $stream = new SUT('http://127.0.0.1:' . $port, null, true),
  65. $stream->on(
  66. 'connect',
  67. function (Event\Bucket $bucket) use ($self, &$connectCalled) {
  68. $connectCalled = true;
  69. $data = $bucket->getData();
  70. $self
  71. ->array($data)
  72. ->isEqualTo([
  73. 'code' => 0,
  74. 'severity' => 0,
  75. 'message' => null,
  76. 'transferred' => 0,
  77. 'max' => 0
  78. ]);
  79. }
  80. ),
  81. $stream->on(
  82. 'mimetype',
  83. function (Event\Bucket $bucket) use ($self, &$mimetypeCalled) {
  84. $mimetypeCalled = true;
  85. $data = $bucket->getData();
  86. $self
  87. ->array($data)
  88. ->isEqualTo([
  89. 'code' => 0,
  90. 'severity' => 0,
  91. 'message' => 'text/html; charset=UTF-8',
  92. 'transferred' => 0,
  93. 'max' => 0
  94. ]);
  95. }
  96. ),
  97. $stream->on(
  98. 'size',
  99. function (Event\Bucket $bucket) use ($self, &$sizeCalled) {
  100. $sizeCalled = true;
  101. $data = $bucket->getData();
  102. $self
  103. ->array($data)
  104. ->isEqualTo([
  105. 'code' => 0,
  106. 'severity' => 0,
  107. 'message' => 'Content-Length: 14',
  108. 'transferred' => 0,
  109. 'max' => 14
  110. ]);
  111. }
  112. ),
  113. $stream->on(
  114. 'progress',
  115. function (Event\Bucket $bucket) use ($self, &$progressCalled) {
  116. $progressCalled = true;
  117. $data = $bucket->getData();
  118. $self
  119. ->array($data)
  120. ->isEqualTo([
  121. 'code' => 0,
  122. 'severity' => 0,
  123. 'message' => null,
  124. 'transferred' => 0,
  125. 'max' => 14
  126. ]);
  127. }
  128. )
  129. )
  130. ->when($stream->open())
  131. ->then
  132. ->boolean($connectCalled)
  133. ->isTrue()
  134. ->boolean($mimetypeCalled)
  135. ->isTrue()
  136. ->boolean($sizeCalled)
  137. ->isTrue()
  138. ->boolean($progressCalled)
  139. ->isTrue()
  140. ->let(!empty($pid) && exec('kill ' . $pid));
  141. }
  142. }
  143. class SUT extends LUT\Stream
  144. {
  145. protected function &_open($streamName, LUT\Context $context = null)
  146. {
  147. if (null === $context) {
  148. $out = fopen($streamName, 'rb');
  149. } else {
  150. $out = fopen($streamName, 'rb', false, $context->getContext());
  151. }
  152. return $out;
  153. }
  154. protected function _close()
  155. {
  156. return fclose($this->getStream());
  157. }
  158. }