CodeIgniter Forums
phpUnit test working directory - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: phpUnit test working directory (/thread-80638.html)



phpUnit test working directory - paul - 11-25-2021

hello
i try to add phpUnit test to my codeigniter 4 application
i use composer

when the code run in phpUnit, I have an error on a file path that is relative
i thinks the working directory in phpUnit is not the same as when the code run from ./public


how can i change the working directory ?


Code:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/codeigniter4/framework/system/Test/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true" processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
<exclude>
<directory suffix=".php">./app/Views</directory>
<file>./app/Config/Routes.php</file>
</exclude>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/logs/html"/>
<php outputFile="build/logs/coverage.serialized"/>
<text outputFile="php://stdout" showUncoveredFiles="false"/>
</report>
</coverage>
<testsuites>
<testsuite name="app">
<directory>./tests</directory>
</testsuite>
</testsuites>
<logging>
<testdoxHtml outputFile="build/logs/testdox.html"/>
<testdoxText outputFile="build/logs/testdox.txt"/>
<junit outputFile="build/logs/logfile.xml"/>
</logging>
<php>
<server name="app.baseURL" value="http://localhost.com/"/>
<!-- Directory containing phpunit.xml -->
<const name="HOMEPATH" value="./"/>
<!-- Directory containing the Paths config file -->
<const name="CONFIGPATH" value="./app/Config/"/>
<!-- Directory containing the front controller (index.php) -->
<const name="PUBLICPATH" value="./public/"/>
<!-- Database configuration -->
<!-- Uncomment to provide your own database for testing
<env name="database.tests.hostname" value="localhost"/>
<env name="database.tests.database" value="tests"/>
<env name="database.tests.username" value="tests_user"/>
<env name="database.tests.password" value=""/>
<env name="database.tests.DBDriver" value="MySQLi"/>
<env name="database.tests.DBPrefix" value="tests_"/>
-->

</php>
</phpunit>




RE: phpUnit test working directory - Taras - 11-25-2021

Do you try to run tests from public? You should run them from your projects dir like "./vendor/bin/phpunit" and it should work without any tweaks.


RE: phpUnit test working directory - paul - 11-25-2021

(11-25-2021, 07:20 AM)Taras Wrote: Do you try to run tests from public? You should run them from your projects dir like "./vendor/bin/phpunit" and it should work without any tweaks.

I run tests with :
php 'c:/wamp64/codeigniter4/vendor/phpunit/phpunit/phpunit' --colors=always -c c:/wamp64/codeigniter4/phpunit.xml --testsuite 'app'


RE: phpUnit test working directory - Taras - 11-25-2021

(11-25-2021, 08:01 AM)paul Wrote: I run tests with :
php 'c:/wamp64/codeigniter4/vendor/phpunit/phpunit/phpunit' --colors=always -c c:/wamp64/codeigniter4/phpunit.xml --testsuite 'app'

I believe you have to run it from 'c:/wamp64/codeigniter4/'. If I run phpunit from outside of the project's dir I have errors too.

Or you may try to play with 'app/Config/Paths.php' (or better .env file) and use absolute paths there. And for test you may even need to adjust yout phpunit.xml and define constants there.