3. Object Oriented Design
Total Page:16
File Type:pdf, Size:1020Kb
Copyright ⃝c 2016 Qafoo GmbH Published by Qafoo GmbH Version 1.1.1; Published December 15, 2016 https://qafoo:com/ | <contact@qafoo:com> Contents 1 Introduction 1.1 About This Book 9 1.2 About Qafoo 10 1.3 The Authors 11 1.3.1 Kore Nordmann . 11 1.3.2 Tobias Schlitt . 11 1.3.3 Bejamin Eberlei . 12 1.3.4 Manuel Pichler . 12 2 Clean Code 2.1 Developers Life is a Trade-Off 13 2.1.1 The NoSQL Dilemma . 13 2.1.2 Overengineered State Machines . 14 2.1.3 Hack Hack Hack . 15 2.1.4 Bottom Line . 16 2.2 Never Use null 17 2.2.1 What it is Used For . 17 2.2.2 null as Return Value . 19 ⃝c Qafoo GmbH 2010 - 2016 2.2.3 Summary . 20 2.3 Struct classes in PHP 21 2.3.1 Implementation. 22 2.3.2 Copy on write . 24 2.3.3 Summary . 25 3 Object Oriented Design 3.1 Learn OOD - to unlearn it again 27 3.1.1 Learning OOD the classical way . 27 3.1.2 OOD in fast pace and agile . 28 3.1.3 Refactoring is the key . 29 3.1.4 Learning OOD to unlearn it . 29 3.1.5 Conclusion / TL;DR. 31 3.2 Object lifecycle control 32 3.2.1 Why is this bad? . 32 3.2.2 How can I solve this? . 33 3.2.3 Conclusion . 34 3.3 Ducks Do Not Type 35 3.3.1 Duck Typing . 36 3.3.2 Prototyping. 36 3.3.3 Using Foreign Code . 37 3.3.4 Package Visibility . 38 3.3.5 Conclusion . 38 3.4 Abstract Classes vs. Interfaces 39 3.4.1 Definitions . 39 3.4.2 Classes are Types. 39 3.4.3 interface . 40 3.4.4 Telling Apart . 41 3.4.5 Examples & Hints . 42 3.4.6 tl;dr . 43 3.5 ContainerAware Considered Harmful 44 3.5.1 Background . 44 3.5.2 Issues . 44 3.5.3 Conclusion . 47 3.6 Code Reuse By Inheritance 48 3.6.1 Inheritance . 48 3.6.2 Active Record . 48 3.6.3 A Smaller Example . 49 3.6.4 The Helper Method . 51 3.6.5 Testing Private Methods . 51 3.6.6 Depth Of Inheritance Tree (DIT) . 52 3.6.7 Summary . 52 3.7 Utilize Dynamic Dispatch 53 3.8 When to Abstract? 58 3.8.1 Summary . 60 4 Testing 4.1 Finding the right Test-Mix 61 4.1.1 The Test-Mix Tradeoff . 63 4.1.2 Conclusion . 64 4.2 Practical PHPUnit: Testing XML generation 65 4.2.1 Test case basics . 68 4.2.2 The naive way . 69 4.2.3 The lazy way . 71 4.2.4 Selecting with CSS . 71 4.2.5 Tag match assertions . 75 4.2.6 Using XPath . 77 4.2.7 Conclusion . 81 4.3 Testing file uploads with PHP 82 4.4 Mocking with Phake 88 4.4.1 Test Doubles Explained . 88 4.4.2 Benefits of Test Doubles . 88 4.4.3 Introduction to Phake . 89 4.4.4 Conclusion . 91 4.5 Testing Effects of Commands With Phake::capture() 92 4.6 Using Mink in PHPUnit 94 4.7 Introduction To Page Objects 97 4.7.1 Groundwork . 97 4.7.2 A First Test . 98 4.7.3 Refactoring The Frontend . 100 4.7.4 Problems With Page Objects . 102 4.7.5 Conclusion . 102 4.8 Database Tests With PHPUnit 103 4.8.1 Removing Data versus Schema Reset . 103 4.8.2 Point of Data Reset . 104 4.8.3 Mocking the Database Away . 106 4.8.4 Conclusion . 106 4.9 Database Fixture Setup in PHPUnit 107 4.9.1 Dump & Insert Live Data . 107 4.9.2 Base Data . 108 4.9.3 Test Data . ..