NumberHelperTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\TestSuite\TestCase;
  21. use Cake\View\Helper\NumberHelper;
  22. use Cake\View\View;
  23. /**
  24. * NumberHelperTestObject class
  25. */
  26. class NumberHelperTestObject extends NumberHelper
  27. {
  28. public function attach(NumberMock $cakeNumber)
  29. {
  30. $this->_engine = $cakeNumber;
  31. }
  32. public function engine()
  33. {
  34. return $this->_engine;
  35. }
  36. }
  37. /**
  38. * NumberMock class
  39. */
  40. class NumberMock
  41. {
  42. }
  43. /**
  44. * NumberHelperTest class
  45. */
  46. class NumberHelperTest extends TestCase
  47. {
  48. /**
  49. * setUp method
  50. *
  51. * @return void
  52. */
  53. public function setUp()
  54. {
  55. parent::setUp();
  56. $this->View = new View();
  57. $this->_appNamespace = Configure::read('App.namespace');
  58. static::setAppNamespace();
  59. }
  60. /**
  61. * tearDown method
  62. *
  63. * @return void
  64. */
  65. public function tearDown()
  66. {
  67. parent::tearDown();
  68. $this->clearPlugins();
  69. static::setAppNamespace($this->_appNamespace);
  70. unset($this->View);
  71. }
  72. /**
  73. * Provider for method proxying.
  74. *
  75. * @return array
  76. */
  77. public function methodProvider()
  78. {
  79. return [
  80. ['precision'],
  81. ['toReadableSize'],
  82. ['toPercentage'],
  83. ['currency'],
  84. ['format'],
  85. ['addFormat'],
  86. ['formatDelta'],
  87. ['defaultCurrency'],
  88. ['ordinal'],
  89. ];
  90. }
  91. /**
  92. * test CakeNumber class methods are called correctly
  93. *
  94. * @dataProvider methodProvider
  95. * @return void
  96. */
  97. public function testNumberHelperProxyMethodCalls($method)
  98. {
  99. $number = $this->getMockBuilder(__NAMESPACE__ . '\NumberMock')
  100. ->setMethods([$method])
  101. ->getMock();
  102. $helper = new NumberHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\NumberMock']);
  103. $helper->attach($number);
  104. $number->expects($this->at(0))
  105. ->method($method)
  106. ->with(12.3);
  107. $helper->{$method}(12.3, ['options']);
  108. }
  109. /**
  110. * test engine override
  111. *
  112. * @return void
  113. */
  114. public function testEngineOverride()
  115. {
  116. $Number = new NumberHelperTestObject($this->View, ['engine' => 'TestAppEngine']);
  117. $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Number->engine());
  118. $this->loadPlugins(['TestPlugin']);
  119. $Number = new NumberHelperTestObject($this->View, ['engine' => 'TestPlugin.TestPluginEngine']);
  120. $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Number->engine());
  121. $this->removePlugins(['TestPlugin']);
  122. }
  123. }