I am working on a refactoring tool, which produces php files. Those generated files can contain variables, functions and classes too.
I would like to write unit tests, to test if those files are working as expected, but I have no idea how should I do it.
If I require an incorrect file with if (! @require('my_new_file.php') ) {
, I still get a parse error, which I am not able to catch.
I call require
inside a function, but the definitions in the required files are still available outside of my function. How could I avoid that? Is it possible to require a file in a scope, so it will not pollute the global namespace?
Even if I call eval(file_get_contents('my_new_file.php'))
inside a function, the functions defined in my_new_file.php
are available globally.
Your web server is responsible for all traffic coming in over HTTP, so assuming it is correctly configured to hand all requests to a PHP file over to the PHP interpreter, there is no way anyone can view the raw contents of a PHP file.
Below are some of the ways in which a PHP script can be tested. 1. Create a file with the following contents. Give the file a name such as myphpInfo.php: 2. Copy the file to the your webservers DocumentRoot directory, for example – /var/www/html.
In the Project tool window, press Alt+Insert or right-click the PHP class to be tested and choose New | PHP Test | <Test framework> Test from the selection context menu. In the Create New PHP Test dialog, specify the following: The test file template based on which the test class will be generated.
The easiest way to get appropriate test data is probably to record a real file-upload with a tool like Wireshark and copy the relevant data into the PHPT test. The following listing shows such a recording: Now you have all information together to test the upload of a file with PHPT. So here is the actual test's description:
PhpStorm can generate tests for the classes that are defined in separate files as well as for the classes that are defined within a single PHP file. In the latter case, for each generated test class PhpStorm will create a separate file. After creating the test, you can quickly navigate between the test and its subject.
Generate PHPunit tests for the generated files; eg. with PhpUnitGen:
composer install paulthebaud/phpunit-generator
When these are CLI scripts, always use exit(0)
and exit(1)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With