NumberHelperTest.php 4.3 KB

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