base_options_test.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @package Base
  22. * @subpackage Tests
  23. * @version //autogentag//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. */
  26. require_once dirname( __FILE__ ) . '/test_options.php';
  27. /**
  28. * @package Base
  29. * @subpackage Tests
  30. */
  31. class ezcBaseOptionsTest extends ezcTestCase
  32. {
  33. public static function suite()
  34. {
  35. return new PHPUnit_Framework_TestSuite("ezcBaseOptionsTest");
  36. }
  37. public function testGetAccessFailure()
  38. {
  39. $opt = new ezcBaseTestOptions();
  40. try
  41. {
  42. echo $opt->properties;
  43. }
  44. catch ( ezcBasePropertyNotFoundException $e )
  45. {
  46. return;
  47. }
  48. $this->fail( "ezcBasePropertyNotFoundException not thrown on access to forbidden property \$properties" );
  49. }
  50. public function testGetOffsetAccessFailure()
  51. {
  52. $opt = new ezcBaseTestOptions();
  53. try
  54. {
  55. echo $opt["properties"];
  56. }
  57. catch ( ezcBasePropertyNotFoundException $e )
  58. {
  59. return;
  60. }
  61. $this->fail( "ezcBasePropertyNotFoundException not thrown on access to forbidden property \$properties" );
  62. }
  63. public function testSetOffsetAccessFailure()
  64. {
  65. $opt = new ezcBaseTestOptions();
  66. try
  67. {
  68. $opt["properties"] = "foo";
  69. }
  70. catch ( ezcBasePropertyNotFoundException $e )
  71. {
  72. return;
  73. }
  74. $this->fail( "ezcBasePropertyNotFoundException not thrown on access to forbidden property \$properties" );
  75. }
  76. public function testConstructorWithParameters()
  77. {
  78. $options = new ezcBaseTestOptions( array( 'foo' => 'xxx' ) );
  79. $this->assertEquals( 'xxx', $options->foo );
  80. }
  81. public function testMerge()
  82. {
  83. $options = new ezcBaseTestOptions();
  84. $this->assertEquals( 'bar', $options->foo );
  85. $options->merge( array( 'foo' => 'xxx' ) );
  86. $this->assertEquals( 'xxx', $options->foo );
  87. }
  88. public function testOffsetExists()
  89. {
  90. $options = new ezcBaseTestOptions();
  91. $this->assertEquals( true, $options->offsetExists( 'foo' ) );
  92. $this->assertEquals( false, $options->offsetExists( 'bar' ) );
  93. }
  94. public function testOffsetSet()
  95. {
  96. $options = new ezcBaseTestOptions();
  97. $this->assertEquals( 'bar', $options->foo );
  98. $options->offsetSet( 'foo', 'xxx' );
  99. $this->assertEquals( 'xxx', $options->foo );
  100. }
  101. public function testOffsetUnset()
  102. {
  103. $options = new ezcBaseTestOptions();
  104. $this->assertEquals( 'bar', $options->foo );
  105. $options->offsetUnset( 'foo' );
  106. $this->assertEquals( null, $options->foo );
  107. $this->assertEquals( true, $options->offsetExists( 'foo' ) );
  108. }
  109. public function testAutoloadOptions()
  110. {
  111. $options = new ezcBaseAutoloadOptions();
  112. try
  113. {
  114. $options->no_such_property = 'value';
  115. $this->fail( 'Expected exception was not thrown.' );
  116. }
  117. catch ( ezcBasePropertyNotFoundException $e )
  118. {
  119. $this->assertEquals( "No such property name 'no_such_property'.", $e->getMessage() );
  120. }
  121. try
  122. {
  123. $options->preload = 'wrong value';
  124. $this->fail( 'Expected exception was not thrown.' );
  125. }
  126. catch ( ezcBaseValueException $e )
  127. {
  128. $this->assertEquals( "The value 'wrong value' that you were trying to assign to setting 'preload' is invalid. Allowed values are: bool.", $e->getMessage() );
  129. }
  130. }
  131. public function testIterator()
  132. {
  133. $options = new ezcBaseTestOptions();
  134. $expectedArray = array( "foo" => "bar", "baz" => "blah" );
  135. $resultArray = array();
  136. foreach( $options as $key => $option )
  137. {
  138. $resultArray[$key] = $option;
  139. }
  140. $this->assertEquals( $expectedArray, $resultArray );
  141. }
  142. }
  143. ?>