Context.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 as LUT;
  37. use Hoa\Stream\Context as SUT;
  38. use Hoa\Test;
  39. /**
  40. * Class \Hoa\Stream\Test\Unit\Context.
  41. *
  42. * Test suite of the context stream class.
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Context extends Test\Unit\Suite
  48. {
  49. public function case_get_instance_with_empty_id()
  50. {
  51. $this
  52. ->exception(function () {
  53. SUT::getInstance(null);
  54. })
  55. ->isInstanceOf(LUT\Exception::class);
  56. }
  57. public function case_get_new_instance()
  58. {
  59. $this
  60. ->when($result = SUT::getInstance('foo'))
  61. ->then
  62. ->object($result)
  63. ->isInstanceOf(SUT::class);
  64. }
  65. public function case_get_new_instances()
  66. {
  67. $this
  68. ->when($result = SUT::getInstance('foo'))
  69. ->then
  70. ->object($result)
  71. ->isNotIdenticalTo(SUT::getInstance('bar'));
  72. }
  73. public function case_get_same_instance()
  74. {
  75. $this
  76. ->when($result = SUT::getInstance('foo'))
  77. ->then
  78. ->object($result)
  79. ->isIdenticalTo(SUT::getInstance('foo'));
  80. }
  81. public function case_get_id()
  82. {
  83. $this
  84. ->given(
  85. $id = 'foo',
  86. $context = SUT::getInstance($id)
  87. )
  88. ->when($result = $context->getId())
  89. ->then
  90. ->string($result)
  91. ->isEqualTo($id);
  92. }
  93. public function case_context_exists()
  94. {
  95. $this
  96. ->given(
  97. $id = 'foo',
  98. SUT::getInstance($id)
  99. )
  100. ->when($result = SUT::contextExists($id))
  101. ->then
  102. ->boolean($result)
  103. ->isTrue();
  104. }
  105. public function case_context_does_not_exist()
  106. {
  107. $this
  108. ->when($result = SUT::contextExists('foo'))
  109. ->then
  110. ->boolean($result)
  111. ->isFalse();
  112. }
  113. public function case_set_options()
  114. {
  115. $this
  116. ->given(
  117. $context = SUT::getInstance('foo'),
  118. $options = ['bar' => ['baz' => 'qux']]
  119. )
  120. ->when($result = $context->setOptions($options))
  121. ->then
  122. ->boolean($result)
  123. ->isTrue();
  124. }
  125. public function case_get_options()
  126. {
  127. $this
  128. ->given(
  129. $context = SUT::getInstance('foo'),
  130. $options = ['bar' => ['baz' => 'qux']],
  131. $context->setOptions($options)
  132. )
  133. ->when($result = $context->getOptions())
  134. ->then
  135. ->array($result)
  136. ->isEqualTo($options);
  137. }
  138. public function case_set_parameters()
  139. {
  140. $this
  141. ->given(
  142. $context = SUT::getInstance('foo'),
  143. $parameters = [
  144. 'notificaion' => 'callback',
  145. 'options' => ['bar' => ['baz' => 'qux']]
  146. ]
  147. )
  148. ->when($result = $context->setParameters($parameters))
  149. ->then
  150. ->boolean($result)
  151. ->isTrue();
  152. }
  153. public function case_get_parameters()
  154. {
  155. $this
  156. ->given(
  157. $context = SUT::getInstance('foo'),
  158. $parameters = [
  159. 'notification' => 'callback',
  160. 'options' => ['bar' => ['baz' => 'qux']]
  161. ],
  162. $context->setParameters($parameters)
  163. )
  164. ->when($result = $context->getParameters())
  165. ->then
  166. ->array($result)
  167. ->isEqualTo($parameters);
  168. }
  169. public function case_get_context()
  170. {
  171. $this
  172. ->given($context = SUT::getInstance('foo'))
  173. ->when($result = $context->getContext())
  174. ->then
  175. ->resource($result)
  176. ->isStreamContext();
  177. }
  178. }