{"id":1419,"date":"2018-01-19T19:22:49","date_gmt":"2018-01-19T19:22:49","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/testing-forms-in-php-with-simpletest-page-2\/"},"modified":"2018-01-19T19:24:46","modified_gmt":"2018-01-19T19:24:46","slug":"testing-forms-in-php-with-simpletest-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/testing-forms-in-php-with-simpletest-page-2\/","title":{"rendered":"Testing Forms in PHP with SimpleTest Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By W. Jason Gilmore<\/div>\n<div class=\"\">on March 2, 2011<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>Testing Invalid Form Submissions<\/h2>\n<div class=\"articlePara\">Web development would be pretty easy if we were concerned only with testing valid form data. Unfortunately, users can be expected to submit all manner of improper, invalid, and even malicious data and so you&#8217;ll need to write numerous tests that ensure your PHP code is properly detecting and reporting errant form input such as an invalid email address or missing name. Again when using the Web testing framework you&#8217;ll be concerned with ensuring that the appropriate error messages are displayed. Once the form validation code has been added to the page you can use the following tests to ensure valid email addresses and names consisting of at least one character are included with the form submission:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>function testInvalidEmailAddress() {\n  $this-&gt;get('http:\/\/www.example.com\/contact.php');\n  $this-&gt;assertResponse(200);\n\n  $this-&gt;setField(\"name\", \"Jason\");\n  $this-&gt;setField(\"email\", \"wjexample.com\");\n  $this-&gt;setField(\"message\", \"I look forward to hearing from you!\");\n\n  $this-&gt;clickSubmit(\"Contact us!\");\n\n  $this-&gt;assertResponse(200);\n  $this-&gt;assertText(\"Please provide a valid email address.\");\n}\n\nfunction testInvalidName() {\n  $this-&gt;get('http:\/\/www.example.com\/contact.php');\n  $this-&gt;assertResponse(200);\n\n  $this-&gt;setField(\"name\", \"\");\n  $this-&gt;setField(\"email\", \"wj@example.com\");\n  $this-&gt;setField(\"message\", \"I look forward to hearing from you!\");\n\n  $this-&gt;clickSubmit(\"Contact us!\");\n\n  $this-&gt;assertResponse(200);\n  $this-&gt;assertText(\"Please provide your name.\");\n}<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">Run all of the tests and you should see output similar to that found in Figure 3.<\/div>\n<div class=\"articlePara\"><img decoding=\"async\" alt=\"The SimpleTest Framework\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/figure3-2.png\"\/><br \/><strong>Figure 3. Comprehensive Form Testing Is Easy with SimpleTest<\/strong><\/div>\n<h2>Conclusion<\/h2>\n<div class=\"articlePara\">SimpleTest&#8217;s native Web testing framework makes it ridiculously easy to automate the testing of your PHP logic from the end user&#8217;s perspective. This functionality can greatly reduce the time you&#8217;ll otherwise need to spend performing tedious tasks such as completing and submitting Web forms. Be sure to check out the following links for more information about Web testing!<\/div>\n<ul>\n<li><a href=\"http:\/\/simpletest.org\/\" target=\"newFrame\">SimpleTest<\/a>: The SimpleTest Homepage<\/li>\n<li><a href=\"http:\/\/seleniumhq.org\/\" target=\"newFrame\">Selenium<\/a>: Another great solution for automating Web testing<\/li>\n<li><a href=\"http:\/\/watir.com\/\" target=\"newFrame\">Watir<\/a>: This Web application testing in Ruby framework is another great tool capable of scripting automated interaction tasks with all major browsers.<\/li>\n<\/ul>\n<div class=\"articlePara\">What other Web testing tools do you recommend? Tell us about them in the comments!<\/div>\n<h2>About the Author<\/h2>\n<div class=\"articlePara\"><em><strong>Jason Gilmore<\/strong> is founder of the publishing, training, and consulting firm <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"newFrame\">WJGilmore.com<\/a>. He is the author of several popular books, including <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"newFrame\">&#8220;Easy PHP Websites with the Zend Framework&#8221;<\/a>, <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"newFrame\">&#8220;Easy PayPal with PHP&#8221;<\/a>, and <a href=\"http:\/\/www.amazon.com\/dp\/1430231149\/\" target=\"newFrame\">&#8220;Beginning PHP and MySQL, Fourth Edition&#8221;<\/a>. Follow him on Twitter at <a href=\"http:\/\/www.twitter.com\/wjgilmore\" target=\"newFrame\">@wjgilmore<\/a>.<\/em><\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"Jason_Gilmore03012011.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Jason_Gilmore03012011.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">2<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The SimpleTest native Web testing framework makes it easy to automate the testing of your PHP logic from the end user<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1419","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1419"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1419\/revisions"}],"predecessor-version":[{"id":2226,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1419\/revisions\/2226"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}