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 |
||
| 22 | class JidTest extends \PHPUnit_Framework_TestCase |
||
| 23 | { |
||
| 24 | public function validJidProvider() |
||
| 25 | { |
||
| 26 | return [ |
||
| 27 | 'domain-part only' => ['hostname.tld'], |
||
| 28 | 'bare jid' => ['hostname.tld', 'local-part'], |
||
| 29 | 'full jid' => ['hostname.tld', 'local-part', 'resource'], |
||
| 30 | 'domain-part with resource' => ['hostname.tld', null, 'resource'], |
||
| 31 | |||
| 32 | 'bare jid from string' => ['[email protected]'], |
||
| 33 | 'full jid from string' => ['[email protected]/resource'], |
||
| 34 | ]; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function invalidJidProvider() |
||
| 38 | { |
||
| 39 | return [ |
||
| 40 | [''], |
||
| 41 | ['local-part@'], |
||
| 42 | ['local-part@/resource'], |
||
| 43 | ['/resource'], |
||
| 44 | |||
| 45 | ['', 'local-part'], |
||
| 46 | ['', 'local-part', 'resource'], |
||
| 47 | ['', null, 'resource'], |
||
| 48 | |||
| 49 | ['<'], |
||
| 50 | ['hostname.tld', '<'], |
||
| 51 | ['hostname.tld', '<', '>'], |
||
| 52 | ['hostname.tld', null, '>'], |
||
| 53 | ]; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testDomainWithResource() |
||
| 57 | { |
||
| 58 | $address = new Jid('hostname.tld', null, 'resource'); |
||
| 59 | $this->assertEquals('hostname.tld', $address->domain); |
||
| 60 | $this->assertEquals('resource', $address->resource); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function testDomainWithResourceFromString() |
||
| 64 | { |
||
| 65 | $address = new Jid('hostname.tld/resource'); |
||
| 66 | $this->assertEquals('hostname.tld', $address->domain); |
||
| 67 | $this->assertEquals('resource', $address->resource); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testBareCreation() |
||
| 71 | { |
||
| 72 | $address = new Jid('hostname.tld', 'local-part'); |
||
| 73 | $this->assertEquals('hostname.tld', $address->domain); |
||
| 74 | $this->assertEquals('local-part', $address->local); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function testBareCreationFromString() |
||
| 78 | { |
||
| 79 | $address = new Jid('[email protected]'); |
||
| 80 | $this->assertEquals('hostname.tld', $address->domain); |
||
| 81 | $this->assertEquals('local-part', $address->local); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function testFull() |
||
| 85 | { |
||
| 86 | $address = new Jid('hostname.tld', 'local-part', 'resource'); |
||
| 87 | $this->assertEquals('hostname.tld', $address->domain); |
||
| 88 | $this->assertEquals('local-part', $address->local); |
||
| 89 | $this->assertEquals('resource', $address->resource); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testFullFromString() |
||
| 93 | { |
||
| 94 | $address = new Jid('[email protected]/resource'); |
||
| 95 | $this->assertEquals('hostname.tld', $address->domain); |
||
| 96 | $this->assertEquals('local-part', $address->local); |
||
| 97 | $this->assertEquals('resource', $address->resource); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testHostnameOnly() |
||
| 101 | { |
||
| 102 | $address = new Jid('hostname.tld'); |
||
| 103 | $this->assertEquals('hostname.tld', $address->domain); |
||
| 104 | $this->assertNull($address->resource); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @expectedException InvalidArgumentException |
||
| 109 | * @expectedExceptionMessageRegExp /Domain-part of JID is REQUIRED/i |
||
| 110 | */ |
||
| 111 | public function testEmptyHostname() |
||
| 112 | { |
||
| 113 | new Jid(''); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @expectedException InvalidArgumentException |
||
| 118 | * @expectedExceptionMessageRegExp /Domain-part of JID is REQUIRED/i |
||
| 119 | */ |
||
| 120 | public function testEmptyHostnameFromString() |
||
| 121 | { |
||
| 122 | new Jid('local-part@/resource'); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @expectedException InvalidArgumentException |
||
| 127 | * @expectedExceptionMessageRegExp /Domain-part of JID contains not allowed character '.'/i |
||
| 128 | */ |
||
| 129 | public function testInvalidHostname() |
||
| 130 | { |
||
| 131 | new Jid('<invalid>'); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @expectedException InvalidArgumentException |
||
| 136 | * @expectedExceptionMessageRegExp /Resource-part of JID contains not allowed character '.'/i |
||
| 137 | */ |
||
| 138 | public function testInvalidResource() |
||
| 139 | { |
||
| 140 | new Jid('hostname.tld', null, '<invalid>'); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @dataProvider validJidProvider |
||
| 145 | */ |
||
| 146 | public function testIsValid(...$arguments) |
||
| 147 | { |
||
| 148 | $this->assertTrue(Jid::isValid(...$arguments)); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @dataProvider invalidJidProvider |
||
| 153 | */ |
||
| 154 | public function testIsNotValid(...$arguments) |
||
| 155 | { |
||
| 156 | $this->assertFalse(Jid::isValid(...$arguments)); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @expectedException InvalidArgumentException |
||
| 161 | * @expectedExceptionMessageRegExp /Domain-part of JID contains not allowed character '.'/i |
||
| 162 | */ |
||
| 163 | public function testValidationInvalidHostname() |
||
| 164 | { |
||
| 165 | Jid::validate('<invalid>'); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @expectedException InvalidArgumentException |
||
| 170 | * @expectedExceptionMessageRegExp /Resource-part of JID contains not allowed character '.'/i |
||
| 171 | */ |
||
| 172 | public function testValidationInvalidResource() |
||
| 173 | { |
||
| 174 | Jid::validate('hostname.tld', null, '<invalid>'); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function testIsBare() |
||
| 178 | { |
||
| 179 | $this->assertTrue((new Jid('[email protected]'))->isBare()); |
||
| 180 | $this->assertTrue((new Jid('hostname.tld'))->isBare()); |
||
| 181 | |||
| 182 | $this->assertFalse((new Jid('[email protected]/resource'))->isBare()); |
||
| 183 | $this->assertFalse((new Jid('hostname.tld/resource'))->isBare()); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function testBare() |
||
| 187 | { |
||
| 188 | $bare = (new Jid('[email protected]/resource'))->bare(); |
||
| 189 | $this->assertEquals('hostname.tld', $bare->domain); |
||
| 190 | $this->assertEquals('local-part', $bare->local); |
||
| 191 | $this->assertNull($bare->resource); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function testIsFull() |
||
| 195 | { |
||
| 196 | $this->assertTrue((new Jid('[email protected]/resource'))->isFull()); |
||
| 197 | |||
| 198 | $this->assertFalse((new Jid('[email protected]'))->isFull()); |
||
| 199 | $this->assertFalse((new Jid('hostname.tld'))->isFull()); |
||
| 200 | $this->assertFalse((new Jid('hostname.tld/resource'))->isFull()); |
||
| 201 | } |
||
| 202 | |||
| 203 | public function testToString() |
||
| 204 | { |
||
| 205 | $this->assertEquals('hostname.tld', (string)(new Jid('hostname.tld'))); |
||
| 206 | $this->assertEquals('hostname.tld/resource', (string)(new Jid('hostname.tld/resource'))); |
||
| 207 | $this->assertEquals('[email protected]/resource', (string)(new Jid('[email protected]/resource'))); |
||
| 208 | $this->assertEquals('[email protected]', (string)(new Jid('[email protected]'))); |
||
| 209 | } |
||
| 210 | } |
||
| 211 |