From 2b1e81c53560d5acf12b778d9ec6a75777527dca Mon Sep 17 00:00:00 2001 From: Anush Ramani Date: Tue, 10 Jul 2018 16:30:26 +0530 Subject: [PATCH 1/4] XOL-4064 replaced PHPExcel with PhpSpreadsheet --- PHPExcelFactory.php | 47 ---- composer.json | 8 +- composer.lock | 213 ++++++++++++------ .../XolaReportWriterExtension.php | 5 +- .../Resources}/config/services.yml | 2 +- {Service => src/Service}/AbstractWriter.php | 1 - {Service => src/Service}/CSVWriter.php | 0 {Service => src/Service}/ExcelWriter.php | 66 +++--- src/SpreadsheetFactory.php | 50 ++++ {Tests => test}/Service/CSVWriterTest.php | 1 - {Tests => test}/Service/ExcelWriterTest.php | 100 ++++---- {Tests => test}/bootstrap.php | 0 phpunit.xml.dist => test/phpunit.xml.dist | 4 +- 13 files changed, 295 insertions(+), 202 deletions(-) delete mode 100644 PHPExcelFactory.php rename {DependencyInjection => src/DependencyInjection}/XolaReportWriterExtension.php (96%) rename {Resources => src/Resources}/config/services.yml (89%) rename {Service => src/Service}/AbstractWriter.php (98%) rename {Service => src/Service}/CSVWriter.php (100%) rename {Service => src/Service}/ExcelWriter.php (76%) create mode 100644 src/SpreadsheetFactory.php rename {Tests => test}/Service/CSVWriterTest.php (98%) rename {Tests => test}/Service/ExcelWriterTest.php (56%) rename {Tests => test}/bootstrap.php (100%) rename phpunit.xml.dist => test/phpunit.xml.dist (84%) diff --git a/PHPExcelFactory.php b/PHPExcelFactory.php deleted file mode 100644 index b1f9d51..0000000 --- a/PHPExcelFactory.php +++ /dev/null @@ -1,47 +0,0 @@ -phpExcelIO = $phpExcelIO; - } - - /** - * Creates an empty PHPExcel Object if the filename is empty, otherwise loads the file into the object. - * - * @param string $filename - * - * @return \PHPExcel - */ - public function createPHPExcelObject($filename = null) - { - if (null == $filename) { - $phpExcelObject = new \PHPExcel(); - return $phpExcelObject; - } - return call_user_func(array($this->phpExcelIO, 'load'), $filename); - } - - /** - * Create a writer given the PHPExcelObject and the type, - * the type could be one of PHPExcel_IOFactory::$_autoResolveClasses - * - * @param \PHPExcel $phpExcelObject - * @param string $type - * - * - * @return \PHPExcel_Writer_IWriter - */ - public function createWriter(\PHPExcel $phpExcelObject, $type = 'Excel5') - { - return call_user_func(array($this->phpExcelIO, 'createWriter'), $phpExcelObject, $type); - } -} \ No newline at end of file diff --git a/composer.json b/composer.json index 0c1000b..a4810a1 100644 --- a/composer.json +++ b/composer.json @@ -2,10 +2,10 @@ "name": "xola/reportwriter-bundle", "description": "A generic report writer", "require": { - "php": ">=5.3.2", + "php": ">=5.6", "symfony/framework-bundle": "~2.1", "psr/log": "~1.0", - "phpoffice/phpexcel": "~1.8.0" + "phpoffice/phpspreadsheet": "^1.3" }, "license": "MIT", "authors": [ @@ -15,8 +15,8 @@ } ], "autoload": { - "psr-0": { - "Xola\\ReportWriterBundle": "" + "psr-4": { + "Xola\\ReportWriterBundle\\": "src/" } }, "target-dir": "Xola/ReportWriterBundle", diff --git a/composer.lock b/composer.lock index 6efa2dd..5dea4de 100644 --- a/composer.lock +++ b/composer.lock @@ -1,10 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "hash": "ed0ed8ac340fd58507e89e2bf3ffa800", + "content-hash": "ebcf30c76494dc642525a19c7eb51eda", "packages": [ { "name": "doctrine/annotations", @@ -72,7 +72,7 @@ "docblock", "parser" ], - "time": "2014-12-20 20:49:38" + "time": "2014-12-20T20:49:38+00:00" }, { "name": "doctrine/lexer", @@ -124,36 +124,62 @@ "lexer", "parser" ], - "time": "2013-01-12 18:59:04" + "time": "2013-01-12T18:59:04+00:00" }, { - "name": "phpoffice/phpexcel", - "version": "1.8.0", + "name": "phpoffice/phpspreadsheet", + "version": "1.3.1", "source": { "type": "git", - "url": "https://github.com/PHPOffice/PHPExcel.git", - "reference": "e69a5e4d0ffa7fb6f171859e0a04346e580df30b" + "url": "https://github.com/PHPOffice/PhpSpreadsheet.git", + "reference": "aa5b0d0236c907fd8dba0883f3ceb97cc52e46ec" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPOffice/PHPExcel/zipball/e69a5e4d0ffa7fb6f171859e0a04346e580df30b", - "reference": "e69a5e4d0ffa7fb6f171859e0a04346e580df30b", + "url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/aa5b0d0236c907fd8dba0883f3ceb97cc52e46ec", + "reference": "aa5b0d0236c907fd8dba0883f3ceb97cc52e46ec", "shasum": "" }, "require": { + "ext-ctype": "*", + "ext-dom": "*", + "ext-gd": "*", + "ext-iconv": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-simplexml": "*", "ext-xml": "*", + "ext-xmlreader": "*", "ext-xmlwriter": "*", - "php": ">=5.2.0" + "ext-zip": "*", + "ext-zlib": "*", + "php": "^5.6|^7.0", + "psr/simple-cache": "^1.0" + }, + "require-dev": { + "dompdf/dompdf": "^0.8.0", + "friendsofphp/php-cs-fixer": "@stable", + "jpgraph/jpgraph": "^4.0", + "mpdf/mpdf": "^7.0.0", + "phpunit/phpunit": "^5.7", + "squizlabs/php_codesniffer": "^2.7", + "tecnickcom/tcpdf": "^6.2" + }, + "suggest": { + "dompdf/dompdf": "Option for rendering PDF with PDF Writer", + "jpgraph/jpgraph": "Option for rendering charts, or including charts with PDF or HTML Writers", + "mpdf/mpdf": "Option for rendering PDF with PDF Writer", + "tecnick.com/tcpdf": "Option for rendering PDF with PDF Writer" }, "type": "library", "autoload": { - "psr-0": { - "PHPExcel": "Classes/" + "psr-4": { + "PhpOffice\\PhpSpreadsheet\\": "src/PhpSpreadsheet" } }, "notification-url": "https://packagist.org/downloads/", "license": [ - "LGPL" + "LGPL-2.1-or-later" ], "authors": [ { @@ -161,27 +187,30 @@ "homepage": "http://blog.maartenballiauw.be" }, { - "name": "Mark Baker" + "name": "Erik Tilt" }, { "name": "Franck Lefevre", - "homepage": "http://blog.rootslabs.net" + "homepage": "http://rootslabs.net" }, { - "name": "Erik Tilt" + "name": "Mark Baker", + "homepage": "http://markbakeruk.net" } ], - "description": "PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine", - "homepage": "http://phpexcel.codeplex.com", + "description": "PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine", + "homepage": "https://github.com/PHPOffice/PhpSpreadsheet", "keywords": [ "OpenXML", "excel", + "gnumeric", + "ods", "php", "spreadsheet", "xls", "xlsx" ], - "time": "2014-03-02 15:22:49" + "time": "2018-06-12T13:40:21+00:00" }, { "name": "psr/log", @@ -219,7 +248,55 @@ "psr", "psr-3" ], - "time": "2012-12-21 11:40:51" + "time": "2012-12-21T11:40:51+00:00" + }, + { + "name": "psr/simple-cache", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "time": "2017-10-23T01:57:42+00:00" }, { "name": "symfony/config", @@ -227,12 +304,12 @@ "target-dir": "Symfony/Component/Config", "source": { "type": "git", - "url": "https://github.com/symfony/Config.git", + "url": "https://github.com/symfony/config.git", "reference": "84c0c150c1520995f09ea9e47e817068b353cb0f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Config/zipball/84c0c150c1520995f09ea9e47e817068b353cb0f", + "url": "https://api.github.com/repos/symfony/config/zipball/84c0c150c1520995f09ea9e47e817068b353cb0f", "reference": "84c0c150c1520995f09ea9e47e817068b353cb0f", "shasum": "" }, @@ -267,7 +344,7 @@ ], "description": "Symfony Config Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/debug", @@ -275,12 +352,12 @@ "target-dir": "Symfony/Component/Debug", "source": { "type": "git", - "url": "https://github.com/symfony/Debug.git", + "url": "https://github.com/symfony/debug.git", "reference": "08b529b4c0aa3e746d187fe2a63f08cb955a3566" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Debug/zipball/08b529b4c0aa3e746d187fe2a63f08cb955a3566", + "url": "https://api.github.com/repos/symfony/debug/zipball/08b529b4c0aa3e746d187fe2a63f08cb955a3566", "reference": "08b529b4c0aa3e746d187fe2a63f08cb955a3566", "shasum": "" }, @@ -323,7 +400,7 @@ ], "description": "Symfony Debug Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/dependency-injection", @@ -331,12 +408,12 @@ "target-dir": "Symfony/Component/DependencyInjection", "source": { "type": "git", - "url": "https://github.com/symfony/DependencyInjection.git", + "url": "https://github.com/symfony/dependency-injection.git", "reference": "e2693382ef9456a7c7e382f34f813e4b4332941d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/DependencyInjection/zipball/e2693382ef9456a7c7e382f34f813e4b4332941d", + "url": "https://api.github.com/repos/symfony/dependency-injection/zipball/e2693382ef9456a7c7e382f34f813e4b4332941d", "reference": "e2693382ef9456a7c7e382f34f813e4b4332941d", "shasum": "" }, @@ -380,7 +457,7 @@ ], "description": "Symfony DependencyInjection Component", "homepage": "http://symfony.com", - "time": "2014-12-03 09:22:11" + "time": "2014-12-03T09:22:11+00:00" }, { "name": "symfony/event-dispatcher", @@ -388,12 +465,12 @@ "target-dir": "Symfony/Component/EventDispatcher", "source": { "type": "git", - "url": "https://github.com/symfony/EventDispatcher.git", + "url": "https://github.com/symfony/event-dispatcher.git", "reference": "720fe9bca893df7ad1b4546649473b5afddf0216" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/720fe9bca893df7ad1b4546649473b5afddf0216", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/720fe9bca893df7ad1b4546649473b5afddf0216", "reference": "720fe9bca893df7ad1b4546649473b5afddf0216", "shasum": "" }, @@ -438,7 +515,7 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/filesystem", @@ -446,12 +523,12 @@ "target-dir": "Symfony/Component/Filesystem", "source": { "type": "git", - "url": "https://github.com/symfony/Filesystem.git", + "url": "https://github.com/symfony/filesystem.git", "reference": "ff6efc95256cb33031933729e68b01d720b5436b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Filesystem/zipball/ff6efc95256cb33031933729e68b01d720b5436b", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/ff6efc95256cb33031933729e68b01d720b5436b", "reference": "ff6efc95256cb33031933729e68b01d720b5436b", "shasum": "" }, @@ -485,7 +562,7 @@ ], "description": "Symfony Filesystem Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/framework-bundle", @@ -493,12 +570,12 @@ "target-dir": "Symfony/Bundle/FrameworkBundle", "source": { "type": "git", - "url": "https://github.com/symfony/FrameworkBundle.git", + "url": "https://github.com/symfony/framework-bundle.git", "reference": "bea9435d5ca931b8831ba3e29baa4bfd3a80a889" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/FrameworkBundle/zipball/bea9435d5ca931b8831ba3e29baa4bfd3a80a889", + "url": "https://api.github.com/repos/symfony/framework-bundle/zipball/bea9435d5ca931b8831ba3e29baa4bfd3a80a889", "reference": "bea9435d5ca931b8831ba3e29baa4bfd3a80a889", "shasum": "" }, @@ -565,7 +642,7 @@ ], "description": "Symfony FrameworkBundle", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/http-foundation", @@ -573,12 +650,12 @@ "target-dir": "Symfony/Component/HttpFoundation", "source": { "type": "git", - "url": "https://github.com/symfony/HttpFoundation.git", + "url": "https://github.com/symfony/http-foundation.git", "reference": "0109221f3cf012bf027768ad3e4236dae1af5332" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/HttpFoundation/zipball/0109221f3cf012bf027768ad3e4236dae1af5332", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/0109221f3cf012bf027768ad3e4236dae1af5332", "reference": "0109221f3cf012bf027768ad3e4236dae1af5332", "shasum": "" }, @@ -618,7 +695,7 @@ ], "description": "Symfony HttpFoundation Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/http-kernel", @@ -626,12 +703,12 @@ "target-dir": "Symfony/Component/HttpKernel", "source": { "type": "git", - "url": "https://github.com/symfony/HttpKernel.git", + "url": "https://github.com/symfony/http-kernel.git", "reference": "6e911d8a9e1a11c4584ad7b03858afa94e627203" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/HttpKernel/zipball/6e911d8a9e1a11c4584ad7b03858afa94e627203", + "url": "https://api.github.com/repos/symfony/http-kernel/zipball/6e911d8a9e1a11c4584ad7b03858afa94e627203", "reference": "6e911d8a9e1a11c4584ad7b03858afa94e627203", "shasum": "" }, @@ -693,7 +770,7 @@ ], "description": "Symfony HttpKernel Component", "homepage": "http://symfony.com", - "time": "2014-12-03 16:40:43" + "time": "2014-12-03T16:40:43+00:00" }, { "name": "symfony/routing", @@ -701,12 +778,12 @@ "target-dir": "Symfony/Component/Routing", "source": { "type": "git", - "url": "https://github.com/symfony/Routing.git", + "url": "https://github.com/symfony/routing.git", "reference": "b50c10839e1639fb3a89710f9510b63cc8be54c5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Routing/zipball/b50c10839e1639fb3a89710f9510b63cc8be54c5", + "url": "https://api.github.com/repos/symfony/routing/zipball/b50c10839e1639fb3a89710f9510b63cc8be54c5", "reference": "b50c10839e1639fb3a89710f9510b63cc8be54c5", "shasum": "" }, @@ -760,7 +837,7 @@ "uri", "url" ], - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/security-core", @@ -822,7 +899,7 @@ ], "description": "Symfony Security Component - Core Library", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/security-csrf", @@ -876,7 +953,7 @@ ], "description": "Symfony Security Component - CSRF Library", "homepage": "http://symfony.com", - "time": "2014-11-28 10:00:40" + "time": "2014-11-28T10:00:40+00:00" }, { "name": "symfony/stopwatch", @@ -923,7 +1000,7 @@ ], "description": "Symfony Stopwatch Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/templating", @@ -976,7 +1053,7 @@ ], "description": "Symfony Templating Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" }, { "name": "symfony/translation", @@ -1034,7 +1111,7 @@ ], "description": "Symfony Translation Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" } ], "packages-dev": [ @@ -1090,7 +1167,7 @@ "constructor", "instantiate" ], - "time": "2014-10-13 12:58:55" + "time": "2014-10-13T12:58:55+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1155,7 +1232,7 @@ "testing", "xunit" ], - "time": "2014-12-03 06:41:44" + "time": "2014-12-03T06:41:44+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1200,7 +1277,7 @@ "filesystem", "iterator" ], - "time": "2013-10-10 15:34:57" + "time": "2013-10-10T15:34:57+00:00" }, { "name": "phpunit/php-text-template", @@ -1244,7 +1321,7 @@ "keywords": [ "template" ], - "time": "2014-01-30 17:20:04" + "time": "2014-01-30T17:20:04+00:00" }, { "name": "phpunit/php-timer", @@ -1288,7 +1365,7 @@ "keywords": [ "timer" ], - "time": "2013-08-02 07:42:54" + "time": "2013-08-02T07:42:54+00:00" }, { "name": "phpunit/php-token-stream", @@ -1337,7 +1414,7 @@ "keywords": [ "tokenizer" ], - "time": "2014-08-31 06:12:13" + "time": "2014-08-31T06:12:13+00:00" }, { "name": "phpunit/phpunit", @@ -1408,7 +1485,7 @@ "testing", "xunit" ], - "time": "2014-12-05 06:49:03" + "time": "2014-12-05T06:49:03+00:00" }, { "name": "phpunit/phpunit-mock-objects", @@ -1463,7 +1540,7 @@ "mock", "xunit" ], - "time": "2014-10-03 05:12:11" + "time": "2014-10-03T05:12:11+00:00" }, { "name": "sebastian/comparator", @@ -1527,7 +1604,7 @@ "compare", "equality" ], - "time": "2014-11-16 21:32:38" + "time": "2014-11-16T21:32:38+00:00" }, { "name": "sebastian/diff", @@ -1579,7 +1656,7 @@ "keywords": [ "diff" ], - "time": "2014-08-15 10:29:00" + "time": "2014-08-15T10:29:00+00:00" }, { "name": "sebastian/environment", @@ -1629,7 +1706,7 @@ "environment", "hhvm" ], - "time": "2014-10-25 08:00:45" + "time": "2014-10-25T08:00:45+00:00" }, { "name": "sebastian/exporter", @@ -1694,7 +1771,7 @@ "export", "exporter" ], - "time": "2014-09-10 00:51:36" + "time": "2014-09-10T00:51:36+00:00" }, { "name": "sebastian/global-state", @@ -1745,7 +1822,7 @@ "keywords": [ "global state" ], - "time": "2014-10-06 09:23:50" + "time": "2014-10-06T09:23:50+00:00" }, { "name": "sebastian/version", @@ -1780,7 +1857,7 @@ ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", "homepage": "https://github.com/sebastianbergmann/version", - "time": "2014-03-07 15:35:33" + "time": "2014-03-07T15:35:33+00:00" }, { "name": "symfony/yaml", @@ -1827,7 +1904,7 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2014-12-02T20:19:20+00:00" } ], "aliases": [], diff --git a/DependencyInjection/XolaReportWriterExtension.php b/src/DependencyInjection/XolaReportWriterExtension.php similarity index 96% rename from DependencyInjection/XolaReportWriterExtension.php rename to src/DependencyInjection/XolaReportWriterExtension.php index 5a95efa..3d75d68 100644 --- a/DependencyInjection/XolaReportWriterExtension.php +++ b/src/DependencyInjection/XolaReportWriterExtension.php @@ -2,10 +2,10 @@ namespace Xola\ReportWriterBundle\DependencyInjection; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; /** * This is the class that loads and manages your bundle configuration @@ -16,6 +16,7 @@ class XolaReportWriterExtension extends Extension { /** * {@inheritDoc} + * @throws \Exception */ public function load(array $configs, ContainerBuilder $container) { diff --git a/Resources/config/services.yml b/src/Resources/config/services.yml similarity index 89% rename from Resources/config/services.yml rename to src/Resources/config/services.yml index 09e4360..93c752a 100644 --- a/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -1,7 +1,7 @@ parameters: csvwriter.service.class: Xola\ReportWriterBundle\Service\CSVWriter excelwriter.service.class: Xola\ReportWriterBundle\Service\ExcelWriter - phpexcel.class: Xola\ReportWriterBundle\PHPExcelFactory + phpexcel.class: Xola\ReportWriterBundle\SpreadsheetFactory services: csvwriter: diff --git a/Service/AbstractWriter.php b/src/Service/AbstractWriter.php similarity index 98% rename from Service/AbstractWriter.php rename to src/Service/AbstractWriter.php index 7742920..a209e19 100644 --- a/Service/AbstractWriter.php +++ b/src/Service/AbstractWriter.php @@ -2,7 +2,6 @@ namespace Xola\ReportWriterBundle\Service; -use Symfony\Component\DependencyInjection\Container; use Psr\Log\LoggerInterface; abstract class AbstractWriter diff --git a/Service/CSVWriter.php b/src/Service/CSVWriter.php similarity index 100% rename from Service/CSVWriter.php rename to src/Service/CSVWriter.php diff --git a/Service/ExcelWriter.php b/src/Service/ExcelWriter.php similarity index 76% rename from Service/ExcelWriter.php rename to src/Service/ExcelWriter.php index 32d8b78..f549833 100644 --- a/Service/ExcelWriter.php +++ b/src/Service/ExcelWriter.php @@ -2,32 +2,38 @@ namespace Xola\ReportWriterBundle\Service; +use PhpOffice\PhpSpreadsheet\Shared\Font; +use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; use Psr\Log\LoggerInterface; -use Xola\ReportWriterBundle\PHPExcelFactory; +use Xola\ReportWriterBundle\SpreadsheetFactory; class ExcelWriter extends AbstractWriter { + /** @var SpreadsheetFactory */ private $phpexcel; - /* @var \PHPExcel $handle */ - private $handle; + /* @var Spreadsheet $spreadsheet */ + private $spreadsheet; private $currentRow = 1; - public function __construct(LoggerInterface $logger, PHPExcelFactory $phpExcel) + public function __construct(LoggerInterface $logger, SpreadsheetFactory $phpExcel) { parent::__construct($logger); $this->phpexcel = $phpExcel; - \PHPExcel_Shared_Font::setAutoSizeMethod(\PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT); + $this->spreadsheet = $phpExcel->createPhpSpreadsheetObject(); + Font::setAutoSizeMethod(Font::AUTOSIZE_METHOD_EXACT); } /** * Initialize the excel writer * * @param string $filepath + * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function setup($filepath) { - $this->handle = $this->phpexcel->createPHPExcelObject(); - $this->handle->getActiveSheet()->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); + $this->spreadsheet = $this->phpexcel->createPhpSpreadsheetObject(); + $this->spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE); $this->filepath = $filepath; } @@ -39,7 +45,7 @@ public function setup($filepath) */ public function setProperties($author = '', $title = '') { - $this->handle->getProperties() + $this->spreadsheet->getProperties() ->setCreator($author) ->setTitle($title); } @@ -49,13 +55,13 @@ public function setProperties($author = '', $title = '') * * @param int $index Location at which to create the sheet (NULL for last) * @param string $title The title of the sheet - * @throws \PHPExcel_Exception + * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function setWorksheet($index, $title) { - $this->handle->createSheet($index); + $this->spreadsheet->createSheet($index); $this->resetCurrentRow(1); - $this->handle->setActiveSheetIndex($index); + $this->spreadsheet->setActiveSheetIndex($index); $this->setSheetTitle($title); } @@ -63,10 +69,11 @@ public function setWorksheet($index, $title) * Set the title for the current active worksheet * * @param string $title + * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function setSheetTitle($title) { - $this->handle->getActiveSheet()->setTitle($title); + $this->spreadsheet->getActiveSheet()->setTitle($title); } /** @@ -75,11 +82,11 @@ public function setSheetTitle($title) * @param $headers * @param $initRow * - * @throws \PHPExcel_Exception + * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function writeHeaders($headers, $initRow = null) { - $worksheet = $this->handle->getActiveSheet(); + $worksheet = $this->spreadsheet->getActiveSheet(); $hasMultiRowHeaders = $this->hasMultiRowHeaders($headers); if ($initRow) { @@ -126,7 +133,7 @@ public function writeHeaders($headers, $initRow = null) $worksheet->getStyle($cell)->getFont()->setBold(true); } - $worksheet->calculateColumnWidths(true); + $worksheet->calculateColumnWidths(); $this->currentRow = $initRow + (($hasMultiRowHeaders) ? 2 : 1); } @@ -153,9 +160,10 @@ private function hasMultiRowHeaders($headers) /** * Utility method that will data from the cached file per row and write it * - * @param string $cacheFile Filename where the fetched data can be cached from - * @param array $sortedHeaders Headers to write sorted in the order you want them - * @param bool $freezeHeaders True if you want to freeze headers (default: false) + * @param string $cacheFile Filename where the fetched data can be cached from + * @param array $sortedHeaders Headers to write sorted in the order you want them + * @param bool $freezeHeaders True if you want to freeze headers (default: false) + * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function prepare($cacheFile, $sortedHeaders, $freezeHeaders = false) { @@ -217,12 +225,13 @@ public function writeRow($dataRow, $headers = []) /** * Write one or more rows starting at the given row and column * - * @param array $lines + * @param array $lines + * @throws \PhpOffice\PhpSpreadsheet\Exception */ private function writeArrays(array $lines) { $startCell = 'A' . $this->currentRow; - $this->handle->getActiveSheet()->fromArray($lines, null, $startCell, true); + $this->spreadsheet->getActiveSheet()->fromArray($lines, null, $startCell, true); $this->currentRow += count($lines); } @@ -230,11 +239,12 @@ private function writeArrays(array $lines) * Write a single row of data * * @param array $row A single row of data + * @throws \PhpOffice\PhpSpreadsheet\Exception */ private function writeArray(array $row) { $startCell = 'A' . $this->currentRow; - $this->handle->getActiveSheet()->fromArray([$row], null, $startCell, true); + $this->spreadsheet->getActiveSheet()->fromArray([$row], null, $startCell, true); $this->currentRow++; } @@ -242,42 +252,42 @@ private function writeArray(array $row) * Freeze panes at the given location so they stay fixed upon scroll * * @param string $cell - * @throws \PHPExcel_Exception + * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function freezePanes($cell = '') { if (empty($cell)) { $cell = 'A3'; } - $this->handle->getActiveSheet()->freezePane($cell); + $this->spreadsheet->getActiveSheet()->freezePane($cell); } /** * Add a horizonal (row) page break for print layout * * @param string $cell - * @throws \PHPExcel_Exception + * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function addHorizontalPageBreak($cell = '') { if (empty($cell)) { $cell = 'A' . ($this->currentRow - 2); } - $this->handle->getActiveSheet()->setBreak($cell, \PHPExcel_Worksheet::BREAK_ROW); + $this->spreadsheet->getActiveSheet()->setBreak($cell, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_ROW); } /** * Save the current data into an .xlsx file * - * @throws \PHPExcel_Exception + * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function finalize() { // Set active sheet index to the first sheet, so Excel opens this as the first sheet - $this->handle->setActiveSheetIndex(0); + $this->spreadsheet->setActiveSheetIndex(0); // Write the file to disk - $writer = $this->phpexcel->createWriter($this->handle, 'Excel2007'); + $writer = $this->phpexcel->createWriter($this->spreadsheet, 'Xlsx'); $writer->save($this->filepath); } diff --git a/src/SpreadsheetFactory.php b/src/SpreadsheetFactory.php new file mode 100644 index 0000000..4f78501 --- /dev/null +++ b/src/SpreadsheetFactory.php @@ -0,0 +1,50 @@ +phpExcelIO = $phpExcelIO; + } + + /** + * Creates an empty \PhpOffice\PhpSpreadsheet\Spreadsheet Object if the filename is empty, otherwise loads the file into the object. + * + * @param string $filename + * + * @return Spreadsheet + */ + public function createPhpSpreadsheetObject($filename = null) + { + if (null == $filename) { + $phpExcelObject = new Spreadsheet(); + return $phpExcelObject; + } + return call_user_func(array($this->phpExcelIO, 'load'), $filename); + } + + /** + * Create a writer given the \PhpOffice\PhpSpreadsheet\SpreadsheetObject and the type, + * the type could be one of \PhpOffice\PhpSpreadsheet\IOFactory::$_autoResolveClasses + * + * @param Spreadsheet $phpExcelObject + * @param string $type + * + * + * @return \PhpOffice\PhpSpreadsheet\Writer\IWriter + */ + public function createWriter(Spreadsheet $phpExcelObject, $type = 'Xls') + { + return call_user_func(array($this->phpExcelIO, 'createWriter'), $phpExcelObject, $type); + } +} \ No newline at end of file diff --git a/Tests/Service/CSVWriterTest.php b/test/Service/CSVWriterTest.php similarity index 98% rename from Tests/Service/CSVWriterTest.php rename to test/Service/CSVWriterTest.php index b53c472..7e2d50d 100644 --- a/Tests/Service/CSVWriterTest.php +++ b/test/Service/CSVWriterTest.php @@ -6,7 +6,6 @@ class CSVWriterTest extends PHPUnit_Framework_TestCase { /** @var CSVWriter */ private $writer; - private $handle; private $testfilename; public function setUp() diff --git a/Tests/Service/ExcelWriterTest.php b/test/Service/ExcelWriterTest.php similarity index 56% rename from Tests/Service/ExcelWriterTest.php rename to test/Service/ExcelWriterTest.php index a49f85f..51c72ca 100644 --- a/Tests/Service/ExcelWriterTest.php +++ b/test/Service/ExcelWriterTest.php @@ -1,23 +1,27 @@ phpExcelHandleMock = $this->getMockBuilder('\PHPExcel')->disableOriginalConstructor()->getMock(); + $this->spreadsheet = $this->getMockBuilder(Spreadsheet::class)->disableOriginalConstructor()->getMock(); } public function buildService($params = []) { - $defaults = ['logger' => $this->getMockBuilder('Psr\Log\LoggerInterface')->disableOriginalConstructor()->getMock()]; + $defaults = ['logger' => $this->getMock(LoggerInterface::class)]; if(!isset($params['phpExcel'])) { - $defaults['phpExcel'] = $this->getPHPExcelMock(); + $defaults['phpExcel'] = $this->getSpreadsheetFactoryMock(); } $params = array_merge($defaults, $params); @@ -28,11 +32,11 @@ public function buildService($params = []) public function testShouldInitializePHPExcelObject() { // Setup all the mocks - $pageSetupMock = $this->getMockBuilder('\PHPExcel_Worksheet_PageSetup')->disableOriginalConstructor()->getMock(); + $pageSetupMock = $this->getMockBuilder(PageSetup::class)->disableOriginalConstructor()->getMock(); $pageSetupMock->expects($this->once())->method('setOrientation')->with('landscape'); - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder(Worksheet::class)->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('getPageSetup')->willReturn($pageSetupMock); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $this->buildService()->setup("filename.xlsx"); } @@ -43,10 +47,10 @@ public function testShouldSetPropertiesForExcelFile() $title = 'bar'; // Setup all the mocks - $propertiesMock = $this->getMockBuilder('\PHPExcel_DocumentProperties')->disableOriginalConstructor()->getMock(); + $propertiesMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Document\Properties')->disableOriginalConstructor()->getMock(); $propertiesMock->expects($this->once())->method('setCreator')->with($author)->willReturn($propertiesMock); $propertiesMock->expects($this->once())->method('setTitle')->with($title)->willReturn($propertiesMock); - $this->phpExcelHandleMock->expects($this->once())->method('getProperties')->willReturn($propertiesMock); + $this->spreadsheet->expects($this->once())->method('getProperties')->willReturn($propertiesMock); $this->buildService()->setProperties($author, $title); } @@ -57,11 +61,11 @@ public function testShouldSetCurrentWorksheet() $title = 'Hello World'; // Setup all the mocks - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('setTitle')->with($title); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); - $this->phpExcelHandleMock->expects($this->once())->method('createSheet')->with($index); - $this->phpExcelHandleMock->expects($this->once())->method('setActiveSheetIndex')->with($index); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('createSheet')->with($index); + $this->spreadsheet->expects($this->once())->method('setActiveSheetIndex')->with($index); $service = $this->buildService(); @@ -71,14 +75,14 @@ public function testShouldSetCurrentWorksheet() public function testShouldWriteSingleRowHeaders() { - $columnDimensionMock = $this->getMockBuilder('\PHPExcel_Worksheet_ColumnDimension')->disableOriginalConstructor()->getMock(); + $columnDimensionMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension')->disableOriginalConstructor()->getMock(); $columnDimensionMock->expects($this->exactly(4))->method('setAutoSize')->with(true); - $phpExcelStyleMock2 = $this->getMockBuilder('\PHPExcel_Style_Font')->disableOriginalConstructor()->getMock(); + $phpExcelStyleMock2 = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Style\Font')->disableOriginalConstructor()->getMock(); $phpExcelStyleMock2->expects($this->exactly(4))->method('setBold')->with(true); - $phpExcelStyleMock = $this->getMockBuilder('\PHPExcel_Style')->disableOriginalConstructor()->getMock(); + $phpExcelStyleMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Style\Style')->disableOriginalConstructor()->getMock(); $phpExcelStyleMock->expects($this->exactly(4))->method('getFont')->willReturn($phpExcelStyleMock2); - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->exactly(4))->method('getStyle')->willReturn($phpExcelStyleMock); $worksheetMock->expects($this->exactly(4))->method('setCellValue')->withConsecutive( ['A1', 'Alpha'], ['B1', 'Bravo'], ['C1', 'Gamma'], ['D1', 'Delta'] @@ -86,7 +90,7 @@ public function testShouldWriteSingleRowHeaders() $worksheetMock->expects($this->exactly(4))->method('getColumnDimension')->withConsecutive( ['A'], ['B'], ['C'], ['D'] )->willReturn($columnDimensionMock); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $headers = ['Alpha', 'Bravo', 'Gamma', 'Delta']; $this->buildService()->writeHeaders($headers); @@ -94,15 +98,15 @@ public function testShouldWriteSingleRowHeaders() public function testShouldWriteNestedHeaders() { - $columnDimensionMock = $this->getMockBuilder('\PHPExcel_Worksheet_ColumnDimension')->disableOriginalConstructor()->getMock(); + $columnDimensionMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension')->disableOriginalConstructor()->getMock(); $columnDimensionMock->expects($this->exactly(6))->method('setAutoSize')->with(true); - $phpExcelStyleMock2 = $this->getMockBuilder('\PHPExcel_Style_Font')->disableOriginalConstructor()->getMock(); + $phpExcelStyleMock2 = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Style\Font')->disableOriginalConstructor()->getMock(); $phpExcelStyleMock2->expects($this->exactly(5))->method('setBold')->with(true); - $phpExcelStyleMock = $this->getMockBuilder('\PHPExcel_Style')->disableOriginalConstructor()->getMock(); + $phpExcelStyleMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Style\Style')->disableOriginalConstructor()->getMock(); $phpExcelStyleMock->expects($this->exactly(5))->method('getFont')->willReturn($phpExcelStyleMock2); - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->exactly(5))->method('getStyle')->willReturn($phpExcelStyleMock); $worksheetMock->expects($this->exactly(7))->method('setCellValue')->withConsecutive( ['A1', 'Alpha'], ['B1', 'Bravo'], ['C1', 'Gamma'], ['D1', 'Delta'], ['E1', 'Echo'], @@ -114,7 +118,7 @@ public function testShouldWriteNestedHeaders() $worksheetMock->expects($this->exactly(6))->method('getColumnDimension')->withConsecutive( ['A'], ['B'], ['C'], ['D'], ['E'], ['F'] )->willReturn($columnDimensionMock); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $headers = [0 => 'Alpha', 1 => 'Bravo', 2 => 'Gamma', 3 => 'Delta', 4 => ['Echo' => ['Foxtrot', 'Hotel']]]; $this->buildService()->writeHeaders($headers); @@ -133,9 +137,9 @@ public function testShouldWriteNonNestedData() 'This is A', 'This is B', 'This is G, we skipped C', 'This is Dee' ]]; - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('fromArray')->with($expected, null, 'A1'); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $headers = [0 => 'Alpha', 1 => 'Bravo', 2 => 'Gamma', 3 => 'Delta']; $this->buildService()->writeRow($input, $headers); @@ -158,9 +162,9 @@ public function testShouldWriteNestedRowData() 'This is A', 'This is B', 'This is G, we skipped C', 'This is Dee', 'Fancy, F', 'Etch' ]]; - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('fromArray')->with($expected, null, 'A1'); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $headers = [0 => 'Alpha', 1 => 'Bravo', 2 => 'Gamma', 3 => 'Delta', 4 => ['Echo' => ['Foxtrot', 'Hotel']]]; $this->buildService()->writeRow($input, $headers); @@ -169,18 +173,18 @@ public function testShouldWriteNestedRowData() public function testShouldWriteArrayAsDataStartingFromTheFirstCell() { $lines = ['Lorem', 'Ipsum', 'Dolor', 'Sit', 'Amet']; - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('fromArray')->with([$lines], null, 'A1'); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $this->buildService()->writeRow($lines); } public function testShouldFreezePanes() { - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('freezePane')->with('A3'); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $this->buildService()->freezePanes(); } @@ -189,18 +193,18 @@ public function testShouldFreezePanesAtTheGivenLocation() { $location = 'X3'; - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('freezePane')->with($location); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $this->buildService()->freezePanes($location); } public function testShouldAddRowPageBreak() { - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('setBreak')->with('A3', 1); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); $service = $this->buildService(); $service->resetCurrentRow(5); @@ -211,19 +215,19 @@ public function testShouldSaveFileInExcel2007Format() { $filename = 'export.xlsx'; - $this->phpExcelHandleMock->expects($this->once())->method('setActiveSheetIndex')->with(0); - $writerMock = $this->getMockBuilder('\PHPExcel_Writer_IWriter')->disableOriginalConstructor()->getMock(); + $this->spreadsheet->expects($this->once())->method('setActiveSheetIndex')->with(0); + $writerMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Writer\IWriter')->disableOriginalConstructor()->getMock(); $writerMock->expects($this->once())->method('save')->with($filename); // Setup all the mocks - $pageSetupMock = $this->getMockBuilder('\PHPExcel_Worksheet_PageSetup')->disableOriginalConstructor()->getMock(); - $worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock(); + $pageSetupMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup')->disableOriginalConstructor()->getMock(); + $worksheetMock = $this->getMockBuilder('\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet')->disableOriginalConstructor()->getMock(); $worksheetMock->expects($this->once())->method('getPageSetup')->willReturn($pageSetupMock); - $this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); + $this->spreadsheet->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock); - $phpExcel = $this->getPHPExcelMock(); + $phpExcel = $this->getSpreadsheetFactoryMock(); $phpExcel->expects($this->once())->method('createWriter') - ->with($this->phpExcelHandleMock, 'Excel2007') + ->with($this->spreadsheet, 'Xlsx') ->willReturn($writerMock); $service = $this->buildService(['phpExcel' => $phpExcel]); @@ -232,11 +236,11 @@ public function testShouldSaveFileInExcel2007Format() $service->finalize(); } - private function getPHPExcelMock() + private function getSpreadsheetFactoryMock() { - $phpExcel = $this->getMockBuilder('Xola\ReportWriterBundle\PHPExcelFactory')->disableOriginalConstructor()->getMock(); - $phpExcel->expects($this->once())->method('createPHPExcelObject')->willReturn($this->phpExcelHandleMock); + $factory = $this->getMockBuilder(SpreadsheetFactory::class)->disableOriginalConstructor()->getMock(); + $factory->expects($this->any())->method('createPhpSpreadsheetObject')->willReturn($this->spreadsheet); - return $phpExcel; + return $factory; } } diff --git a/Tests/bootstrap.php b/test/bootstrap.php similarity index 100% rename from Tests/bootstrap.php rename to test/bootstrap.php diff --git a/phpunit.xml.dist b/test/phpunit.xml.dist similarity index 84% rename from phpunit.xml.dist rename to test/phpunit.xml.dist index 01d9a41..a33833e 100644 --- a/phpunit.xml.dist +++ b/test/phpunit.xml.dist @@ -8,11 +8,11 @@ processIsolation="false" stopOnFailure="false" syntaxCheck="false" - bootstrap="Tests/bootstrap.php"> + bootstrap="bootstrap.php"> - ./Tests + ./test From eab2639b02bafe5fc21f53d8d11546ee0346c403 Mon Sep 17 00:00:00 2001 From: Anush Ramani Date: Tue, 10 Jul 2018 16:37:25 +0530 Subject: [PATCH 2/4] XOL-4064 removed `target-dir` from composer --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index a4810a1..b81c348 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,6 @@ "Xola\\ReportWriterBundle\\": "src/" } }, - "target-dir": "Xola/ReportWriterBundle", "require-dev": { "phpunit/phpunit": "~4.4" } From 51c689ccb00c08a27dfff3dca1cc06fe03c3da4e Mon Sep 17 00:00:00 2001 From: Anush Ramani Date: Tue, 10 Jul 2018 16:59:48 +0530 Subject: [PATCH 3/4] XOL-4064 moved missed file into `src` and fixed unit test config --- XolaReportWriterBundle.php => src/XolaReportWriterBundle.php | 0 test/phpunit.xml.dist | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename XolaReportWriterBundle.php => src/XolaReportWriterBundle.php (100%) diff --git a/XolaReportWriterBundle.php b/src/XolaReportWriterBundle.php similarity index 100% rename from XolaReportWriterBundle.php rename to src/XolaReportWriterBundle.php diff --git a/test/phpunit.xml.dist b/test/phpunit.xml.dist index a33833e..1a83edb 100644 --- a/test/phpunit.xml.dist +++ b/test/phpunit.xml.dist @@ -11,8 +11,8 @@ bootstrap="bootstrap.php"> - - ./test + + . From 92ca84706d8823b85bd15914841f5100185a3f91 Mon Sep 17 00:00:00 2001 From: Anush Ramani Date: Tue, 10 Jul 2018 17:05:52 +0530 Subject: [PATCH 4/4] XOL-4064 using approx autosize to work around true type font dependency --- src/Service/ExcelWriter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/ExcelWriter.php b/src/Service/ExcelWriter.php index f549833..de8ee3f 100644 --- a/src/Service/ExcelWriter.php +++ b/src/Service/ExcelWriter.php @@ -21,7 +21,7 @@ public function __construct(LoggerInterface $logger, SpreadsheetFactory $phpExce parent::__construct($logger); $this->phpexcel = $phpExcel; $this->spreadsheet = $phpExcel->createPhpSpreadsheetObject(); - Font::setAutoSizeMethod(Font::AUTOSIZE_METHOD_EXACT); + Font::setAutoSizeMethod(Font::AUTOSIZE_METHOD_APPROX); } /**