PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/unit/testsuite/Magento/Validator/ConfigTest.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 194 lines | 117 code | 14 blank | 63 comment | 3 complexity | 43ceac9bcc72c188f7b85fb292dc4edd MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Magento
  22. * @package Magento_Validator
  23. * @subpackage unit_tests
  24. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  25. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. */
  27. class Magento_Validator_ConfigTest extends PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * @var Magento_Validator_Config
  31. */
  32. protected static $_model = null;
  33. public static function setUpBeforeClass()
  34. {
  35. self::$_model = new Magento_Validator_Config(glob(__DIR__ . '/_files/validation/positive/*/validation.xml'));
  36. }
  37. /**
  38. * @expectedException InvalidArgumentException
  39. */
  40. public function testConstructException()
  41. {
  42. new Magento_Validator_Config(array());
  43. }
  44. /**
  45. * @expectedException InvalidArgumentException
  46. */
  47. public function testGetValidationRulesInvalidEntityName()
  48. {
  49. self::$_model->getValidationRules('invalid_entity', null);
  50. }
  51. /**
  52. * @expectedException InvalidArgumentException
  53. */
  54. public function testGetValidationRulesInvalidGroupName()
  55. {
  56. self::$_model->getValidationRules('test_entity', 'invalid_group');
  57. }
  58. /**
  59. * @expectedException InvalidArgumentException
  60. */
  61. public function testGetValidationRulesInvalidZendConstraint()
  62. {
  63. $configFile = glob(__DIR__ . '/_files/validation/negative/invalid_zend_constraint.xml');
  64. $config = new Magento_Validator_Config($configFile);
  65. $config->getValidationRules('test_entity', 'test_group_a');
  66. }
  67. /**
  68. * @expectedException InvalidArgumentException
  69. */
  70. public function testGetValidationRulesInvalidMagentoConstraint()
  71. {
  72. $configFile = glob(__DIR__ . '/_files/validation/negative/invalid_magento_constraint.xml');
  73. $config = new Magento_Validator_Config($configFile);
  74. $config->getValidationRules('test_entity', 'test_group_a');
  75. }
  76. /**
  77. * @dataProvider getValidationRulesDataProvider
  78. * @param string $entityName
  79. * @param string $groupName
  80. * @param array $expectedRules
  81. */
  82. public function testGetValidationRules($entityName, $groupName, $expectedRules)
  83. {
  84. $actualRules = self::$_model->getValidationRules($entityName, $groupName);
  85. $this->assertRulesEqual($expectedRules, $actualRules);
  86. }
  87. /**
  88. * Assert that all expected validation rules are present with correct constraint objects.
  89. *
  90. * @param array $expectedRules
  91. * @param array $actualRules
  92. */
  93. public function assertRulesEqual(array $expectedRules, array $actualRules)
  94. {
  95. foreach ($expectedRules as $expectedRule => $expectedConstraints) {
  96. $this->assertArrayHasKey($expectedRule, $actualRules);
  97. foreach ($expectedConstraints as $expectedConstraint) {
  98. $constraintFound = false;
  99. foreach ($actualRules[$expectedRule] as $actualConstraint) {
  100. if ($expectedConstraint['constraint'] instanceof $actualConstraint['constraint']) {
  101. $constraintFound = true;
  102. if (isset($expectedConstraint['field'])) {
  103. $this->assertArrayHasKey('field', $actualConstraint);
  104. $this->assertEquals($expectedConstraint['field'], $actualConstraint['field']);
  105. }
  106. break;
  107. }
  108. }
  109. if (!$constraintFound) {
  110. $this->fail(sprintf('Expected constraint "%s" was not found in the rule "%"',
  111. get_class($expectedConstraint['constraint']), $expectedRule));
  112. }
  113. }
  114. }
  115. }
  116. public function getValidationRulesDataProvider()
  117. {
  118. $groupARules = array(
  119. 'test_rule_zend' => array(
  120. array(
  121. 'constraint' => $this->getMock('Zend_Validate_Alnum'),
  122. 'field' => 'test_field'
  123. ),
  124. ),
  125. 'test_rule_constraint' => array(
  126. array(
  127. 'constraint' => $this->getMock('Magento_Validator_Test'),
  128. ),
  129. ),
  130. );
  131. $groupBRules = array(
  132. 'test_rule_constraint' => array(
  133. array(
  134. 'constraint' => $this->getMock('Magento_Validator_Test'),
  135. ),
  136. ),
  137. 'test_rule_constraint_2' => array(
  138. array(
  139. 'constraint' => $this->getMock('Magento_Validator_Test'),
  140. 'field' => 'constraint_field'
  141. ),
  142. ),
  143. );
  144. $groupCRules = array(
  145. 'test_rule' => array(
  146. array(
  147. 'constraint' => $this->getMock('Zend_Validate_Int'),
  148. 'field' => 'test_field'
  149. ),
  150. ),
  151. );
  152. return array(
  153. array('test_entity', 'test_group_a', $groupARules),
  154. array('test_entity', 'test_group_b', $groupBRules),
  155. array('test_entity_b', 'test_group_c', $groupCRules),
  156. );
  157. }
  158. public function testGetSchemaFile()
  159. {
  160. $this->assertFileExists(self::$_model->getSchemaFile());
  161. }
  162. }
  163. /** Dummy classes to test that constraint classes extend correct abstract. */
  164. class Magento_Validator_Invalid_Abstract
  165. {
  166. }
  167. class Magento_Validator_Test extends Magento_Validator_ConstraintAbstract
  168. {
  169. /**
  170. * @param array $data
  171. * @param null $field
  172. * @return bool
  173. *
  174. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  175. */
  176. public function isValidData(array $data, $field = null)
  177. {
  178. return true;
  179. }
  180. }