Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chained border operations on cell ranges operating on last cell only #578

Merged
merged 1 commit into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Cell formats with escaped spaces were causing incorrect date formatting - [#557](https://github.com/PHPOffice/PhpSpreadsheet/issues/557)
- Could not open CSV file containing HTML fragment - [#564](https://github.com/PHPOffice/PhpSpreadsheet/issues/564)
- Exclude the vendor folder in migration - [#481](https://github.com/PHPOffice/PhpSpreadsheet/issues/481)
- Chained operations on cell ranges involving borders operated on last cell only [#428](https://github.com/PHPOffice/PhpSpreadsheet/issues/428)

## [1.3.1] - 2018-06-12

Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Style/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ public function applyFromArray(array $pStyles, $pAdvanced = true)
}
}

// restore initial cell selection range
$this->getActiveSheet()->getStyle($pRange);

return $this;
}

Expand Down
73 changes: 73 additions & 0 deletions tests/PhpSpreadsheetTests/Style/BorderRangeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Style;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PHPUnit\Framework\TestCase;

class BorderRangeTest extends TestCase
{
public function testBorderRangeInAction()
{
// testcase for the initial bug problem: setting border+color fails
// set red borders aroundlA1:B3 square. Verify that the borders set are actually correct

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$argb = 'FFFF0000';
$color = new Color($argb);

$sheet->getStyle('A1:C1')->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN)->setColor($color);
$sheet->getStyle('A1:A3')->getBorders()->getLeft()->setBorderStyle(Border::BORDER_THIN)->setColor($color);
$sheet->getStyle('C1:C3')->getBorders()->getRight()->setBorderStyle(Border::BORDER_THIN)->setColor($color);
$sheet->getStyle('A3:C3')->getBorders()->getBottom()->setBorderStyle(Border::BORDER_THIN)->setColor($color);

// upper row
$expectations = [
// cell => Left/Right/Top/Bottom
'A1' => 'LT',
'B1' => 'T',
'C1' => 'RT',
'A2' => 'L',
'B2' => '',
'C2' => 'R',
'A3' => 'LB',
'B3' => 'B',
'C3' => 'RB',
];
$sides = [
'L' => 'Left',
'R' => 'Right',
'T' => 'Top',
'B' => 'Bottom',
];

foreach ($expectations as $cell => $borders) {
$bs = $sheet->getStyle($cell)->getBorders();
foreach ($sides as $sidekey => $side) {
$assertion = "setBorderStyle on a range of cells, $cell $side";
$func = "get$side";
$b = $bs->$func(); // boo

if (strpos($borders, $sidekey) === false) {
self::assertSame(Border::BORDER_NONE, $b->getBorderStyle(), $assertion);
} else {
self::assertSame(Border::BORDER_THIN, $b->getBorderStyle(), $assertion);
self::assertSame($argb, $b->getColor()->getARGB(), $assertion);
}
}
}
}

public function testBorderRangeDirectly()
{
// testcase for the underlying problem directly
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$style = $sheet->getStyle('A1:C1')->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN);
self::assertSame('A1:C1', $style->getSelectedCells(), 'getSelectedCells should not change after a style operation on a border range');
}
}