PHP Database Connection Test

How to test a PHP script

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP runs on all major operating systems, from Unix variants including Linux, FreeBSD, Ubuntu, Debian, and Solaris to Windows and Mac OS X. It can be used with all leading web servers, including Apache, Nginx, OpenBSD servers to name a few; even cloud environments like Azure and Amazon are on the rise.

Below are some of the ways in which a PHP script can be tested.

Testing Simple PHP Script

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. You may have a different DocumentRoot directory depending on which webserver you are using and the configuration done for it.

3. Change the permissions to 755 (Linux only):

4. Call the file from a browser:

http://Fully-Qualified-Hostname:PORT#/phpinfo.php

Testing a PHP Script that uses Database Connections

1. Create a file with the following contents. Give the file a name such as phpdbchk.php:

     $query = 'SELECT SYSDATE FROM DUAL'; $stmt = ociparse($conn, $query); ociexecute($stmt, OCI_DEFAULT); print 'Checking for the Date and Database Connectivity
'; $success = 0; while (ocifetch($stmt)) < print "Date: " . ociresult($stmt, "SYSDATE") . "
\n"; $success = 1; > if ($success) < print 'Success.

'; > else < print 'Failed to retrieve the date.

\n'; > OCILogoff($conn); print 'PHP Configuration
'; print '======================

'; phpinfo(); ?>

2. Set ORACLE_HOME and TNS_ADMIN to the proper values.

3. Copy the file to the DocumentRoot directory.

4. Modify the variables $username, $password, $database_hostname, $database_port, $database_sid and $database_srvc as necessary for the test system

5. Change the permissions to 755 (Linux only):

6. Call the file from a browser:

http://Fully-Qualified-Hostname:PORT#/phpdbchk.php

The following error occurs if ORACLE_HOME\network\admin\tnsnames.ora is not set up correctly or missing. If it is missing, the one from the database can be copied over and used as is.

Warning: ocilogon(): _oci_open_server: ORA-12560: TNS:protocol adapter error in [oracle_home]\apache\apache\htdocs\phpdbchk.php on line 25 ORA-12560: TNS:protocol adapter error

Running PHP Script to another directory outside of htdocs

For example, if you want to place php scripts to $ORACLE_HOME/Apache/Apache/phpsrc and run them from there via browser e.g http:FQHN:[port]/php/info.php, then do the following:

1. make directory $ORACLE_HOME/Apache/Apache/phpsrc

2. Copy info.php script to $ORACLE_HOME/Apache/Apache/phpsrc

3. Edit httpd.conf and add this line:

Alias /php/ $ORACLE_HOME/Apache/Apache/phpsrc

4. Restart http server and now it should work:

Note: The php script info.php was used as an example, you can use any name you choose for your php scripts

Источник

Test if php is working code example

The implementation detail is that the subject under test (SUT) will obtain the info-data on first call (lazy loading) and store it internally for future reference. For a comparison of the last two see as well PHP ternary operator vs null coalescing operator Solution 2: The idea of unit testing is that you have a known state before the test and an expected state after the test.

How to Unit Test an if Statement in PHP

The given answer is correct in it’s introduction, I’d like to add another aspect, especially after reading your comment beneath it with the question about mocking.

You’re asking about testing an if statement inside a public method.

It is important for that case that the if statement is actually an internal detail of the public method getInfo() , the unit test should not really care about that implementation detail. The implementation detail is that the subject under test (SUT) will obtain the info-data on first call (lazy loading) and store it internally for future reference.

This means that the public interface you have is a wrapper around some implementation detail (how that object aquires the data is that objects buisiness, but none of the business of your test).

Now — as you write — for a unit test you’d like to test for all paths in that method. This requires to call the method twice. In the first call you get data back and in the second call that data must be the same. So technically all you need to do is to call that method twice to test the internals.

$foo = new Foo(); $expected = $foo->getInfo(); $actual = $foo->getInfo(); $this->assertSame($expected, $actual); 

However to test the unit, you actually only need to call the method once to know that it works as you can expect that the private property won’t change as it is private (properly encapsulated) and you actually don’t want to care if you use that object.

So actually one could argue that you don’t want to test the if clause as that is an implementation detail.

Note: When you would measure the code coverage you would see all lines covered with the single call already (albeit not all paths have been covered by the single call). This just shows that 100% coverage must not mean you covered all paths in isolation.

It is my personal understanding that for a unit-test it should be enough to call the method once in a test and assert that it won’t return NULL.

$foo = new Foo(); $this->assertNotNull($foo->getInfo()); 

You could make this more strict by asserting more exactly of what you expect that method to return (e.g. some concrete type not just non-NULL), but for the unit-test this should already be enough.

