PHP - With Unit Testing, How Extensive Should Mocking Be?
I’ve got a colleague who was brought in as a testing specialist to help improve code quality and reduce bugs. I’ve done a little testing before, but am no means an expert. When they joined I was really excited to learn from them as testing is definitely an area I lack experience. He’s got good coverage and he writes tests for literally everything he does, but it’s something like 95% mocks as opposed to what I would call “black box” testing; testing the input and output. Does that sound reasonable? As I say, I’m not an expert, but his testing strategy doesn’t seem particularly effective and it puts constraints over exactly how code is implemented rather than testing that things behave correctly. Similar TutorialsHi, I have a class XMLtool where one of the functions is to output a parameter-passed dom document and then output to the browser screen, so basically: function outputXML($domDocument) { if has attributes print cur nodes attributes for each child, if child is element node outputXML(cur node) else if child is text print text end for each } Please I am not sure what type of unit tests I can perform. I'd appreciate all the help! Just wanted some general advice/guidance, I know there are plenty of articles and tutorials online about unit testing, but I just wanted to get a first hand perspective off of some of you guys... What are some real world examples of actual PHP Unit Testing? Also any unit testing frameworks you can recommend? Apologies if this has been covered before. I must confess, I've not really done much unit testing at all in the last 3 years. Can anyone give me an idea of what they would test and why that would be faster or more efficient than running the code as a user? One area I can see it having huge benefits is when I'm testing methods which interact with a database, like user registration, which I would probably test by registering several times - fixing any bugs, making amendments and repeating. Ok, so I kinda sold the idea to myself there But, what about for small applications? I've seen people testing whether a variable is countable...why would I not just check the output immediately? Appreciate any advice. pls help me.. i am new to PHP unit testing concept. i just wrote a PHP unit test case for a login module in ZEND frame work. for that we have to see the result via command promt. but in some places of zend frame work we are using '$_SERVER['HTTP_HOST']' to get server host name but it can get only by hitting via browser so while am hitting via command promt it will give error message lik this(given below) Generating code coverage report, this may take a moment.PHP Notice: Undefined index: HTTP_HOST in /opt/coolstack/apache2/htdocs/LPS/application/bootstrap/Bootstrap.php on line 147 Notice: Undefined index: HTTP_HOST in /opt/coolstack/apache2/htdocs/LPS/application/bootstrap/Bootstrap.php on line 147 PHP Notice: Undefined index: HTTP_HOST in /opt/coolstack/apache2/htdocs/LPS/application/bootstrap/Bootstrap.php on line 197 Notice: Undefined index: HTTP_HOST in /opt/coolstack/apache2/htdocs/LPS/application/bootstrap/Bootstrap.php on line 197 PHP Notice: Undefined index: HTTP_HOST in /opt/coolstack/apache2/htdocs/LPS/library/Zaah/Controller/Plugin/Ssl.php on line 79 Notice: Undefined index: HTTP_HOST in /opt/coolstack/apache2/htdocs/LPS/library/Zaah/Controller/Plugin/Ssl.php on line 79 i cant give server host name at all the places. so please help me how to solve this problem.... thanks Yogesh
I need to create the unique SKU (Stock Keeping Unit) name for each products mixed with i.e.: SKU for a product with
id=13, becomes : BI-IKHA0013 This is how I tried: $inm = "Bio Clean green500ml"; $cnm = "INSECTS KILLERS"; $bnm = "HARPIC "; echo SKU_gen($inm, $cnm,$bnm,20).'<br>'; function SKU_gen($pname, $cat=null, $brand=null, $id = null, $l = 2){ $results = ''; // empty string $str1 = array_shift(explode(' ',$pname)); $str1 = strtoupper(substr($str1, 0, $l)); $str2 = array_shift(explode(' ',$cat)); $str2 = strtoupper(substr($str2, 0, $l)); $str3 = array_shift(explode(' ',$brand)); $str3 = strtoupper(substr($str3, 0, $l)); $id = str_pad($id , 4, 0, STR_PAD_LEFT); $results .= "{$str1}-{$str2}{$str3}{$id}"; return $results; } But this function is not working as expected when category of brand values become NULL. Let's say there is an application where a user would like to use English and Metric systems to enter values. For example, to handle customer orders where customers give data in this or that system, (but not both).
From the user-view, all the user cares about is if they can select English or Metric system, enter their values, click submit, and have their data added to the page in their choice of system, and have their values (and choice of system) stay the same.
The back end however is set up to do "compute" operations with English system in mind only. So inside back end, before the compute module, all the values should be converted to English. Some things like precomputed constants, that exist in computations are in English unit of measurements.
So I have attached an image of what I am thinking ..
* first (top) way is one where the front-end takes care of conversions. Back-end is not aware of another system (which makes it difficult sometimes, when trying to display a page back to the user saying "We know you chose system X"), and being aware of chosen system is something that I think is beneficial.
* the second (bottom) way is where front end is merely a passing device to back end, and bac-end takes care of conversions. Back-end is also aware of the two systems
My question is -- is there a preferred way to design an application when it comes to considerations above? Have a system-aware back-end, or try to localize system to front end only?
Attached Files
ways of encoding unit system.png 207.92KB
0 downloads Hello all, I have a script uploading files on the server. I would like to send a confirmation to user if the file had been uploaded well. Code: [Select] $targetFile = str_replace('//','/',$targetPath) . $filename; move_uploaded_file($tempFile,$targetFile); if (str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile)) { mail($to, 'upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded"); }else { mail($to, ' upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT uploaded"); } I presume I do something wrong because it doesn't work. The successfull email is sent each time. Thank you very much for helping, Eric This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=352939.0 I have a part of my code that requires php 5.3+ how can I test to see if the user has 5.3 or greater? At first I thought this would be a good idea, but then I realized that it isn't such as a version: 5.2.10 (not sure if that is an actual version or not). if((int)preg_replace("/\./", "", phpversion()) >= 530){ // Allow functionality } using my code, using 5.2.10 it would allow it, but it shouldn't because it isn't larger than 5.3. what can I do? Hi All,
I have developed a project in PHP, it is a web application.
Now i want to do testing on it,can any one suggest me a free tool by which i can test the php website.
Thanks in advance.
Hi. I am trying to test to see if a particular record id (int) is included in array. This is the code I am using $sql2 = "SELECT `id` FROM `orderitems` WHERE `orderid` = '" . $orderid . "'"; $orderlist = mysql_query($sql2) or trigger_error(mysql_error() . '<br />Query was:' . $sql2); $orderarray = array(); while ($orderitem = mysql_fetch_array($orderlist)); ( $orderarray[] = $orderitem['id']; ) $prodid=$row['prodid']; if (in_array($prodid, $orderarray)) { echo "<td> In.</td>"; } else { echo "<td> Out.</td>"; } Every case is outputting "Out" even though some should pass. I have verified that the variables are outputting the correct thing, but am not sure about the array. I am just a beginner, so i am hoping there is something really obvious I am doing wrong. Thanks for reading this. Can someone give me a simple socket script that I can copy and paste. It can return anything you like. I just want to make sure I have a working socket connection, and if not then you can maybe help me fix it. So I built a tool for my cms a while back and have decided to share it. The only 2 things that I know of that are similar in nature is phpmyadmin or sql buddy. Now my tool is not nearly as in depth as either of those but what it lets you do is run any query string right from the webpage. But the cool thing that it has over phpmyadmin and sql buddy is that it has buttons for many of the common commands and a ajax select to get the available column names for the selected table. So now you don't have to try and remember what table had what column and "how did I spell that column again".
I use this to test sql strings for syntax and to quickly see if it returns the results I expect without having to edit the code and then save, upload or hit refresh or whatever. Since all the the stuff is done via ajax, it's all quickly displayed and you can change the query immediately and hit the Run Query button again.
I just thought that this could be helpful and useful for others. You can download it here http://amecms.com/ar...ckly-and-Easily
I am creating a quiz, with Multiple choice, true and false, and fill in the blanks questions.
The first 2 types are working fine, and I am working on tesing the fill the blank questions...
See code:
//QUESTION 9 if (empty($_POST["ix"])) { $ixErr = "Please select an answer."; } else { $ix = test_input($_POST["ix"]); // check if question only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$ix)) { $ixErr = "Only letters and white space allowed."; else ($ix == "charming") { $score = $score+10; echo "<p>9.) " . $ix . " is correct.</p>\n"; } else { echo "<p>9.) " . $ix . " is incorrect.</p>\n"; } } }First, I am testing to see if it is empty, if yes, display error, otherwise use the test_input function on the entered information. Then I want to test and verify that the entered data only contains characters and/or blank spaces, if that returns yes, then I want to test the content for the correct answer. But for some reason the code gets stuck upon testing for the correct answer, please note: I did try elseif, but same issue, please help, thank you. HI - I'm trying to write an expression which tests the existence of a value within a POST variable. I have tried every which way ie isset, empty, ==0.00, NULL and yet I'm doing something wrong as nothing works. I hope you can help this student of PHP ! The expression is testing whether a price per lb exists ie the price of chicken per pound. But some products are not priced this way eg a jar of jam has no $pricelb[$i]. Here's some detail. The expression I am testing is: Code: [Select] if (!isset($pricelb[$i])){ // FIRST HALF FAILS $ordervalue = $price[$i]; $weight = '0.00';} else // SECOND HALF WORKS $pricelb = $pricelb[$i];{ $weight = $weight[$i]; $ordervalue = $pricelb * $weight; The $pricelb[$i] refers to an array of product weights coming off of the array $pricelb = $_POST['pricelb'] which I then cycle through using a for Loop, hence $pricelb[$i]. Bottom Line: if there is no pricelb then the then the ordervalue for the purchase is the same as the starting unit price. Suppose I have 2 products on the order. One with a pricelb of 5 and one with none. Then I do print_r on $_POST['pricelb'] I get: Code: [Select] Array ( [0] => 5 ) I.E. the second value is absent. YET, if I look into the DB table I see pricelb 0.00 for the second value - very confusing ! MANY MANY thanks ! for all your help ! I'm pulling my hair out trying to figure out a simple problem. I've got the following PHP code in an HTML page: $shutoff_date = strtotime($wkend_start_date) - 259200; if ($shutoff_date > $todays_date) { $current_month = date("m"); $current_year = date("y"); $freshman_eligible = 0; if ($current_month >= 2 && $current_month <=7) { $freshman_eligible = 1; } if ($current_month <= 7) { $seniors = $current_year ; } else {$seniors = $current_year + 1; } $juniors = $seniors + 1; $sophomores = $seniors + 2; $freshman = $seniors + 3; echo "HTML Stuff blah, blah, blah"; if ($freshman_eligible = 1) { echo "<option value=\"$freshman\">$freshman</option>";} } The last test to see if $freshman_eligible is equal to 1 is always testing true, and it's actually resetting $freshman_eligible to whatever I put inside the IF statement. No matter what the variable is set to previously, it will reset to whatever I test for in the last section, and hence always tests true. I thought maybe it was an issue with scope, but I'm not sure what the issue would be or how to resolve it. What am I missing??? I want to give the visitors to my site the option of setting certain preferences and remembering what was chosen from one visit to the next. They might have the choice of setting the backgrond colour of the web page or the font or whatever.
I've already written and tested code to write standard cookies that preserve their choices. However, I'm wondering if that's the best way or if there is a better approach. I don't want visitors to have to re-choose their preferences every time they arrive at the site, which would appear to be the result of using a session cookie stored on the server.
If standard cookies ARE the best solution for my situation, how can I test if the visitor has cookies enabled on their browser? I know that some people definitely don't enable cookies and I'm quite prepared to advise them that they won't be able to save preferences until they enable cookies. However, I first need to know how I can tell definitively if cookies are enabled.
Most of the simple code examples I've found by googling simply try to write a cookie and then try to read it; if both succeed, they think the they've established whether cookies are enabled. The more sophisticated examples, particularly those at stackoverflow, come closer but all of them seem to be critiqued by someone to the effect that they won't work in this or that particular situation.
Given how long cookies and PHP have been in existence, I have to believe that SOMEONE has figured out a definitive way of testing whether cookies are enabled that works in all reasonable circumstances. Can anyone share that technique with me?
these are sections of my profile, just plain right now so easier for u to read: Code: [Select] $notes = 'notes'; $signature = 'signatures'; $friends = 'friends'; $middle=array ( "0" => array ( "section" => $notes, "hide" => "0", "c" => "apple" ), "1" => array ("section" => $signature, "hide" => "0", "c" => "apple" ), "2" => array ("section" => $friends, "hide" => "0", "c" => "apple" ), ); $order = array(2,1,0); foreach ($order as $index) { echo '<br>'.$middle[$index]['section']; } If the hide = 1 how do i hide a array in my foreach? hi, Hi have a web application www.prakhargoel.com/projects/lms/ that is used by students for logging and requesting a computer system in the internet lab. There is a corresponding labadmin at www.prakhargoel.com/projects/lms/labadmin. Though I tried to make this web app as secure as possible, there might be some shortcomings in the security which could be used by hackers. Please analyse the vulnerabilities and tell where I need to improve. If anyone needs, I can provide the php code for further testing. I have: 1) applied sha1 encryption to all passwords. 2) timeout sessions 3) sql injection filters, etc This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=326737.0 |