ArrayNodeConfigTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Test\Unit\Dom;
  7. use \Magento\Framework\Config\Dom\ArrayNodeConfig;
  8. class ArrayNodeConfigTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var ArrayNodeConfig
  12. */
  13. protected $object;
  14. /**
  15. * @var NodePathMatcher|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $nodePathMatcher;
  18. protected function setUp()
  19. {
  20. $this->nodePathMatcher = $this->createMock(\Magento\Framework\Config\Dom\NodePathMatcher::class);
  21. $this->object = new ArrayNodeConfig(
  22. $this->nodePathMatcher,
  23. ['/root/assoc/one' => 'name', '/root/assoc/two' => 'id', '/root/assoc/three' => 'key'],
  24. ['/root/numeric/one', '/root/numeric/two', '/root/numeric/three']
  25. );
  26. }
  27. public function testIsNumericArrayMatched()
  28. {
  29. $xpath = '/root/numeric[@attr="value"]/two';
  30. $this->nodePathMatcher->expects(
  31. $this->at(0)
  32. )->method(
  33. 'match'
  34. )->with(
  35. '/root/numeric/one',
  36. $xpath
  37. )->will(
  38. $this->returnValue(false)
  39. );
  40. $this->nodePathMatcher->expects(
  41. $this->at(1)
  42. )->method(
  43. 'match'
  44. )->with(
  45. '/root/numeric/two',
  46. $xpath
  47. )->will(
  48. $this->returnValue(true)
  49. );
  50. $this->assertTrue($this->object->isNumericArray($xpath));
  51. }
  52. public function testIsNumericArrayNotMatched()
  53. {
  54. $xpath = '/root/numeric[@attr="value"]/four';
  55. $this->nodePathMatcher->expects(
  56. $this->at(0)
  57. )->method(
  58. 'match'
  59. )->with(
  60. '/root/numeric/one',
  61. $xpath
  62. )->will(
  63. $this->returnValue(false)
  64. );
  65. $this->nodePathMatcher->expects(
  66. $this->at(1)
  67. )->method(
  68. 'match'
  69. )->with(
  70. '/root/numeric/two',
  71. $xpath
  72. )->will(
  73. $this->returnValue(false)
  74. );
  75. $this->nodePathMatcher->expects(
  76. $this->at(2)
  77. )->method(
  78. 'match'
  79. )->with(
  80. '/root/numeric/three',
  81. $xpath
  82. )->will(
  83. $this->returnValue(false)
  84. );
  85. $this->assertFalse($this->object->isNumericArray($xpath));
  86. }
  87. public function testGetAssocArrayKeyAttributeMatched()
  88. {
  89. $xpath = '/root/assoc[@attr="value"]/two';
  90. $this->nodePathMatcher->expects(
  91. $this->at(0)
  92. )->method(
  93. 'match'
  94. )->with(
  95. '/root/assoc/one',
  96. $xpath
  97. )->will(
  98. $this->returnValue(false)
  99. );
  100. $this->nodePathMatcher->expects(
  101. $this->at(1)
  102. )->method(
  103. 'match'
  104. )->with(
  105. '/root/assoc/two',
  106. $xpath
  107. )->will(
  108. $this->returnValue(true)
  109. );
  110. $this->assertEquals('id', $this->object->getAssocArrayKeyAttribute($xpath));
  111. }
  112. public function testGetAssocArrayKeyAttributeNotMatched()
  113. {
  114. $xpath = '/root/assoc[@attr="value"]/four';
  115. $this->nodePathMatcher->expects(
  116. $this->at(0)
  117. )->method(
  118. 'match'
  119. )->with(
  120. '/root/assoc/one',
  121. $xpath
  122. )->will(
  123. $this->returnValue(false)
  124. );
  125. $this->nodePathMatcher->expects(
  126. $this->at(1)
  127. )->method(
  128. 'match'
  129. )->with(
  130. '/root/assoc/two',
  131. $xpath
  132. )->will(
  133. $this->returnValue(false)
  134. );
  135. $this->nodePathMatcher->expects(
  136. $this->at(2)
  137. )->method(
  138. 'match'
  139. )->with(
  140. '/root/assoc/three',
  141. $xpath
  142. )->will(
  143. $this->returnValue(false)
  144. );
  145. $this->assertNull($this->object->getAssocArrayKeyAttribute($xpath));
  146. }
  147. }