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 |
||
| 5 | class ConnectFactoryTest extends \Tests\ShopwarePlugins\Connect\ConnectTestHelper |
||
| 6 | { |
||
| 7 | |||
| 8 | private $connectFactory; |
||
| 9 | private $configMock; |
||
| 10 | private $localProductQuery; |
||
| 11 | |||
| 12 | public function setUp() |
||
| 13 | { |
||
| 14 | parent::setUp(); |
||
| 15 | |||
| 16 | $this->connectFactory = $this->getMockBuilder('ShopwarePlugins\Connect\Components\ConnectFactory') |
||
| 17 | ->setMethods(array('getConfigComponent', 'getLocalProductQuery', 'getRemoteProductQuery')) |
||
| 18 | ->getMock(); |
||
| 19 | |||
| 20 | $this->configMock = $this->getMockBuilder('\ShopwarePlugins\Connect\Components\Config') |
||
| 21 | ->setConstructorArgs([Shopware()->Models()]) |
||
| 22 | ->setMethods(['getConfig']) |
||
| 23 | ->getMock(); |
||
| 24 | |||
| 25 | $this->localProductQuery = $this->getMockBuilder('ShopwarePlugins\Connect\Components\ProductQuery\LocalProductQuery') |
||
| 26 | ->disableOriginalConstructor() |
||
| 27 | ->getMock(); |
||
| 28 | |||
| 29 | $this->connectFactory->method('getConfigComponent')->willReturn($this->configMock); |
||
| 30 | $this->connectFactory->method('getLocalProductQuery')->willReturn($this->localProductQuery); |
||
| 31 | |||
| 32 | } |
||
| 33 | |||
| 34 | public function testCreateSDKDefaultLocal() |
||
| 35 | { |
||
| 36 | putenv("_TRANSACTION_HOST"); |
||
| 37 | $this->configMock->method('getConfig')->willReturn('sn.connect.local'); |
||
| 38 | |||
| 39 | $this->assertEquals(null, getenv('_TRANSACTION_HOST')); |
||
| 40 | $this->connectFactory->createSDK(); |
||
| 41 | |||
| 42 | $this->assertEquals('transaction.connect.local', getenv('_TRANSACTION_HOST')); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function testCreateSDKSemLocal() |
||
| 46 | { |
||
| 47 | putenv("_TRANSACTION_HOST"); |
||
| 48 | |||
| 49 | $this->configMock->method('getConfig')->willReturn('semdemo.connect.local'); |
||
| 50 | |||
| 51 | $this->assertEquals(null, getenv('_TRANSACTION_HOST')); |
||
| 52 | $this->connectFactory->createSDK(); |
||
| 53 | |||
| 54 | $this->assertEquals('transaction.connect.local', getenv('_TRANSACTION_HOST')); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function testCreateSDKRandomLocal() |
||
| 58 | { |
||
| 59 | putenv("_TRANSACTION_HOST"); |
||
| 60 | $prefix = $this->generateRandomString(); |
||
| 61 | $this->configMock->method('getConfig')->willReturn($prefix . '.connect.local'); |
||
| 62 | |||
| 63 | $this->assertEquals(null, getenv('_TRANSACTION_HOST')); |
||
| 64 | $this->connectFactory->createSDK(); |
||
| 65 | |||
| 66 | $this->assertEquals('transaction.connect.local', getenv('_TRANSACTION_HOST')); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testCreateSDKDefaultStaging() |
||
| 70 | { |
||
| 71 | putenv("_TRANSACTION_HOST"); |
||
| 72 | $this->configMock->method('getConfig')->willReturn('sn.stage.connect.shopware.com'); |
||
| 73 | |||
| 74 | $this->assertEquals(null, getenv('_TRANSACTION_HOST')); |
||
| 75 | $this->connectFactory->createSDK(); |
||
| 76 | |||
| 77 | $this->assertEquals('transaction.stage.connect.shopware.com', getenv('_TRANSACTION_HOST')); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testCreateSDKSemStaging() |
||
| 81 | { |
||
| 82 | putenv("_TRANSACTION_HOST"); |
||
| 83 | $this->configMock->method('getConfig')->willReturn('sn.sem.stage.connect.shopware.com'); |
||
| 84 | |||
| 85 | $this->assertEquals(null, getenv('_TRANSACTION_HOST')); |
||
| 86 | $this->connectFactory->createSDK(); |
||
| 87 | |||
| 88 | $this->assertEquals('transaction.stage.connect.shopware.com', getenv('_TRANSACTION_HOST')); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testCreateSDKMarketplaceStaging() |
||
| 92 | { |
||
| 93 | putenv("_TRANSACTION_HOST"); |
||
| 94 | $prefix = $this->generateRandomString(); |
||
| 95 | $this->configMock->method('getConfig')->willReturn($prefix . '.stage.connect.shopware.com'); |
||
| 96 | |||
| 97 | $this->assertEquals(null, getenv('_TRANSACTION_HOST')); |
||
| 98 | $this->connectFactory->createSDK(); |
||
| 99 | |||
| 100 | $this->assertEquals('transaction.stage.connect.shopware.com', getenv('_TRANSACTION_HOST')); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function testCreateSDKMarketplaceStagingWithMultipleStagings() |
||
| 104 | { |
||
| 105 | putenv("_TRANSACTION_HOST"); |
||
| 106 | $prefix = $this->generateRandomString(); |
||
| 107 | $suffix = '.stage'.rand(1, 9).'.connect.shopware.com'; |
||
| 108 | $this->configMock->method('getConfig')->willReturn($prefix . $suffix); |
||
| 109 | $this->assertEquals(null, getenv('_TRANSACTION_HOST')); |
||
| 110 | $this->connectFactory->createSDK(); |
||
| 111 | |||
| 112 | $this->assertEquals('transaction' . $suffix, getenv('_TRANSACTION_HOST')); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function testCreateSDKLive() |
||
| 116 | { |
||
| 117 | putenv("_TRANSACTION_HOST"); |
||
| 118 | //everything that is "unknown" defaults to live (the default value of transactionHost in the DependencyResolver) |
||
| 119 | $prefix = $this->generateRandomString(20); |
||
| 120 | $this->configMock->method('getConfig')->willReturn($prefix); |
||
| 121 | |||
| 122 | $this->assertEquals(null, getenv('_TRANSACTION_HOST')); |
||
| 123 | $this->connectFactory->createSDK(); |
||
| 124 | |||
| 125 | $this->assertFalse(getenv('_TRANSACTION_HOST')); |
||
| 126 | } |
||
| 127 | |||
| 128 | private function generateRandomString($length = 10) |
||
| 129 | { |
||
| 130 | putenv("_TRANSACTION_HOST"); |
||
| 131 | $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.'; |
||
| 132 | $charactersLength = strlen($characters); |
||
| 133 | $randomString = ''; |
||
| 134 | for ($i = 0; $i < $length; $i++) { |
||
| 135 | $randomString .= $characters[rand(0, $charactersLength - 1)]; |
||
| 136 | } |
||
| 137 | |||
| 138 | return $randomString; |
||
| 139 | } |
||
| 140 | } |
||
| 141 |