The key point is here, that you first-of-all test the public interface of the SUT. You only need to test for details if there is a regression with a specific implementation. That belongs into another test so that you can remove it after a year, b/c the test was only necessary to fix a regression and after some more time should not be necessary any longer at all .

And better then: For that Facade in use, you probably want to test if the Facade is working, that is calling that global function and check for the expected outcome. This is in another test for that Facade, not for the object collaborating with it.

Otherwise you can mock things to death just for the sake of it, but it’s better to keep things simple in testing as it is often the case in programming. Otherwise there is always tendency to over-engineer things, which can cause harm in testing. You would turn public interface ad absurdum , but instead you want to rely on them. To have trust in relying on them, therefore is that test.

As an additional note, consider that your tests test for the things even if the implementation details of a method change. This is probably a good description, you want to change things over time and the existing tests should report to you if the changes didn’t change any of the (specified by the unit-tests) intention.

And another side-note: As you’re having the initialize-property-if-null-on-get, you can also inline this with PHP:

public function getInfo() < return $this->Info ?: $this->Info = InfoFacade::getInfoByToken($this->getToken()); > 
public function getInfo() < return $this->Info ?? $this->Info = InfoFacade::getInfoByToken($this->getToken()); > 

See the = , ternary (first example) or null-coalesce operator (second example) (please refere to the PHP manual for all these). For a comparison of the last two see as well

The idea of unit testing is that you have a known state before the test and an expected state after the test. Here, you’re expecting the state to only be changed the first time getInfo is called.

To run this test, you’ll start with a state where Info is null . Then you can call the method and check if the returned info is what you expected to be returned by the facade. Then change what’s in the facade and call getInfo again. What’s returned should be the same as from the first call, not whatever you changed the facade to.

Testing — Help to test if a proxy works using php, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Help to test if a proxy works using php

you can use fsockopen for this, where you can specify a timeout.

A simple proxy list checker. You can check a list ip:port if that port is opened on that IP.

'; // Show the proxy fclose($con); // Close the socket handle > > fclose($fisier); // Close the file ?> 

you may also want to use set_time_limit so that you can run your script for longer.

Code taken from: http://www.php.net/manual/en/function.fsockopen.php#95605

You can use this function

 function check($proxy=null) < $proxy= explode(':', $proxy); $host = $proxy[0]; $port = $proxy[1]; $waitTimeoutInSeconds = 10; $bool=false; if($fp = @fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds))< $bool=true; >fclose($fp); return $bool; > 

Check if a process is running using PHP in Linux, I am using kannel to send SMS via PHP. I want to know how can I check if a particular process is running. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Bad code can give evil minded people access to the whole system. – Mattis. Jun 19, …

How do you test php code on my local computer (Ubuntu)?

Install a LAMP stack. Since you already have Ubuntu, an AMP stack.
sudo apt-get install apache2 php5 php5-mysql from the command line will set you up a web server with php. Then, you can put your scripts in /var/www/

How do you test php code on my local computer, Install a LAMP stack. Since you already have Ubuntu, an AMP stack. sudo apt-get install apache2 php5 php5-mysql from the command line will set you up a web server with php. Then, you can put your scripts in /var/www/. Share. Improve this answer. answered Jul 3, 2012 at 1:15. Lusitanian.

Источник

How to Test PHP & Apache Working or Not

test apache and php

In our previous post, we shared how to install PHP on windows. If you don’t know how to install PHP then you can check our previous post. PHP is used to make websites dynamic.

PHP is a server-side scripting language. It has lots of frameworks to do programming. To use PHP you need to install PHP first. Then after you have to install an Apache server that will help you to run your PHP script through a web browser using localhost.

Apache’s main job is to listen to any incoming request from a browser and return an appropriate response. Apache is quite powerful and can accomplish virtually any task that you as a webmaster require.

According to the website (www.netcraft.com), Apache is running over 83.5 million internet servers, more than Microsoft, Sun ONE, and Zeus. To make sure that both PHP and Apache have been configured to work together let’s write a short test program.

To write program code you can choose any of your favorite Text-Editor and save that file to an appropriate location to execute, otherwise, you can not run your PHP script. If you are using Xampp,

If you are using Apache official,

C:Program FilesApache Software FoundationApache2.2

Before that, you have to start Apache server. If you are using Xampp then go to xampp control panel and start Apache server. If you are using Wamp server then put it online. If any other than run Apache server online to execute .php extension files. PHP CODE

    Testing Apache and PHP

"; ?>
Browser Output: localhost/tphp.php - localhost/filename.php - PHP is the extension

Now you get output on your browser then PHP and Apache both are working fine. If you are not getting output then your Apache and PHP configuration is wrong.

Источник

Читайте также:  Изображение
Оцените статью