Skip to content

Commit 3f91a7a

Browse files
committed
Add info about troubleshooting assets loading with Panther.
- Can happen with any requested uri that look like a non `.php` file - Case can happen with AssetMapper.
1 parent 6c48614 commit 3f91a7a

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

testing/end_to_end.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,106 @@ variable to ``false`` in your style file:
799799
800800
$enable-smooth-scroll: false;
801801
802+
Assets not loading (PHP built-in server only)
803+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
804+
805+
You may face cases where your assets are not loaded while running your tests.
806+
Because Panther use the `PHP built-in server`_ to serve your app, if your assets files
807+
(or any requested URI that not a ``.php`` file) does not exist in your public directory
808+
(e.g. rendered by your Symfony app), the built-in server will return a 404 not found.
809+
810+
This can happen when using :doc:`AssetMapper component </frontend/asset_mapper>`
811+
if you let your Symfony app handle your assets in dev environment.
812+
813+
To solve this, add a ``tests/router.php``::
814+
815+
// tests/router.php
816+
if (is_file($_SERVER['DOCUMENT_ROOT'].\DIRECTORY_SEPARATOR.$_SERVER['SCRIPT_NAME'])) {
817+
return false;
818+
}
819+
820+
$script = 'index.php';
821+
822+
$_SERVER = array_merge($_SERVER, $_ENV);
823+
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].\DIRECTORY_SEPARATOR.$script;
824+
825+
$_SERVER['SCRIPT_NAME'] = \DIRECTORY_SEPARATOR.$script;
826+
$_SERVER['PHP_SELF'] = \DIRECTORY_SEPARATOR.$script;
827+
828+
require $script;
829+
830+
Then declare it as a router for Panther server in ``phpunit.xml.dist`` using ``PANTHER_WEB_SERVER_ROUTER`` var:
831+
832+
.. code-block:: xml
833+
834+
<!-- phpunit.xml.dist -->
835+
<phpunit>
836+
<!-- ... -->
837+
<php>
838+
<!-- ... -->
839+
<server name="PANTHER_WEB_SERVER_ROUTER" value="../tests/router.php"/>
840+
</php>
841+
</phpunit>
842+
843+
Credit from `Testing Part 2 Functional Testing on Symfony cast`_ were you can see more about this case.
844+
845+
.. note::
846+
847+
When using :doc:`AssetMapper component </frontend/asset_mapper>`, you can also compile your assets before running
848+
your tests. It will make your tests faster because Symfony will not have to handle them, the built-in server
849+
will serve them directly.
850+
851+
852+
Assets not loading
853+
~~~~~~~~~~~~~~~~~~
854+
855+
You may face cases where your assets are not loaded while running your tests.
856+
Because Panther use the `PHP built-in server`_ to serve your app, if your assets files
857+
(or any requested URI that not a ``.php`` file) does not exist in your public directory
858+
(e.g. rendered by your Symfony app), the built-in server will return a 404 not found.
859+
860+
This can happen when using :doc:`AssetMapper component </frontend/asset_mapper>`
861+
if you let your Symfony app handle your assets in dev environment.
862+
863+
To solve this, add a ``tests/router.php``::
864+
865+
// tests/router.php
866+
if (is_file($_SERVER['DOCUMENT_ROOT'].\DIRECTORY_SEPARATOR.$_SERVER['SCRIPT_NAME'])) {
867+
return false;
868+
}
869+
870+
$script = 'index.php';
871+
872+
$_SERVER = array_merge($_SERVER, $_ENV);
873+
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].\DIRECTORY_SEPARATOR.$script;
874+
875+
$_SERVER['SCRIPT_NAME'] = \DIRECTORY_SEPARATOR.$script;
876+
$_SERVER['PHP_SELF'] = \DIRECTORY_SEPARATOR.$script;
877+
878+
require $script;
879+
880+
Then declare it as a router for Panther server in ``phpunit.xml.dist`` using ``PANTHER_WEB_SERVER_ROUTER`` var:
881+
882+
.. code-block:: xml
883+
884+
<!-- phpunit.xml.dist -->
885+
<phpunit>
886+
<!-- ... -->
887+
<php>
888+
<!-- ... -->
889+
<server name="PANTHER_WEB_SERVER_ROUTER" value="../tests/router.php"/>
890+
</php>
891+
</phpunit>
892+
893+
Credit from `Testing Part 2 Functional Testing on Symfony cast`_ were you can see more about this case.
894+
895+
.. note::
896+
897+
When using :doc:`AssetMapper component </frontend/asset_mapper>`, you can also compile your assets before running
898+
your tests. It will make your tests faster because Symfony will not have to handle them, the built-in server
899+
will serve them directly.
900+
901+
802902
Additional Documentation
803903
------------------------
804904

@@ -825,3 +925,7 @@ documentation:
825925
.. _`Gitlab CI`: https://docs.gitlab.com/ee/ci/
826926
.. _`AppVeyor`: https://www.appveyor.com/
827927
.. _`LiipFunctionalTestBundle`: https://github.com/liip/LiipFunctionalTestBundle
928+
.. _`PHP built-in server`: https://www.php.net/manual/en/features.commandline.webserver.php
929+
.. _`Testing Part 2 Functional Testing on Symfony cast`: https://symfonycasts.com/screencast/last-stack/testing
930+
.. _`PHP built in server`: https://www.php.net/manual/en/features.commandline.webserver.php
931+
.. _`Testing Part 2 Functional Testing on Symfony cast`: https://symfonycasts.com/screencast/last-stack/testing

0 commit comments

Comments
 (0)