Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to test generated php files?

Tags:

php

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.

like image 665
Iter Ator Avatar asked Aug 02 '19 09:08

Iter Ator


People also ask

Are PHP files public?

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.

How to test a PHP script?

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.

How do I create a PHP test in Visual Studio?

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.

How can I test the upload of a file with PHPT?

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:

Can PhpStorm generate tests for multiple classes?

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.


1 Answers

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).

like image 146
Martin Zeitler Avatar answered Oct 23 '22 18:10

Martin Zeitler