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 |
||
| 8 | class AbstractModelTest extends PHPUnit_Framework_TestCase |
||
| 9 | { |
||
| 10 | protected $vocabulary = array( |
||
| 11 | 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', |
||
| 12 | 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', |
||
| 13 | 'owl' => 'http://www.w3.org/2002/07/owl#', |
||
| 14 | 'xsd' => 'http://www.w3.org/2001/XMLSchema#', |
||
| 15 | 'dct' => 'http://purl.org/dc/terms/', |
||
| 16 | 'void' => 'http://rdfs.org/ns/void#', |
||
| 17 | 'prov' => 'http://www.w3.org/ns/prov#', |
||
| 18 | 'schema' => 'http://schema.org/', |
||
| 19 | 'wgs' => 'http://www.w3.org/2003/01/geo/wgs84_pos#', |
||
| 20 | 'foaf' => 'http://xmlns.com/foaf/0.1/', |
||
| 21 | 'dq' => 'http://purl.org/linked-data/cube#', |
||
| 22 | 'daq' => 'http://purl.org/eis/vocab/daq#', |
||
| 23 | 'botk' => 'http://http://linkeddata.center/botk/v1#', |
||
| 24 | ); |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * @dataProvider data |
||
| 29 | */ |
||
| 30 | public function testConstructor($data, $expectedData) |
||
| 31 | { |
||
| 32 | $localBusiness = new DummyModel($data); |
||
| 33 | $this->assertEquals($expectedData, $localBusiness->asArray()); |
||
| 34 | } |
||
| 35 | public function data() |
||
| 36 | { |
||
| 37 | return array( |
||
| 38 | array( array(), array('base'=> 'http://linkeddata.center/botk/resource/'),), |
||
| 39 | array( array('base' => 'urn:a:','id'=>'x'), array('base'=> 'urn:a:','id'=>'x')), |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | public function testConstructorWithCustomOptions() |
||
| 45 | { |
||
| 46 | $localBusiness = new DummyModel(array(), array ( |
||
| 47 | 'base' => array('default' => 'urn:a:'), |
||
| 48 | 'lang' => array('default' => 'en'), |
||
| 49 | )); |
||
| 50 | $options = $localBusiness->getOptions(); |
||
| 51 | $this->assertEquals( |
||
| 52 | array( |
||
| 53 | 'default' => 'urn:a:', |
||
| 54 | 'filter' => FILTER_SANITIZE_URL, |
||
| 55 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 56 | ), |
||
| 57 | $options['base'] |
||
| 58 | ); |
||
| 59 | $this->assertEquals(array('default' => 'en'),$options['lang']); |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | public function testgetVocabularies() |
||
| 64 | { |
||
| 65 | $obj = new DummyModel(array()); |
||
| 66 | |||
| 67 | $this->assertEquals($this->vocabulary, $obj->getVocabularies()); |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | public function testSetVocabulary() |
||
| 72 | { |
||
| 73 | $vocabulary = $this->vocabulary; |
||
| 74 | $vocabulary['my'] = 'urn:test:'; |
||
| 75 | |||
| 76 | $obj = new DummyModel(array()); |
||
| 77 | $obj->setVocabulary('my','urn:test:'); |
||
| 78 | |||
| 79 | $this->assertEquals($vocabulary, $obj->getVocabularies()); |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | public function testUnsetVocabulary() |
||
| 84 | { |
||
| 85 | $vocabulary = $this->vocabulary; |
||
| 86 | unset($vocabulary['foaf']); |
||
| 87 | |||
| 88 | $obj = new DummyModel(array()); |
||
| 89 | $obj->unsetVocabulary('foaf'); |
||
| 90 | |||
| 91 | $this->assertEquals($vocabulary, $obj->getVocabularies()); |
||
| 92 | } |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * @dataProvider uris |
||
| 97 | */ |
||
| 98 | public function testGetUri($data, $expectedData) |
||
| 99 | { |
||
| 100 | $obj = new DummyModel($data); |
||
| 101 | $obj->setIdGenerator(function($d){return'abc';}); |
||
| 102 | $this->assertEquals($expectedData, $obj->getUri()); |
||
| 103 | } |
||
| 104 | public function uris() |
||
| 105 | { |
||
| 106 | return array( |
||
| 107 | array( array(), 'http://linkeddata.center/botk/resource/abc'), |
||
| 108 | array( array('base'=>'http://example.com/resource/'), 'http://example.com/resource/abc'), |
||
| 109 | array( array('base'=>'http://example.com/resource/', 'id'=>'efg'), 'http://example.com/resource/efg'), |
||
| 110 | array( array('uri'=>'http://example.com/resource/ijk'), 'http://example.com/resource/ijk'), |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function testTurtleHeader() |
||
| 115 | { |
||
| 116 | $obj = new DummyModel(array()); |
||
| 117 | $s =""; |
||
| 118 | foreach( $this->vocabulary as $p=>$v){ |
||
| 119 | $s.= "@prefix $p: <$v> .\n"; |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->assertEquals($s, $obj->getTurtleHeader()); |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | public function testTurtleHeaderWithBase() |
||
| 127 | { |
||
| 128 | $obj = new DummyModel(array()); |
||
| 129 | $s ="@base <urn:a:b> .\n"; |
||
| 130 | foreach( $this->vocabulary as $p=>$v){ |
||
| 131 | $s.= "@prefix $p: <$v> .\n"; |
||
| 132 | } |
||
| 133 | |||
| 134 | $this->assertEquals($s, $obj->getTurtleHeader('urn:a:b')); |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | public function testAsString() |
||
| 140 | { |
||
| 141 | $obj = new DummyModel(array()); |
||
| 142 | $s= $obj->getTurtleHeader() ."\n<urn:a:b> owl:sameAs <urn:a:b> ."; |
||
| 143 | |||
| 144 | $this->assertEquals($s, (string)$obj); |
||
| 145 | } |
||
| 146 | |||
| 147 | } |
||
| 148 | |||
| 149 |