Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 19 | class AbstractEntryPointTest extends \PHPUnit_Framework_TestCase { |
||
| 20 | |||
| 21 | public static function setUpBeforeClass() |
||
| 22 | { |
||
| 23 | } |
||
| 24 | |||
| 25 | public static function tearDownAfterClass() |
||
| 26 | { |
||
| 27 | } |
||
| 28 | |||
| 29 | protected $url = 'http://localhost/rest/v10/'; |
||
| 30 | protected $options = array('foo'); |
||
| 31 | protected $data = array( |
||
| 32 | 'foo' => 'bar' |
||
| 33 | ); |
||
| 34 | |||
| 35 | public function setUp() |
||
| 36 | { |
||
| 37 | parent::setUp(); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function tearDown() |
||
| 41 | { |
||
| 42 | parent::tearDown(); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return EntryPointStub $Stub |
||
| 47 | * @covers ::__construct |
||
| 48 | * @group abstractEP |
||
| 49 | */ |
||
| 50 | public function testConstructor(){ |
||
| 51 | $Stub = new EntryPointStub($this->url); |
||
| 52 | $this->assertEquals('http://localhost/rest/v10/$test',$Stub->getUrl()); |
||
| 53 | $this->assertEquals(array(),$Stub->getOptions()); |
||
| 54 | $this->assertEmpty($Stub->getData()); |
||
| 55 | $this->assertEmpty($Stub->getRequest()); |
||
| 56 | $this->assertEmpty($Stub->getResponse()); |
||
| 57 | |||
| 58 | unset($Stub); |
||
| 59 | $Stub = new EntryPointStub($this->url,$this->options); |
||
| 60 | $this->assertEquals($this->url.'foo',$Stub->getUrl()); |
||
| 61 | $this->assertEquals($this->options,$Stub->getOptions()); |
||
| 62 | $this->assertEmpty($Stub->getData()); |
||
| 63 | $this->assertEmpty($Stub->getRequest()); |
||
| 64 | $this->assertEmpty($Stub->getResponse()); |
||
| 65 | |||
| 66 | return $Stub; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param EntryPointStub $Stub |
||
| 71 | * @return EntryPointStub $Stub |
||
| 72 | * @depends testConstructor |
||
| 73 | * @covers ::setOptions |
||
| 74 | * @covers ::getOptions |
||
| 75 | * @covers ::getUrl |
||
| 76 | * @covers ::configureUrl |
||
| 77 | * @group abstractEP |
||
| 78 | */ |
||
| 79 | public function testSetOptions($Stub){ |
||
| 80 | $Stub->setOptions(array()); |
||
| 81 | $this->assertEquals($this->url.'$test',$Stub->getUrl()); |
||
| 82 | $this->assertEquals(array(),$Stub->getOptions()); |
||
| 83 | $Stub->setOptions($this->options); |
||
| 84 | $this->assertEquals($this->url.'foo',$Stub->getUrl()); |
||
| 85 | $this->assertEquals($this->options,$Stub->getOptions()); |
||
| 86 | |||
| 87 | return $Stub; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param EntryPointStub $Stub |
||
| 92 | * @return EntryPointStub $Stub |
||
| 93 | * @depends testSetOptions |
||
| 94 | * @covers ::setData |
||
| 95 | * @covers ::getData |
||
| 96 | * @group abstractEP |
||
| 97 | */ |
||
| 98 | public function testSetData($Stub){ |
||
| 99 | $Stub->setData(array()); |
||
| 100 | $this->assertEquals(array(),$Stub->getData()); |
||
| 101 | $Stub->setData('string'); |
||
| 102 | $this->assertEquals('string',$Stub->getData()); |
||
| 103 | $class = new \stdClass(); |
||
| 104 | $class->foo = 'bar'; |
||
| 105 | $Stub->setData($class); |
||
| 106 | $this->assertEquals($class,$Stub->getData()); |
||
| 107 | unset($class); |
||
| 108 | $Stub->setData($this->data); |
||
| 109 | return $Stub; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param EntryPointStub $Stub |
||
| 114 | * @return EntryPointStub $Stub |
||
| 115 | * @depends testSetData |
||
| 116 | * @covers ::setRequest |
||
| 117 | * @covers ::getRequest |
||
| 118 | * @group abstractEP |
||
| 119 | */ |
||
| 120 | public function testSetRequest($Stub){ |
||
| 121 | $GET = new GET(); |
||
| 122 | $POST = new POST(); |
||
| 123 | $POSTFile = new POSTFile(); |
||
| 124 | $PUT = new PUT(); |
||
| 125 | $DELETE = new DELETE(); |
||
| 126 | $GETFile = new GETFile(); |
||
| 127 | $Stub->setRequest($POST); |
||
| 128 | $this->assertEquals($POST,$Stub->getRequest()); |
||
| 129 | unset($POST); |
||
| 130 | $Stub->setRequest($POSTFile); |
||
| 131 | $this->assertEquals($POSTFile,$Stub->getRequest()); |
||
| 132 | unset($POSTFile); |
||
| 133 | $Stub->setRequest($PUT); |
||
| 134 | $this->assertEquals($PUT,$Stub->getRequest()); |
||
| 135 | unset($PUT); |
||
| 136 | $Stub->setRequest($DELETE); |
||
| 137 | $this->assertEquals($DELETE,$Stub->getRequest()); |
||
| 138 | unset($DELETE); |
||
| 139 | $Stub->setRequest($GETFile); |
||
| 140 | $this->assertEquals($GETFile,$Stub->getRequest()); |
||
| 141 | unset($GETFile); |
||
| 142 | $Stub->setRequest($GET); |
||
| 143 | $this->assertEquals($GET,$Stub->getRequest()); |
||
| 144 | return $Stub; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param EntryPointStub $Stub |
||
| 149 | * @return EntryPointStub $Stub |
||
| 150 | * @depends testSetRequest |
||
| 151 | * @covers ::setAuth |
||
| 152 | * @covers ::authRequired |
||
| 153 | * @group abstractEP |
||
| 154 | */ |
||
| 155 | public function testSetAuth($Stub){ |
||
| 156 | $Stub->setAuth('1234'); |
||
| 157 | $this->assertEquals(true,$Stub->authRequired()); |
||
| 158 | return $Stub; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param EntryPointStub $Stub |
||
| 163 | * @return EntryPointStub $Stub |
||
| 164 | * @depends testSetAuth |
||
| 165 | * @covers ::execute |
||
| 166 | * @covers ::configureRequest |
||
| 167 | * @covers ::verifyUrl |
||
| 168 | * @covers ::verifyData |
||
| 169 | * @covers ::verifyRequiredData |
||
| 170 | * @covers ::verifyDataType |
||
| 171 | * @covers ::verifyOptions |
||
| 172 | * @covers ::configureData |
||
| 173 | * @covers ::configureDefaultData |
||
| 174 | * @covers ::configureUrl |
||
| 175 | * @covers ::configureAuth |
||
| 176 | * @group abstractEP |
||
| 177 | */ |
||
| 178 | public function testExecute($Stub){ |
||
| 179 | $Stub->execute(); |
||
| 180 | $this->assertEquals(array( |
||
| 181 | 'foo' => 'bar', |
||
| 182 | 'bar' => 'foo' |
||
| 183 | ),$Stub->getData()); |
||
| 184 | $this->assertEquals($this->url.'foo?foo=bar&bar=foo',$Stub->getRequest()->getUrl()); |
||
| 185 | $this->assertEquals(array('OAuth-Token: 1234'),$Stub->getRequest()->getHeaders()); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @covers ::verifyDataType |
||
| 190 | * @expectedException SugarAPI\SDK\Exception\EntryPoint\RequiredDataException |
||
| 191 | * @expectedExceptionMessageRegExp /Valid DataType is array/ |
||
| 192 | * @group abstractEP |
||
| 193 | */ |
||
| 194 | public function testInvalidDataType(){ |
||
| 195 | $Stub = new EntryPointStub($this->url); |
||
| 196 | $Stub->setOptions($this->options); |
||
| 197 | $Stub->setRequest(new POST()); |
||
| 198 | $class = new \stdClass(); |
||
| 199 | $class->foo = 'bar'; |
||
| 200 | $Stub->setData($class); |
||
| 201 | $Stub->execute(); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @covers ::verifyRequiredData |
||
| 206 | * @expectedException SugarAPI\SDK\Exception\EntryPoint\RequiredDataException |
||
| 207 | * @expectedExceptionMessageRegExp /Missing data for/ |
||
| 208 | * @group abstractEP |
||
| 209 | */ |
||
| 210 | public function testInvalidData(){ |
||
| 211 | $Stub = new EntryPointStub($this->url); |
||
| 212 | $Stub->setOptions($this->options); |
||
| 213 | $Stub->setRequest(new POST()); |
||
| 214 | $Stub->setData(array()); |
||
| 215 | $Stub->execute(); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @covers ::execute |
||
| 220 | * @expectedException SugarAPI\SDK\Exception\EntryPoint\InvalidRequestException |
||
| 221 | * @expectedExceptionMessageRegExp /Request property not configured/ |
||
| 222 | * @group abstractEP |
||
| 223 | */ |
||
| 224 | public function testInvalidRequest(){ |
||
| 225 | $Stub = new EntryPointStub($this->url); |
||
| 226 | $Stub->setOptions($this->options); |
||
| 227 | $Stub->setData($this->data); |
||
| 228 | $Stub->execute(); |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | } |
||
| 234 |