|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Presenter\Analyze\Component\Shared\Component; |
| 4 | +use App\Presenter\Analyze\Component\Shared\ComponentMapper; |
| 5 | + |
| 6 | +it('should map metrics to components', function () { |
| 7 | + |
| 8 | + $mapper = new ComponentMapper(); |
| 9 | + |
| 10 | + $metrics = [ |
| 11 | + 'A' => [ |
| 12 | + $this->oneAnalyzeMetric()->withName('A\Class1')->build(), |
| 13 | + $this->oneAnalyzeMetric()->withName('A\Class2')->build(), |
| 14 | + $this->oneAnalyzeMetric()->withName('A\Class3')->build(), |
| 15 | + ] |
| 16 | + ]; |
| 17 | + |
| 18 | + $components = $mapper->from($metrics); |
| 19 | + |
| 20 | + expect($components)->toHaveCount(1); |
| 21 | + expect($components[0])->toBeInstanceOf(Component::class); |
| 22 | + expect($components[0]->name())->toBe('A'); |
| 23 | +}); |
| 24 | + |
| 25 | +it('should map metrics to components with dependencies', function () { |
| 26 | + |
| 27 | + $mapper = new ComponentMapper(); |
| 28 | + |
| 29 | + $metrics = [ |
| 30 | + 'A' => [ |
| 31 | + $this->oneAnalyzeMetric()->withName('A\Class1')->withDependencies(['B\Class2'])->build(), |
| 32 | + ], |
| 33 | + 'B' => [ |
| 34 | + // |
| 35 | + ] |
| 36 | + ]; |
| 37 | + |
| 38 | + $components = $mapper->from($metrics); |
| 39 | + |
| 40 | + expect($components)->toHaveCount(2); |
| 41 | + expect($components[0]->dependencies())->toBe(['B']); |
| 42 | + |
| 43 | +}); |
| 44 | + |
| 45 | +it('should not keep dependencies from unwanted namespaces', function () { |
| 46 | + |
| 47 | + $mapper = new ComponentMapper(); |
| 48 | + |
| 49 | + $metrics = [ |
| 50 | + 'A' => [ |
| 51 | + /** |
| 52 | + * This dependency is in an unwanted namespace C |
| 53 | + */ |
| 54 | + $this->oneAnalyzeMetric()->withName('A\Class1')->withDependencies(['C\Class2'])->build(), |
| 55 | + ], |
| 56 | + 'B' => [ |
| 57 | + // |
| 58 | + ] |
| 59 | + ]; |
| 60 | + |
| 61 | + $components = $mapper->from($metrics); |
| 62 | + |
| 63 | + expect($components)->toHaveCount(2); |
| 64 | + expect($components[0]->dependencies())->toBe([]); |
| 65 | +}); |
| 66 | + |
| 67 | +it('should calculate the average abstractness', function () { |
| 68 | + |
| 69 | + $mapper = new ComponentMapper(); |
| 70 | + |
| 71 | + $metrics = [ |
| 72 | + 'A' => [ |
| 73 | + $this->oneAnalyzeMetric()->build(), |
| 74 | + $this->oneAnalyzeMetric()->isAbstract()->build(), |
| 75 | + $this->oneAnalyzeMetric()->isAbstract()->build(), |
| 76 | + $this->oneAnalyzeMetric()->isAbstract()->build(), |
| 77 | + ], |
| 78 | + ]; |
| 79 | + |
| 80 | + $components = $mapper->from($metrics); |
| 81 | + |
| 82 | + expect($components[0]->countClasses())->toBe(4); |
| 83 | + expect($components[0]->countAbstractions())->toBe(3); |
| 84 | + expect($components[0]->abstractness())->toBe(0.75); |
| 85 | +}); |
| 86 | + |
| 87 | +it('should calculate the average instability', function () { |
| 88 | + |
| 89 | + $mapper = new ComponentMapper(); |
| 90 | + |
| 91 | + $metrics = [ |
| 92 | + 'A' => [ |
| 93 | + $this->oneAnalyzeMetric()->withInstability(0.3)->build(), |
| 94 | + $this->oneAnalyzeMetric()->withInstability(0.7)->build(), |
| 95 | + $this->oneAnalyzeMetric()->withInstability(1)->build(), |
| 96 | + ], |
| 97 | + ]; |
| 98 | + |
| 99 | + $components = $mapper->from($metrics); |
| 100 | + |
| 101 | + expect($components[0]->instability())->toBe(0.67); |
| 102 | +}); |
0 commit comments