2012-10-25 10:44:47 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Cell;
|
2016-03-22 14:35:50 +00:00
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder;
|
2017-10-29 08:39:42 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
2017-04-14 05:22:33 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Collection\Cells;
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
2017-10-29 08:39:42 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2017-11-08 15:48:01 +00:00
|
|
|
class AdvancedValueBinderTest extends TestCase
|
2012-10-25 10:44:47 +00:00
|
|
|
{
|
Restoring State After Static Changes in Tests (#1571)
This request does not change any source code, only tests.
For a change on which I was working, a test passed when run on its own,
but failed when run as part of the full test suite. It turned out that
an existing test had changed a static value,
thousands separator in this case, and failed to restore it.
The test turned out to be AdvancedBinderTest.
The search for the offending test was more difficult than it should have
been because 26 test scripts which had nothing to do with thousands
separator nevertheless changed that value. They all changed
decimal separator, currency code, and compatibility mode as well,
again for no reason. I changed all of those to eliminate those operations.
I changed the following tests, which actually do change the static
properties identified above for a reason, to restore them as part of teardown.
- CalculationTest sets compatibilityMode and locale
- DayTest sets compatibilityMode, returnDateType, and excelCalendar
- CountTest sets compatibilityMode
- FunctionsTest sets compatibilityMode and returnDateType
- AdvancedValueBinderTest sets currencyCode, decimalSeparator, thousandsSeparator
- StringHelperTest sets currencyCode, decimalSeparator, thousandsSeparator
- NumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
- HtmlNumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
2020-07-15 11:23:00 +00:00
|
|
|
private $currencyCode;
|
|
|
|
|
|
|
|
private $decimalSeparator;
|
|
|
|
|
|
|
|
private $thousandsSeparator;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->currencyCode = StringHelper::getCurrencyCode();
|
|
|
|
$this->decimalSeparator = StringHelper::getDecimalSeparator();
|
|
|
|
$this->thousandsSeparator = StringHelper::getThousandsSeparator();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
StringHelper::setCurrencyCode($this->currencyCode);
|
|
|
|
StringHelper::setDecimalSeparator($this->decimalSeparator);
|
|
|
|
StringHelper::setThousandsSeparator($this->thousandsSeparator);
|
|
|
|
}
|
|
|
|
|
2012-10-25 10:44:47 +00:00
|
|
|
public function provider()
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
$currencyUSD = NumberFormat::FORMAT_CURRENCY_USD_SIMPLE;
|
|
|
|
$currencyEURO = str_replace('$', '€', NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
|
2012-10-25 10:44:47 +00:00
|
|
|
|
2016-08-16 15:33:57 +00:00
|
|
|
return [
|
|
|
|
['10%', 0.1, NumberFormat::FORMAT_PERCENTAGE_00, ',', '.', '$'],
|
|
|
|
['$10.11', 10.11, $currencyUSD, ',', '.', '$'],
|
|
|
|
['$1,010.12', 1010.12, $currencyUSD, ',', '.', '$'],
|
|
|
|
['$20,20', 20.2, $currencyUSD, '.', ',', '$'],
|
|
|
|
['$2.020,20', 2020.2, $currencyUSD, '.', ',', '$'],
|
|
|
|
['€2.020,20', 2020.2, $currencyEURO, '.', ',', '€'],
|
|
|
|
['€ 2.020,20', 2020.2, $currencyEURO, '.', ',', '€'],
|
|
|
|
['€2,020.22', 2020.22, $currencyEURO, ',', '.', '€'],
|
|
|
|
];
|
2012-10-25 10:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provider
|
2016-12-22 14:43:37 +00:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @param mixed $valueBinded
|
|
|
|
* @param mixed $format
|
|
|
|
* @param mixed $thousandsSeparator
|
|
|
|
* @param mixed $decimalSeparator
|
|
|
|
* @param mixed $currencyCode
|
2012-10-25 10:44:47 +00:00
|
|
|
*/
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testCurrency($value, $valueBinded, $format, $thousandsSeparator, $decimalSeparator, $currencyCode): void
|
2012-10-25 10:44:47 +00:00
|
|
|
{
|
2017-01-23 07:02:59 +00:00
|
|
|
$sheet = $this->getMockBuilder(Worksheet::class)
|
2017-04-14 05:22:33 +00:00
|
|
|
->setMethods(['getStyle', 'getNumberFormat', 'setFormatCode', 'getCellCollection'])
|
2017-01-23 07:02:59 +00:00
|
|
|
->getMock();
|
2017-04-14 05:22:33 +00:00
|
|
|
$cellCollection = $this->getMockBuilder(Cells::class)
|
2015-05-17 13:00:02 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2020-05-18 04:49:57 +00:00
|
|
|
$cellCollection->expects(self::any())
|
2017-01-05 03:26:20 +00:00
|
|
|
->method('getParent')
|
2020-05-18 04:49:57 +00:00
|
|
|
->willReturn($sheet);
|
2012-10-25 10:44:47 +00:00
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
$sheet->expects(self::once())
|
2017-01-05 03:26:20 +00:00
|
|
|
->method('getStyle')
|
2020-05-18 04:49:57 +00:00
|
|
|
->willReturnSelf();
|
|
|
|
$sheet->expects(self::once())
|
2017-01-05 03:26:20 +00:00
|
|
|
->method('getNumberFormat')
|
2020-05-18 04:49:57 +00:00
|
|
|
->willReturnSelf();
|
|
|
|
$sheet->expects(self::once())
|
2017-01-05 03:26:20 +00:00
|
|
|
->method('setFormatCode')
|
|
|
|
->with($format)
|
2020-05-18 04:49:57 +00:00
|
|
|
->willReturnSelf();
|
|
|
|
$sheet->expects(self::any())
|
2017-04-14 05:22:33 +00:00
|
|
|
->method('getCellCollection')
|
2020-05-18 04:49:57 +00:00
|
|
|
->willReturn($cellCollection);
|
2012-10-25 10:44:47 +00:00
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
StringHelper::setCurrencyCode($currencyCode);
|
|
|
|
StringHelper::setDecimalSeparator($decimalSeparator);
|
|
|
|
StringHelper::setThousandsSeparator($thousandsSeparator);
|
2012-10-25 10:44:47 +00:00
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
$cell = new Cell(null, DataType::TYPE_STRING, $sheet);
|
2012-10-25 10:44:47 +00:00
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
$binder = new AdvancedValueBinder();
|
2012-10-25 10:44:47 +00:00
|
|
|
$binder->bindValue($cell, $value);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($valueBinded, $cell->getValue());
|
2012-10-25 10:44:47 +00:00
|
|
|
}
|
2015-05-17 16:34:30 +00:00
|
|
|
}
|