CellAddressTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Cell;
  3. use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
  4. use PhpOffice\PhpSpreadsheet\Exception;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PHPUnit\Framework\TestCase;
  7. class CellAddressTest extends TestCase
  8. {
  9. /**
  10. * @dataProvider providerCreateFromCellAddress
  11. */
  12. public function testCreateFromCellAddress(
  13. string $cellAddress,
  14. string $expectedColumnName,
  15. int $expectedColumnId,
  16. int $expectedRowId
  17. ): void {
  18. $cellAddressObject = CellAddress::fromCellAddress($cellAddress);
  19. self::assertSame($cellAddress, (string) $cellAddressObject);
  20. self::assertSame($cellAddress, $cellAddressObject->cellAddress());
  21. self::assertSame($expectedRowId, $cellAddressObject->rowId());
  22. self::assertSame($expectedColumnId, $cellAddressObject->columnId());
  23. self::assertSame($expectedColumnName, $cellAddressObject->columnName());
  24. }
  25. public function providerCreateFromCellAddress(): array
  26. {
  27. return [
  28. ['A1', 'A', 1, 1],
  29. ['C5', 'C', 3, 5],
  30. ['IV256', 'IV', 256, 256],
  31. ];
  32. }
  33. /**
  34. * @dataProvider providerCreateFromCellAddressException
  35. *
  36. * @param mixed $cellAddress
  37. */
  38. public function testCreateFromCellAddressException($cellAddress): void
  39. {
  40. $this->expectException(Exception::class);
  41. $this->expectExceptionMessage(
  42. $cellAddress === ''
  43. ? 'Cell coordinate can not be zero-length string'
  44. : "Invalid cell coordinate {$cellAddress}"
  45. );
  46. CellAddress::fromCellAddress($cellAddress);
  47. }
  48. public function providerCreateFromCellAddressException(): array
  49. {
  50. return [
  51. ['INVALID'],
  52. [''],
  53. ['IV'],
  54. ['12'],
  55. [123],
  56. ];
  57. }
  58. /**
  59. * @dataProvider providerCreateFromColumnAndRow
  60. */
  61. public function testCreateFromColumnAndRow(
  62. int $columnId,
  63. int $rowId,
  64. string $expectedCellAddress,
  65. string $expectedColumnName
  66. ): void {
  67. $cellAddressObject = CellAddress::fromColumnAndRow($columnId, $rowId);
  68. self::assertSame($expectedCellAddress, (string) $cellAddressObject);
  69. self::assertSame($expectedCellAddress, $cellAddressObject->cellAddress());
  70. self::assertSame($rowId, $cellAddressObject->rowId());
  71. self::assertSame($columnId, $cellAddressObject->columnId());
  72. self::assertSame($expectedColumnName, $cellAddressObject->columnName());
  73. }
  74. /**
  75. * @dataProvider providerCreateFromColumnRowException
  76. *
  77. * @param mixed $columnId
  78. * @param mixed $rowId
  79. */
  80. public function testCreateFromColumnRowException($columnId, $rowId): void
  81. {
  82. $this->expectException(Exception::class);
  83. $this->expectExceptionMessage('Row and Column Ids must be positive integer values');
  84. CellAddress::fromColumnAndRow($columnId, $rowId);
  85. }
  86. public function providerCreateFromColumnAndRow(): array
  87. {
  88. return [
  89. [1, 1, 'A1', 'A'],
  90. [3, 5, 'C5', 'C'],
  91. [256, 256, 'IV256', 'IV'],
  92. ];
  93. }
  94. /**
  95. * @dataProvider providerCreateFromColumnRowArray
  96. */
  97. public function testCreateFromColumnRowArray(
  98. int $columnId,
  99. int $rowId,
  100. string $expectedCellAddress,
  101. string $expectedColumnName
  102. ): void {
  103. $columnRowArray = [$columnId, $rowId];
  104. $cellAddressObject = CellAddress::fromColumnRowArray($columnRowArray);
  105. self::assertSame($expectedCellAddress, (string) $cellAddressObject);
  106. self::assertSame($expectedCellAddress, $cellAddressObject->cellAddress());
  107. self::assertSame($rowId, $cellAddressObject->rowId());
  108. self::assertSame($columnId, $cellAddressObject->columnId());
  109. self::assertSame($expectedColumnName, $cellAddressObject->columnName());
  110. }
  111. public function providerCreateFromColumnRowArray(): array
  112. {
  113. return [
  114. [1, 1, 'A1', 'A'],
  115. [3, 5, 'C5', 'C'],
  116. [256, 256, 'IV256', 'IV'],
  117. ];
  118. }
  119. /**
  120. * @dataProvider providerCreateFromColumnRowException
  121. *
  122. * @param mixed $columnId
  123. * @param mixed $rowId
  124. */
  125. public function testCreateFromColumnRowArrayException($columnId, $rowId): void
  126. {
  127. $this->expectException(Exception::class);
  128. $this->expectExceptionMessage('Row and Column Ids must be positive integer values');
  129. $columnRowArray = [$columnId, $rowId];
  130. CellAddress::fromColumnRowArray($columnRowArray);
  131. }
  132. public function providerCreateFromColumnRowException(): array
  133. {
  134. return [
  135. [-1, 1],
  136. [3, 'A'],
  137. ];
  138. }
  139. /**
  140. * @dataProvider providerCreateFromCellAddressWithWorksheet
  141. */
  142. public function testCreateFromCellAddressWithWorksheet(
  143. string $cellAddress,
  144. string $expectedCellAddress,
  145. string $expectedColumnName,
  146. int $expectedColumnId,
  147. int $expectedRowId
  148. ): void {
  149. $spreadsheet = new Spreadsheet();
  150. $worksheet = $spreadsheet->getActiveSheet();
  151. $worksheet->setTitle("Mark's Worksheet");
  152. $cellAddressObject = CellAddress::fromCellAddress($cellAddress, $worksheet);
  153. self::assertSame($expectedCellAddress, (string) $cellAddressObject);
  154. self::assertSame($cellAddress, $cellAddressObject->cellAddress());
  155. self::assertSame($expectedRowId, $cellAddressObject->rowId());
  156. self::assertSame($expectedColumnId, $cellAddressObject->columnId());
  157. self::assertSame($expectedColumnName, $cellAddressObject->columnName());
  158. }
  159. public function providerCreateFromCellAddressWithWorksheet(): array
  160. {
  161. return [
  162. ['A1', "'Mark''s Worksheet'!A1", 'A', 1, 1],
  163. ['C5', "'Mark''s Worksheet'!C5", 'C', 3, 5],
  164. ['IV256', "'Mark''s Worksheet'!IV256", 'IV', 256, 256],
  165. ];
  166. }
  167. public function testNextRow(): void
  168. {
  169. $cellAddress = CellAddress::fromCellAddress('C5');
  170. // default single row
  171. $cellAddressC6 = $cellAddress->nextRow();
  172. self::assertSame('C6', (string) $cellAddressC6);
  173. // multiple rows
  174. $cellAddressC9 = $cellAddress->nextRow(4);
  175. self::assertSame('C9', (string) $cellAddressC9);
  176. // negative rows
  177. $cellAddressC3 = $cellAddress->nextRow(-2);
  178. self::assertSame('C3', (string) $cellAddressC3);
  179. // negative beyond the minimum
  180. $cellAddressC1 = $cellAddress->nextRow(-10);
  181. self::assertSame('C1', (string) $cellAddressC1);
  182. // Check that the original object is still unchanged
  183. self::assertSame('C5', (string) $cellAddress);
  184. }
  185. public function testPreviousRow(): void
  186. {
  187. $cellAddress = CellAddress::fromCellAddress('C5');
  188. // default single row
  189. $cellAddressC4 = $cellAddress->previousRow();
  190. self::assertSame('C4', (string) $cellAddressC4);
  191. }
  192. public function testNextColumn(): void
  193. {
  194. $cellAddress = CellAddress::fromCellAddress('C5');
  195. // default single row
  196. $cellAddressD5 = $cellAddress->nextColumn();
  197. self::assertSame('D5', (string) $cellAddressD5);
  198. // multiple rows
  199. $cellAddressG5 = $cellAddress->nextColumn(4);
  200. self::assertSame('G5', (string) $cellAddressG5);
  201. // negative rows
  202. $cellAddressB5 = $cellAddress->nextColumn(-1);
  203. self::assertSame('B5', (string) $cellAddressB5);
  204. // negative beyond the minimum
  205. $cellAddressA5 = $cellAddress->nextColumn(-10);
  206. self::assertSame('A5', (string) $cellAddressA5);
  207. // Check that the original object is still unchanged
  208. self::assertSame('C5', (string) $cellAddress);
  209. }
  210. public function testPreviousColumn(): void
  211. {
  212. $cellAddress = CellAddress::fromCellAddress('C5');
  213. // default single row
  214. $cellAddressC4 = $cellAddress->previousColumn();
  215. self::assertSame('B5', (string) $cellAddressC4);
  216. }
  217. }