PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Webdav/tests/server_configuration_test.php

https://github.com/Yannix/zetacomponents
PHP | 548 lines | 454 code | 57 blank | 37 comment | 3 complexity | 3372ca38681c94e48a70b1f11fa6680e MD5 | raw file
  1. <?php
  2. /**
  3. * Basic test cases for the memory backend.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Webdav
  23. * @subpackage Tests
  24. * @version //autogentag//
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Reqiuire base test
  29. */
  30. /**
  31. * Custom classes to test inheritence.
  32. */
  33. require_once 'classes/foo_custom_classes.php';
  34. /**
  35. * Tests for ezcWebdavServerConfiguration class.
  36. *
  37. * @package Webdav
  38. * @subpackage Tests
  39. */
  40. class ezcWebdavServerConfigurationTest extends ezcTestCase
  41. {
  42. public static function suite()
  43. {
  44. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  45. }
  46. public function testCtorSuccess()
  47. {
  48. $cfg = new ezcWebdavServerConfiguration();
  49. $this->assertAttributeEquals(
  50. array(
  51. 'userAgentRegex' => '(.*)',
  52. 'transportClass' => 'ezcWebdavTransport',
  53. 'xmlToolClass' => 'ezcWebdavXmlTool',
  54. 'propertyHandlerClass' => 'ezcWebdavPropertyHandler',
  55. 'headerHandlerClass' => 'ezcWebdavHeaderHandler',
  56. 'pathFactory' => new ezcWebdavAutomaticPathFactory(),
  57. ),
  58. 'properties',
  59. $cfg,
  60. 'Default properties not created correctly on empty ctor.'
  61. );
  62. $cfg = new ezcWebdavServerConfiguration(
  63. '(.*Nautilus.*)'
  64. );
  65. $this->assertAttributeEquals(
  66. array(
  67. 'userAgentRegex' => '(.*Nautilus.*)',
  68. 'transportClass' => 'ezcWebdavTransport',
  69. 'xmlToolClass' => 'ezcWebdavXmlTool',
  70. 'propertyHandlerClass' => 'ezcWebdavPropertyHandler',
  71. 'headerHandlerClass' => 'ezcWebdavHeaderHandler',
  72. 'pathFactory' => new ezcWebdavAutomaticPathFactory(),
  73. ),
  74. 'properties',
  75. $cfg,
  76. 'Default properties not created correctly on empty ctor.'
  77. );
  78. $cfg = new ezcWebdavServerConfiguration(
  79. '(.*Nautilus.*)',
  80. 'ezcWebdavCustomTransport'
  81. );
  82. $this->assertAttributeEquals(
  83. array(
  84. 'userAgentRegex' => '(.*Nautilus.*)',
  85. 'transportClass' => 'ezcWebdavCustomTransport',
  86. 'xmlToolClass' => 'ezcWebdavXmlTool',
  87. 'propertyHandlerClass' => 'ezcWebdavPropertyHandler',
  88. 'headerHandlerClass' => 'ezcWebdavHeaderHandler',
  89. 'pathFactory' => new ezcWebdavAutomaticPathFactory(),
  90. ),
  91. 'properties',
  92. $cfg,
  93. 'Default properties not created correctly on empty ctor.'
  94. );
  95. $cfg = new ezcWebdavServerConfiguration(
  96. '(.*Nautilus.*)',
  97. 'fooCustomTransport',
  98. 'fooCustomXmlTool'
  99. );
  100. $this->assertAttributeEquals(
  101. array(
  102. 'userAgentRegex' => '(.*Nautilus.*)',
  103. 'transportClass' => 'fooCustomTransport',
  104. 'xmlToolClass' => 'fooCustomXmlTool',
  105. 'propertyHandlerClass' => 'ezcWebdavPropertyHandler',
  106. 'headerHandlerClass' => 'ezcWebdavHeaderHandler',
  107. 'pathFactory' => new ezcWebdavAutomaticPathFactory(),
  108. ),
  109. 'properties',
  110. $cfg,
  111. 'Default properties not created correctly on empty ctor.'
  112. );
  113. $cfg = new ezcWebdavServerConfiguration(
  114. '(.*Nautilus.*)',
  115. 'fooCustomTransport',
  116. 'fooCustomXmlTool',
  117. 'fooCustomPropertyHandler'
  118. );
  119. $this->assertAttributeEquals(
  120. array(
  121. 'userAgentRegex' => '(.*Nautilus.*)',
  122. 'transportClass' => 'fooCustomTransport',
  123. 'xmlToolClass' => 'fooCustomXmlTool',
  124. 'propertyHandlerClass' => 'fooCustomPropertyHandler',
  125. 'headerHandlerClass' => 'ezcWebdavHeaderHandler',
  126. 'pathFactory' => new ezcWebdavAutomaticPathFactory(),
  127. ),
  128. 'properties',
  129. $cfg,
  130. 'Default properties not created correctly on empty ctor.'
  131. );
  132. $cfg = new ezcWebdavServerConfiguration(
  133. '(.*Nautilus.*)',
  134. 'fooCustomTransport',
  135. 'fooCustomXmlTool',
  136. 'fooCustomPropertyHandler',
  137. 'fooCustomHeaderHandler'
  138. );
  139. $this->assertAttributeEquals(
  140. array(
  141. 'userAgentRegex' => '(.*Nautilus.*)',
  142. 'transportClass' => 'fooCustomTransport',
  143. 'xmlToolClass' => 'fooCustomXmlTool',
  144. 'propertyHandlerClass' => 'fooCustomPropertyHandler',
  145. 'headerHandlerClass' => 'fooCustomHeaderHandler',
  146. 'pathFactory' => new ezcWebdavAutomaticPathFactory(),
  147. ),
  148. 'properties',
  149. $cfg,
  150. 'Default properties not created correctly on empty ctor.'
  151. );
  152. $cfg = new ezcWebdavServerConfiguration(
  153. '(.*Nautilus.*)',
  154. 'fooCustomTransport',
  155. 'fooCustomXmlTool',
  156. 'fooCustomPropertyHandler',
  157. 'fooCustomHeaderHandler',
  158. new ezcWebdavBasicPathFactory( 'http://example.com' )
  159. );
  160. $this->assertAttributeEquals(
  161. array(
  162. 'userAgentRegex' => '(.*Nautilus.*)',
  163. 'transportClass' => 'fooCustomTransport',
  164. 'xmlToolClass' => 'fooCustomXmlTool',
  165. 'propertyHandlerClass' => 'fooCustomPropertyHandler',
  166. 'headerHandlerClass' => 'fooCustomHeaderHandler',
  167. 'pathFactory' => new ezcWebdavBasicPathFactory( 'http://example.com' ),
  168. ),
  169. 'properties',
  170. $cfg,
  171. 'Default properties not created correctly on empty ctor.'
  172. );
  173. }
  174. public function testCtorFailure()
  175. {
  176. $typicalFails = array(
  177. '',
  178. 23,
  179. 23.42,
  180. true,
  181. false,
  182. array(),
  183. new stdClass(),
  184. );
  185. $typicalValid = 'fooSomeClass';
  186. $validCtorParams = array(
  187. $typicalValid, // userAgentRegex
  188. $typicalValid, // transportClass
  189. $typicalValid, // xmlToolClass
  190. $typicalValid, // propertyHandlerClass
  191. $typicalValid, // headerHandlerClass
  192. new ezcWebdavAutomaticPathFactory(), // pathFactory
  193. );
  194. $invalidCtorParams = array(
  195. $typicalFails, // userAgentRegex
  196. $typicalFails, // transportClass
  197. $typicalFails, // xmlToolClass
  198. $typicalFails, // propertyHandlerClass
  199. $typicalFails, // headerHandlerClass
  200. array_merge( $typicalFails, array( 'foo' ) ), // pathFactory
  201. );
  202. foreach ( $invalidCtorParams as $id => $paramSet )
  203. {
  204. $params = array();
  205. for ( $i = 0; $i < $id; ++$i )
  206. {
  207. $params[$i] = $validCtorParams[$i];
  208. }
  209. foreach ( $paramSet as $param )
  210. {
  211. $params[$id] = $param;
  212. $this->assertCtorFailure( $params, ( $i !== 5 ? 'ezcBaseValueException' : 'PHPUnit_Framework_Error' ) );
  213. }
  214. }
  215. }
  216. public function testGetPropertiesDefaultSuccess()
  217. {
  218. $cfg = new ezcWebdavServerConfiguration();
  219. $defaults = array(
  220. 'userAgentRegex' => '(.*)',
  221. 'transportClass' => 'ezcWebdavTransport',
  222. 'xmlToolClass' => 'ezcWebdavXmlTool',
  223. 'propertyHandlerClass' => 'ezcWebdavPropertyHandler',
  224. 'headerHandlerClass' => 'ezcWebdavHeaderHandler',
  225. 'pathFactory' => new ezcWebdavAutomaticPathFactory(),
  226. );
  227. foreach ( $defaults as $property => $value )
  228. {
  229. $this->assertEquals(
  230. $value,
  231. $cfg->$property,
  232. "Property $property has incorrect default."
  233. );
  234. }
  235. }
  236. public function testGetPropertiesFromCtorSuccess()
  237. {
  238. $cfg = new ezcWebdavServerConfiguration(
  239. '(.*Nautilus.*)',
  240. 'fooCustomTransport',
  241. 'fooCustomXmlTool',
  242. 'fooCustomPropertyHandler',
  243. 'fooCustomHeaderHandler',
  244. new ezcWebdavBasicPathFactory( 'http://example.com' )
  245. );
  246. $values = array(
  247. 'userAgentRegex' => '(.*Nautilus.*)',
  248. 'transportClass' => 'fooCustomTransport',
  249. 'xmlToolClass' => 'fooCustomXmlTool',
  250. 'propertyHandlerClass' => 'fooCustomPropertyHandler',
  251. 'headerHandlerClass' => 'fooCustomHeaderHandler',
  252. 'pathFactory' => new ezcWebdavBasicPathFactory( 'http://example.com' ),
  253. );
  254. foreach ( $values as $property => $value )
  255. {
  256. $this->assertEquals(
  257. $value,
  258. $cfg->$property,
  259. "Property $property has incorrect value after ctor setting."
  260. );
  261. }
  262. }
  263. public function testGetPropertiesFailure()
  264. {
  265. $cfg = new ezcWebdavServerConfiguration();
  266. try
  267. {
  268. echo $cfg->foo;
  269. }
  270. catch ( ezcBasePropertyNotFoundException $e )
  271. {
  272. return;
  273. }
  274. $this->fail( 'Property not thrown on get access of non-existent property.' );
  275. }
  276. public function testSetPropertiesGetPropertiesSuccess()
  277. {
  278. $cfg = new ezcWebdavServerConfiguration();
  279. $values = array(
  280. 'userAgentRegex' => '(.*Nautilus.*)',
  281. 'transportClass' => 'fooCustomTransport',
  282. 'xmlToolClass' => 'fooCustomXmlTool',
  283. 'propertyHandlerClass' => 'fooCustomPropertyHandler',
  284. 'headerHandlerClass' => 'fooCustomHeaderHandler',
  285. 'pathFactory' => new ezcWebdavBasicPathFactory( 'http://example.com' ),
  286. );
  287. foreach( $values as $property => $value )
  288. {
  289. $cfg->$property = $value;
  290. }
  291. $this->assertAttributeEquals(
  292. $values,
  293. 'properties',
  294. $cfg
  295. );
  296. foreach ( $values as $property => $value )
  297. {
  298. $this->assertEquals(
  299. $value,
  300. $cfg->$property,
  301. "Property $property has incorrect value after ctor setting."
  302. );
  303. }
  304. }
  305. public function testSetAccessFailure()
  306. {
  307. $typicalFails = array(
  308. '',
  309. 23,
  310. 23.42,
  311. true,
  312. false,
  313. array(),
  314. new stdClass(),
  315. );
  316. $invalidValues = array(
  317. 'userAgentRegex' => $typicalFails,
  318. 'transportClass' => $typicalFails,
  319. 'xmlToolClass' => $typicalFails,
  320. 'propertyHandlerClass' => $typicalFails,
  321. 'headerHandlerClass' => $typicalFails,
  322. 'pathFactory' => array_merge( $typicalFails, array( 'foo' ) ),
  323. );
  324. foreach ( $invalidValues as $propertyName => $propertyValues )
  325. {
  326. $this->assertSetPropertyFailure( $propertyName, $propertyValues, 'ezcBaseValueException' );
  327. }
  328. try
  329. {
  330. $cfg = new ezcWebdavServerConfiguration();
  331. $cfg->fooBar = 23;
  332. $this->fail( 'Exception not thrown on set access to non-existent property.' );
  333. }
  334. catch ( ezcBasePropertyNotFoundException $e ){}
  335. }
  336. public function testPropertiesIssetAccessDefaultCtorSuccess()
  337. {
  338. $cfg = new ezcWebdavServerConfiguration();
  339. $properties =array(
  340. 'userAgentRegex',
  341. 'transportClass',
  342. 'xmlToolClass',
  343. 'propertyHandlerClass',
  344. 'headerHandlerClass',
  345. 'pathFactory',
  346. );
  347. foreach( $properties as $propertyName )
  348. {
  349. $this->assertTrue(
  350. isset( $cfg->$propertyName ),
  351. "Property not set after default construction: '$propertyName'."
  352. );
  353. }
  354. }
  355. public function testPropertiesIssetAccessNonDefaultCtorSuccess()
  356. {
  357. $cfg = new ezcWebdavServerConfiguration(
  358. '(.*Nautilus.*)',
  359. 'fooCustomTransport',
  360. 'fooCustomXmlTool',
  361. 'fooCustomPropertyHandler',
  362. 'fooCustomHeaderHandler',
  363. new ezcWebdavBasicPathFactory( 'http://example.com' )
  364. );
  365. $properties =array(
  366. 'userAgentRegex',
  367. 'transportClass',
  368. 'xmlToolClass',
  369. 'propertyHandlerClass',
  370. 'headerHandlerClass',
  371. 'pathFactory',
  372. );
  373. foreach( $properties as $propertyName )
  374. {
  375. $this->assertTrue(
  376. isset( $cfg->$propertyName ),
  377. "Property not set after default construction: '$propertyName'."
  378. );
  379. }
  380. }
  381. public function testPropertyIssetAccessFailure()
  382. {
  383. $cfg = new ezcWebdavServerConfiguration();
  384. $this->assertFalse(
  385. isset( $cfg->foo ),
  386. 'Non-existent property $foo seems to be set.'
  387. );
  388. $this->assertFalse(
  389. isset( $cfg->properties ),
  390. 'Non-existent property $properties seems to be set.'
  391. );
  392. }
  393. public function testGetTransportInstanceSuccessDefaultCtor()
  394. {
  395. $cfg = new ezcWebdavServerConfiguration();
  396. $server = ezcWebdavServer::getInstance();
  397. $cfg->configure( $server );
  398. $pathFactory = new ezcWebdavAutomaticPathFactory();
  399. $xmlTool = new ezcWebdavXmlTool();
  400. $propertyHandler = new ezcWebdavPropertyHandler();
  401. $transport = new ezcWebdavTransport();
  402. $this->assertEquals(
  403. $xmlTool,
  404. $server->xmlTool
  405. );
  406. $this->assertEquals(
  407. $pathFactory,
  408. $server->pathFactory
  409. );
  410. $this->assertEquals(
  411. $propertyHandler,
  412. $server->propertyHandler
  413. );
  414. $this->assertEquals(
  415. $transport,
  416. $server->transport
  417. );
  418. }
  419. public function testGetTransportInstanceSuccessNonDefaultCtor()
  420. {
  421. $cfg = new ezcWebdavServerConfiguration(
  422. '(.*Nautilus.*)',
  423. 'fooCustomTransport',
  424. 'fooCustomXmlTool',
  425. 'fooCustomPropertyHandler',
  426. 'fooCustomHeaderHandler',
  427. new ezcWebdavBasicPathFactory( 'http://foo.example.com/webdav/' )
  428. );
  429. $server = ezcWebdavServer::getInstance();
  430. $cfg->configure( $server );
  431. $xmlTool = new fooCustomXmlTool();
  432. $pathFactory = new ezcWebdavBasicPathFactory( 'http://foo.example.com/webdav/' );
  433. $propertyHandler = new fooCustomPropertyHandler();
  434. $headerHandler = new fooCustomHeaderHandler();
  435. $transport = new fooCustomTransport();
  436. $this->assertEquals(
  437. $xmlTool,
  438. $server->xmlTool
  439. );
  440. $this->assertEquals(
  441. $pathFactory,
  442. $server->pathFactory
  443. );
  444. $this->assertEquals(
  445. $propertyHandler,
  446. $server->propertyHandler
  447. );
  448. $this->assertEquals(
  449. $transport,
  450. $server->transport
  451. );
  452. }
  453. protected function assertCtorFailure( array $args, $exceptionClass )
  454. {
  455. try
  456. {
  457. $cfgClass = new ReflectionClass( 'ezcWebdavServerConfiguration' );
  458. $cfg = $cfgClass->newInstanceArgs( $args );
  459. }
  460. catch( Exception $e )
  461. {
  462. ( !( $e instanceof $exceptionClass ) ? var_dump( $e ) : null );
  463. $this->assertTrue(
  464. ( $e instanceof $exceptionClass ),
  465. "Exception thrown on invalid value set of wrong exception class. '" . get_class( $e ) . "' instead of '$exceptionClass'."
  466. );
  467. return;
  468. }
  469. $this->fail( "Exception not thrown on invalid argument set." );
  470. }
  471. protected function assertSetPropertyFailure( $propertyName, array $propertyValues, $exceptionClass )
  472. {
  473. foreach ( $propertyValues as $value )
  474. {
  475. try
  476. {
  477. $cfg = new ezcWebdavServerConfiguration();
  478. $cfg->$propertyName = $value;
  479. $this->fail( "Exception not thrown on invalid ___set() value for property '$propertyName'." );
  480. }
  481. catch( Exception $e )
  482. {
  483. $this->assertTrue(
  484. ( $e instanceof $exceptionClass ),
  485. "Exception thrown on invalid value set for property '$propertyName'. '" . get_class( $e ) . "' instead of '$exceptionClass'."
  486. );
  487. }
  488. }
  489. }
  490. }
  491. ?>