Skip to content

Commit

Permalink
Changing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer committed Oct 3, 2024
1 parent 7ef8334 commit 819f144
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 20 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache" executionOrder="depends,defects" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="true" beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
cacheResult="true"
executionOrder="random"
resolveDependencies="false"
requireCoverageMetadata="false"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="false"
failOnRisky="true"
failOnWarning="false">

<php>
<env name="APP_ENV" value="test" />
</php>

<testsuites>
<testsuite name="default">
<directory>tests</directory>
<exclude>tests/Store/AbstractStoreTest.php</exclude>
</testsuite>
</testsuites>

<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
<include>
<directory>src</directory>
</include>
</source>

</phpunit>
15 changes: 13 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -205,4 +211,9 @@ public function get(string $id): mixed
{
return $this->containerBuilder->get($id);
}

public function getContainer(): ContainerBuilder
{
return $this->containerBuilder;
}
}
5 changes: 3 additions & 2 deletions src/Business/Cognitive/CognitiveMetricsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
{
Expand Down Expand Up @@ -65,7 +66,7 @@ private function getCodeFromFile(SplFileInfo $file): string
*
* @param iterable<SplFileInfo> $files
* @return CognitiveMetricsCollection
* @throws CognitiveAnalysisException
* @throws CognitiveAnalysisException|ExceptionInterface
*/
private function findMetrics(iterable $files): CognitiveMetricsCollection
{
Expand Down
24 changes: 21 additions & 3 deletions tests/Unit/Business/Cognitive/CognitiveMetricsCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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(),
Expand All @@ -39,7 +57,7 @@ protected function setUp(): void
new Processor(),
new ConfigLoader(),
),
new MessageBus(),
$bus
);

$this->configService = new ConfigService(
Expand Down Expand Up @@ -75,7 +93,7 @@ public function testCollectWithExcludedClasses(): void
),
new DirectoryScanner(),
$configService,
new MessageBus(),
$this->messageBus
);

$path = './tests/TestCode';
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Business/DirectoryScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 819f144

Please sign in to comment.