2017-04-14 05:22:33 +00:00
< ? php
namespace PhpOffice\PhpSpreadsheetTests\Collection ;
2017-10-29 08:39:42 +00:00
use PhpOffice\PhpSpreadsheet\Cell\Cell ;
2017-04-14 05:22:33 +00:00
use PhpOffice\PhpSpreadsheet\Collection\Cells ;
use PhpOffice\PhpSpreadsheet\Collection\Memory ;
use PhpOffice\PhpSpreadsheet\Spreadsheet ;
2017-10-29 08:39:42 +00:00
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet ;
2017-05-17 22:02:17 +00:00
use PHPUnit_Framework_TestCase ;
2017-04-14 05:22:33 +00:00
2017-05-17 22:02:17 +00:00
class CellsTest extends PHPUnit_Framework_TestCase
2017-04-14 05:22:33 +00:00
{
public function testCollectionCell ()
{
$spreadsheet = new Spreadsheet ();
$sheet = $spreadsheet -> getActiveSheet ();
$collection = $sheet -> getCellCollection ();
// Assert empty state
2017-09-20 05:55:42 +00:00
self :: assertEquals ([], $collection -> getCoordinates (), 'cell list should be empty' );
self :: assertEquals ([], $collection -> getSortedCoordinates (), 'sorted cell list should be empty' );
self :: assertNull ( $collection -> get ( 'B2' ), 'getting non-existing cell must return null' );
self :: assertFalse ( $collection -> has ( 'B2' ), 'non-existing cell should be non-existent' );
2017-04-14 05:22:33 +00:00
// Add one cell
$cell1 = $sheet -> getCell ( 'B2' );
2017-09-20 05:55:42 +00:00
self :: assertSame ( $cell1 , $collection -> add ( 'B2' , $cell1 ), 'adding a cell should return the cell' );
2017-04-14 05:22:33 +00:00
// Assert cell presence
2017-09-20 05:55:42 +00:00
self :: assertEquals ([ 'B2' ], $collection -> getCoordinates (), 'cell list should contains the cell' );
self :: assertEquals ([ 'B2' ], $collection -> getSortedCoordinates (), 'sorted cell list contains the cell' );
self :: assertSame ( $cell1 , $collection -> get ( 'B2' ), 'should get exact same object' );
self :: assertTrue ( $collection -> has ( 'B2' ), 'cell should exists' );
2017-04-14 05:22:33 +00:00
// Add a second cell
$cell2 = $sheet -> getCell ( 'A1' );
2017-09-20 05:55:42 +00:00
self :: assertSame ( $cell2 , $collection -> add ( 'A1' , $cell2 ), 'adding a second cell should return the cell' );
self :: assertEquals ([ 'B2' , 'A1' ], $collection -> getCoordinates (), 'cell list should contains the cell' );
self :: assertEquals ([ 'A1' , 'B2' ], $collection -> getSortedCoordinates (), 'sorted cell list contains the cell' );
2017-04-14 05:22:33 +00:00
// Assert collection copy
$sheet2 = $spreadsheet -> createSheet ();
$collection2 = $collection -> cloneCellCollection ( $sheet2 );
2017-09-20 05:55:42 +00:00
self :: assertTrue ( $collection2 -> has ( 'A1' ));
2017-04-14 05:22:33 +00:00
$copiedCell2 = $collection2 -> get ( 'A1' );
2017-09-20 05:55:42 +00:00
self :: assertNotSame ( $cell2 , $copiedCell2 , 'copied cell should not be the same object any more' );
self :: assertSame ( $collection2 , $copiedCell2 -> getParent (), 'copied cell should be owned by the copied collection' );
self :: assertSame ( 'A1' , $copiedCell2 -> getCoordinate (), 'copied cell should keep attributes' );
2017-04-14 05:22:33 +00:00
// Assert deletion
$collection -> delete ( 'B2' );
2017-09-20 05:55:42 +00:00
self :: assertFalse ( $collection -> has ( 'B2' ), 'cell should have been deleted' );
self :: assertEquals ([ 'A1' ], $collection -> getCoordinates (), 'cell list should contains the cell' );
2017-04-14 05:22:33 +00:00
// Assert update
$cell2 = $sheet -> getCell ( 'A1' );
2017-09-20 05:55:42 +00:00
self :: assertSame ( $sheet -> getCellCollection (), $collection );
self :: assertSame ( $cell2 , $collection -> update ( $cell2 ), 'should update existing cell' );
2017-04-14 05:22:33 +00:00
$cell3 = $sheet -> getCell ( 'C3' );
2017-09-20 05:55:42 +00:00
self :: assertSame ( $cell3 , $collection -> update ( $cell3 ), 'should silently add non-existing cell' );
self :: assertEquals ([ 'A1' , 'C3' ], $collection -> getCoordinates (), 'cell list should contains the cell' );
2017-04-14 05:22:33 +00:00
}
public function testCacheLastCell ()
{
$workbook = new Spreadsheet ();
$cells = [ 'A1' , 'A2' ];
$sheet = $workbook -> getActiveSheet ();
$sheet -> setCellValue ( 'A1' , 1 );
$sheet -> setCellValue ( 'A2' , 2 );
2017-09-20 05:55:42 +00:00
self :: assertEquals ( $cells , $sheet -> getCoordinates (), 'list should include last added cell' );
2017-04-14 05:22:33 +00:00
}
public function testCanGetCellAfterAnotherIsDeleted ()
{
$workbook = new Spreadsheet ();
$sheet = $workbook -> getActiveSheet ();
$collection = $sheet -> getCellCollection ();
$sheet -> setCellValue ( 'A1' , 1 );
$sheet -> setCellValue ( 'A2' , 1 );
$collection -> delete ( 'A1' );
$sheet -> setCellValue ( 'A3' , 1 );
2017-09-20 05:55:42 +00:00
self :: assertNotNull ( $collection -> get ( 'A2' ), 'should be able to get back the cell even when another cell was deleted while this one was the current one' );
2017-04-14 05:22:33 +00:00
}
/**
* @ expectedException \PhpOffice\PhpSpreadsheet\Exception
*/
public function testThrowsWhenCellCannotBeRetrievedFromCache ()
{
$collection = $this -> getMockBuilder ( Cells :: class )
-> setConstructorArgs ([ new Worksheet (), new Memory ()])
-> setMethods ([ 'has' ])
-> getMock ();
$collection -> method ( 'has' )
-> willReturn ( true );
$collection -> get ( 'A2' );
}
/**
* @ expectedException \PhpOffice\PhpSpreadsheet\Exception
*/
public function testThrowsWhenCellCannotBeStoredInCache ()
{
$cache = $this -> createMock ( Memory :: class );
$cell = $this -> createMock ( Cell :: class );
$cache -> method ( 'set' )
-> willReturn ( false );
$collection = new Cells ( new Worksheet (), $cache );
$collection -> add ( 'A1' , $cell );
$collection -> add ( 'A2' , $cell );
}
}