Danh mục

Advanced PHP Programming- P5

Số trang: 50      Loại file: pdf      Dung lượng: 565.63 KB      Lượt xem: 13      Lượt tải: 0    
10.10.2023

Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu advanced php programming- p5, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Advanced PHP Programming- P5178 Chapter 6 Unit Testing “; $this->numSentences = 2; $this->numWords = 16; $this->numSyllables = 24; $this->object = new Text_Statistics($this->sample); } function _ _construct($name) { parent::_ _construct($name); } } Sure enough, the bug is there. Mr. matches as the end of a sentence.You can try to avoid this problem by removing the periods from common abbreviations.To do this, you need to add a list of common abbreviations and expansions that strip the abbreviations of their punctuation.You make this a static attribute of Text_Statistics and then sub- stitute on that list during analyze_line. Here’s the code for this: class Text_Statistics { // ... static $abbreviations = array(‘/Mr./’ =>’Mr’, ‘/Mrs./i’ =>’Mrs’, ‘/etc./i’ =>’etc’, ‘/Dr./i’ =>’Dr’, ); // ... protected function analyze_line($line) { // replace our known abbreviations $line = preg_replace(array_keys(self::$abbreviations), array_values(self::$abbreviations), $line); preg_match_all(“/(w[w’-]*)/”, $line, $words); foreach($words[1] as $word) { $word = strtolower($word); $w_obj = new Text_Word($word); $this->numSyllables += $w_obj->numSyllables(); $this->numWords++; if(!isset($this->_uniques[$word])) { $this->_uniques[$word] = 1; } else { $this->uniqWords++; } } preg_match_all(“/[.!?]/”, $line, $matches); $this->numSentences += count($matches[0]); } } Unit Testing in a Web Environment 179The sentence count is correct now, but now the syllable count is off. It seems that Mr.counts as only one syllable (because it has no vowels).To handle this, you can expand theabbreviation expansion list to not only eliminate punctuation but also to expand theabbreviations for the purposes of counting syllables. Here’s the code that does this:class Text_Statistics { // ... static $abbreviations = array(‘/Mr./’ =>’Mister’, ‘/Mrs./i’ =>’Misses’, //Phonetic ‘/etc./i’ =>’etcetera’, ‘/Dr./i’ =>’Doctor’, ); // ...}There are still many improvements you can make to the Text_Statistics routine.The $silentSyllable and $additionalSyllable arrays for tracking exceptionalcases are a good start, but there is still much work to do. Similarly, the abbreviations list ispretty limited at this point and could easily be expanded as well. Adding multilingualsupport by extending the classes is an option, as is expanding the statistics to includeother readability indexes (for example, the Gunning FOG index, the SMOG index, theFlesch-Kincaid grade estimation, the Powers-Sumner-Kearl formula, and the FORCASTFormula). All these changes are easy, and with the regression tests in place, it is easy toverify that modifications to any one of them does not affect current behavior.Unit Testing in a Web EnvironmentWhen I speak with developers about unit testing in PHP in the past, they often said“PHP is a Web-centric language, and it’s really hard to unit test Web pages.” This is notreally true, however. With just a reasonable separation of presentation logic from business logic, the vastmajority of application code can be unit tested and certified completely independentlyof the Web.The small portion of code that cannot be tested independently of the Webcan be validated through the curl extension. About curl curl is a client library that supports file transfer over an incredibly wide variety of Internet protocols (for example, FTP, HTTP, HTTPS, LDAP). The best part about curl is that it provides highly granular access to the requests and responses, making it easy to emulate a client browser. To enable curl, you must either con- figure PHP by using --with-curl if you are building it from source code, or you must ensure that your binary build has curl enabled.We will talk about user authentication in much greater depth in Chapter 13, “UserAuthentication and Session Security” but for now let’s evaluate a simple example.You180 Chapter 6 Unit Testing can write a simple inline authentication system that attempts to validate a user based on his or her user cookie. If the cookie is found, ...

Tài liệu được xem nhiều: