diff --git a/config.yml b/config.yml index a691095..dd874fd 100644 --- a/config.yml +++ b/config.yml @@ -17,11 +17,11 @@ cognitive: scale: 5.0 enabled: true variableCount: - threshold: 2 + threshold: 4 scale: 5.0 enabled: true propertyCallCount: - threshold: 2 + threshold: 4 scale: 15.0 enabled: true ifCount: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index aa1d602..ef59533 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,32 @@ - + + + + + + tests - tests/Store/AbstractStoreTest.php + src + diff --git a/src/Application.php b/src/Application.php index fc41fc7..eac7223 100644 --- a/src/Application.php +++ b/src/Application.php @@ -26,6 +26,7 @@ use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\ConsoleOutput; +use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -85,8 +86,13 @@ private function registerServices(): void $this->containerBuilder->register(NodeTraverserInterface::class, NodeTraverser::class) ->setPublic(true); - $this->containerBuilder->register(OutputInterface::class, ConsoleOutput::class) - ->setPublic(true); + if (getenv('APP_ENV') === 'test') { + $this->containerBuilder->register(OutputInterface::class, NullOutput::class) + ->setPublic(true); + } else { + $this->containerBuilder->register(OutputInterface::class, ConsoleOutput::class) + ->setPublic(true); + } $this->containerBuilder->register(InputInterface::class, ArgvInput::class) ->setPublic(true); @@ -205,4 +211,9 @@ public function get(string $id): mixed { return $this->containerBuilder->get($id); } + + public function getContainer(): ContainerBuilder + { + return $this->containerBuilder; + } } diff --git a/src/Business/Cognitive/CognitiveMetricsCollector.php b/src/Business/Cognitive/CognitiveMetricsCollector.php index 18918c1..5afb848 100644 --- a/src/Business/Cognitive/CognitiveMetricsCollector.php +++ b/src/Business/Cognitive/CognitiveMetricsCollector.php @@ -11,6 +11,7 @@ use Phauthentic\CognitiveCodeAnalysis\Config\CognitiveConfig; use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService; use SplFileInfo; +use Symfony\Component\Messenger\Exception\ExceptionInterface; use Symfony\Component\Messenger\MessageBusInterface; /** @@ -32,7 +33,7 @@ public function __construct( * @param string $path * @param CognitiveConfig $config * @return CognitiveMetricsCollection - * @throws CognitiveAnalysisException + * @throws CognitiveAnalysisException|ExceptionInterface */ public function collect(string $path, CognitiveConfig $config): CognitiveMetricsCollection { @@ -65,7 +66,7 @@ private function getCodeFromFile(SplFileInfo $file): string * * @param iterable $files * @return CognitiveMetricsCollection - * @throws CognitiveAnalysisException + * @throws CognitiveAnalysisException|ExceptionInterface */ private function findMetrics(iterable $files): CognitiveMetricsCollection { diff --git a/tests/Unit/Business/Cognitive/CognitiveMetricsCollectorTest.php b/tests/Unit/Business/Cognitive/CognitiveMetricsCollectorTest.php index c94eecb..918752d 100644 --- a/tests/Unit/Business/Cognitive/CognitiveMetricsCollectorTest.php +++ b/tests/Unit/Business/Cognitive/CognitiveMetricsCollectorTest.php @@ -4,19 +4,25 @@ namespace Phauthentic\CognitiveCodeAnalysis\Tests\Unit\Business\Cognitive; -use Phauthentic\CognitiveCodeAnalysis\Business\AbstractMetricCollector; +use Phauthentic\CognitiveCodeAnalysis\Application; use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollection; use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollector; use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Parser; use Phauthentic\CognitiveCodeAnalysis\Business\DirectoryScanner; use Phauthentic\CognitiveCodeAnalysis\Config\ConfigLoader; use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService; +use PHPMD\Console\OutputInterface; use PhpParser\NodeTraverser; use PhpParser\ParserFactory; use PHPUnit\Framework\TestCase; use RuntimeException; use Symfony\Component\Config\Definition\Processor; +use Symfony\Component\Console\Output\NullOutput; +use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Handler\HandlersLocator; use Symfony\Component\Messenger\MessageBus; +use Symfony\Component\Messenger\MessageBusInterface; +use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware; /** * @@ -25,10 +31,22 @@ class CognitiveMetricsCollectorTest extends TestCase { private CognitiveMetricsCollector $metricsCollector; private ConfigService $configService; + private MessageBusInterface $messageBus; protected function setUp(): void { parent::setUp(); + + $bus = $this->getMockBuilder(MessageBusInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $bus->expects($this->any()) + ->method('dispatch') + ->willReturn(new Envelope(new \stdClass())); + + $this->messageBus = $bus; + $this->metricsCollector = new CognitiveMetricsCollector( new Parser( new ParserFactory(), @@ -39,7 +57,7 @@ protected function setUp(): void new Processor(), new ConfigLoader(), ), - new MessageBus(), + $bus ); $this->configService = new ConfigService( @@ -75,7 +93,7 @@ public function testCollectWithExcludedClasses(): void ), new DirectoryScanner(), $configService, - new MessageBus(), + $this->messageBus ); $path = './tests/TestCode'; diff --git a/tests/Unit/Business/DirectoryScannerTest.php b/tests/Unit/Business/DirectoryScannerTest.php index 649d734..fd058ef 100644 --- a/tests/Unit/Business/DirectoryScannerTest.php +++ b/tests/Unit/Business/DirectoryScannerTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Phauthentic\CognitiveCodeAnalysis\Tests\Unit\Business\Halstead; +namespace Phauthentic\CognitiveCodeAnalysis\Tests\Unit\Business; use FilesystemIterator; use Phauthentic\CognitiveCodeAnalysis\Business\DirectoryScanner;