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 |
||
3 | namespace Dynamic\FoxyStripe\Test; |
||
4 | |||
5 | use Dynamic\FoxyStripe\Model\Order; |
||
6 | use SilverStripe\Dev\SapphireTest; |
||
7 | use SilverStripe\ORM\FieldType\DBHTMLVarchar; |
||
8 | use SilverStripe\Security\Member; |
||
9 | |||
10 | class OrderTest extends SapphireTest |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected static $fixture_file = 'fixtures.yml'; |
||
16 | |||
17 | /** |
||
18 | * |
||
19 | */ |
||
20 | public function testFieldLabels() |
||
21 | { |
||
22 | $object = $this->objFromFixture(Order::class, 'one'); |
||
23 | $labels = $object->FieldLabels(); |
||
24 | $this->assertNotNull($labels['Order_ID']); |
||
25 | $this->assertNotNull($labels['TransactionDate']); |
||
26 | $this->assertNotNull($labels['TransactionDate.NiceUS']); |
||
27 | $this->assertNotNull($labels['Member.Name']); |
||
28 | $this->assertNotNull($labels['Member.ID']); |
||
29 | $this->assertNotNull($labels['ProductTotal.Nice']); |
||
30 | $this->assertNotNull($labels['TaxTotal.Nice']); |
||
31 | $this->assertNotNull($labels['ShippingTotal.Nice']); |
||
32 | $this->assertNotNull($labels['OrderTotal']); |
||
33 | $this->assertNotNull($labels['OrderTotal.Nice']); |
||
34 | $this->assertNotNull($labels['ReceiptLink']); |
||
35 | $this->assertNotNull($labels['Details.ProductID']); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | */ |
||
41 | public function testReceiptLink() |
||
42 | { |
||
43 | $object = $this->objFromFixture(Order::class, 'one'); |
||
44 | $this->assertInstanceOf(DBHTMLVarchar::class, $object->ReceiptLink()); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * |
||
49 | */ |
||
50 | public function testCanView() |
||
51 | { |
||
52 | $object = $this->objFromFixture(Order::class, 'one'); |
||
53 | $admin = $this->objFromFixture(Member::class, 'admin'); |
||
54 | $this->assertTrue($object->canView($admin)); |
||
55 | $member = $this->objFromFixture(Member::class, 'customer'); |
||
56 | $this->assertFalse($object->canView($member)); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * |
||
61 | */ |
||
62 | public function testCanEdit() |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | */ |
||
74 | public function testCanDelete() |
||
75 | { |
||
76 | $object = $this->objFromFixture(Order::class, 'one'); |
||
77 | $admin = $this->objFromFixture(Member::class, 'admin'); |
||
78 | $this->assertFalse($object->canDelete($admin)); |
||
79 | $member = $this->objFromFixture(Member::class, 'customer'); |
||
80 | $this->assertFalse($object->canDelete($member)); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * |
||
85 | */ |
||
86 | public function testCanCreate() |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * |
||
97 | */ |
||
98 | public function testProvidePermissions() |
||
99 | { |
||
100 | $object = $this->objFromFixture(Order::class, 'one'); |
||
107 |