NumberHelperTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * NumberHelperTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 1.2.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\I18n\Number;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Helper\NumberHelper;
  23. use Cake\View\View;
  24. use ReflectionMethod;
  25. /**
  26. * NumberHelperTestObject class
  27. */
  28. class NumberHelperTestObject extends NumberHelper
  29. {
  30. public function attach(NumberMock $cakeNumber)
  31. {
  32. $this->_engine = $cakeNumber;
  33. }
  34. public function engine()
  35. {
  36. return $this->_engine;
  37. }
  38. }
  39. /**
  40. * NumberMock class
  41. */
  42. class NumberMock
  43. {
  44. }
  45. /**
  46. * NumberHelperTest class
  47. */
  48. class NumberHelperTest extends TestCase
  49. {
  50. /**
  51. * setUp method
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. parent::setUp();
  58. $this->View = new View();
  59. $this->_appNamespace = Configure::read('App.namespace');
  60. static::setAppNamespace();
  61. }
  62. /**
  63. * tearDown method
  64. *
  65. * @return void
  66. */
  67. public function tearDown()
  68. {
  69. parent::tearDown();
  70. $this->clearPlugins();
  71. static::setAppNamespace($this->_appNamespace);
  72. unset($this->View);
  73. }
  74. /**
  75. * Provider for method proxying.
  76. *
  77. * @return array
  78. */
  79. public function methodProvider()
  80. {
  81. return [
  82. ['precision'],
  83. ['toReadableSize'],
  84. ['toPercentage'],
  85. ['currency'],
  86. ['format'],
  87. ['formatDelta'],
  88. ['defaultCurrency'],
  89. ['ordinal'],
  90. ];
  91. }
  92. /**
  93. * test CakeNumber class methods are called correctly
  94. *
  95. * @dataProvider methodProvider
  96. * @return void
  97. */
  98. public function testNumberHelperProxyMethodCalls($method)
  99. {
  100. $number = $this->getMockBuilder(__NAMESPACE__ . '\NumberMock')
  101. ->setMethods([$method])
  102. ->getMock();
  103. $helper = new NumberHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\NumberMock']);
  104. $helper->attach($number);
  105. $number->expects($this->at(0))
  106. ->method($method)
  107. ->with(12.3);
  108. $helper->{$method}(12.3, ['options']);
  109. }
  110. /**
  111. * Test that number of argument of helper's proxy methods matches
  112. * corresponding method of Number class.
  113. *
  114. * @dataProvider methodProvider
  115. * @return void
  116. */
  117. public function testParameterCountMatch($method)
  118. {
  119. $numberMethod = new ReflectionMethod(Number::class, $method);
  120. $helperMethod = new ReflectionMethod(NumberHelper::class, $method);
  121. $this->assertSame($numberMethod->getNumberOfParameters(), $helperMethod->getNumberOfParameters());
  122. }
  123. /**
  124. * test engine override
  125. *
  126. * @return void
  127. */
  128. public function testEngineOverride()
  129. {
  130. $Number = new NumberHelperTestObject($this->View, ['engine' => 'TestAppEngine']);
  131. $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Number->engine());
  132. $this->loadPlugins(['TestPlugin']);
  133. $Number = new NumberHelperTestObject($this->View, ['engine' => 'TestPlugin.TestPluginEngine']);
  134. $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Number->engine());
  135. $this->removePlugins(['TestPlugin']);
  136. }
  137. }