Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit a6758a0

Browse files
author
Andrey Helldar
committed
Added tests for Symfony
1 parent 238a51d commit a6758a0

File tree

12 files changed

+166
-18
lines changed

12 files changed

+166
-18
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ build/
88
*.orig
99

1010
composer.lock
11+
12+
###> symfony/phpunit-bridge ###
13+
.phpunit
14+
.phpunit.result.cache
15+
/phpunit.xml
16+
###< symfony/phpunit-bridge ###

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
"require": {
1818
"php": "^7.3|^8.0",
1919
"andrey-helldar/support": "^2.5",
20+
"composer-plugin-api": "^2.0",
2021
"symfony/finder": "^4.0|^5.0"
2122
},
2223
"require-dev": {
24+
"composer/composer": "^2.0",
2325
"mockery/mockery": "^1.3.1",
2426
"orchestra/testbench": "^4.0|^5.0|^6.0",
2527
"phpunit/phpunit": "^8.0|^9.0",
26-
"symfony/symfony": "^4.0|^5.0"
28+
"symfony/console": "^4.0|^5.0",
29+
"symfony/framework-bundle": "^4.0|^5.0",
30+
"symfony/phpunit-bridge": "^4.0|^5.0"
2731
},
2832
"autoload": {
2933
"psr-4": {

src/Concerns/Makeable.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Helldar\EnvSync\Concerns;
4+
5+
use Helldar\EnvSync\Services\Compiler;
6+
use Helldar\EnvSync\Services\Finder;
7+
use Helldar\EnvSync\Services\Parser;
8+
use Helldar\EnvSync\Services\Stringify;
9+
use Helldar\EnvSync\Services\Syncer;
10+
use Helldar\EnvSync\Support\Config;
11+
use Symfony\Component\Finder\Finder as SymfonyFinder;
12+
13+
trait Makeable
14+
{
15+
public static function make(array $config = null): Syncer
16+
{
17+
$parser = static::makeParser();
18+
$stringify = static::makeStringify();
19+
$config = static::makeConfig($config);
20+
$compiler = static::makeCompiler($stringify, $config);
21+
$finder = static::makeFinder();
22+
23+
return new Syncer($parser, $compiler, $finder);
24+
}
25+
26+
protected static function makeParser(): Parser
27+
{
28+
return Parser::make();
29+
}
30+
31+
protected static function makeStringify(): Stringify
32+
{
33+
return Stringify::make();
34+
}
35+
36+
protected static function makeConfig(array $config = null): Config
37+
{
38+
return Config::make($config);
39+
}
40+
41+
protected static function makeCompiler(Stringify $stringify, Config $config): Compiler
42+
{
43+
return Compiler::make($stringify, $config);
44+
}
45+
46+
protected static function makeFinder(): Finder
47+
{
48+
return Finder::make(SymfonyFinder::create());
49+
}
50+
}

src/Frameworks/Symfony/Console/Sync.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Helldar\EnvSync\Frameworks\Symfony\Console;
44

5+
use Composer\Config;
56
use Helldar\EnvSync\Services\Syncer;
67
use Symfony\Component\Console\Command\Command;
78
use Symfony\Component\Console\Input\InputArgument;
@@ -16,12 +17,17 @@ final class Sync extends Command
1617
/** @var \Symfony\Component\Console\Output\OutputInterface */
1718
protected $output;
1819

20+
/** @var \Composer\Config */
21+
protected $config;
22+
23+
/** @var \Helldar\EnvSync\Services\Syncer */
1924
protected $syncer;
2025

21-
public function __construct(Syncer $syncer)
26+
public function __construct(Config $config, Syncer $syncer)
2227
{
2328
parent::__construct();
2429

30+
$this->config = $config;
2531
$this->syncer = $syncer;
2632
}
2733

@@ -33,7 +39,7 @@ protected function configure()
3339
->addOption('path', null, InputArgument::OPTIONAL, 'Gets the path to scan for files');
3440
}
3541

36-
protected function execute(InputInterface $input, OutputInterface $output)
42+
protected function execute(InputInterface $input, OutputInterface $output): int
3743
{
3844
$this->input = $input;
3945
$this->output = $output;
@@ -45,6 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
4551
$this->sync($filename);
4652

4753
$this->info("The found keys were successfully saved to the {$filename} file.");
54+
55+
return 0;
4856
}
4957

5058
protected function sync(string $filename): void
@@ -72,8 +80,9 @@ protected function optionPath(): ?string
7280

7381
protected function realPath(): string
7482
{
75-
dd($this->getUsages());
76-
// return realpath($this->pa);
83+
$vendor = $this->config->get('vendor-dir');
84+
85+
return realpath($vendor . '/../');
7786
}
7887

7988
protected function info(string $message): void

src/Services/Compiler.php

+3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace Helldar\EnvSync\Services;
44

55
use Helldar\EnvSync\Support\Config;
6+
use Helldar\Support\Concerns\Makeable;
67
use Helldar\Support\Facades\Helpers\Str;
78

89
final class Compiler
910
{
11+
use Makeable;
12+
1013
protected $hides = ['CLIENT', 'HOOK', 'KEY', 'LOGIN', 'PASS', 'SECRET', 'TOKEN', 'USER'];
1114

1215
protected $separator = "\n";

src/Services/Finder.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Helldar\EnvSync\Services;
44

5+
use Helldar\Support\Concerns\Makeable;
56
use Symfony\Component\Finder\Finder as SymfonyFinder;
67

78
final class Finder
89
{
10+
use Makeable;
11+
912
protected $exclude_dirs = ['vendor', 'node_modules', '.idea', '.git', '.github', 'tests'];
1013

1114
protected $names = ['*.php', '*.json', '*.yml', '*.yaml', '*.twig'];

src/Services/Parser.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Helldar\EnvSync\Services;
44

5+
use Helldar\Support\Concerns\Makeable;
56
use Helldar\Support\Facades\Helpers\Str;
67

78
final class Parser
89
{
10+
use Makeable;
11+
912
protected $files = [];
1013

1114
protected $keys = [];

src/Services/Stringify.php

+4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Helldar\EnvSync\Services;
44

5+
use Helldar\Support\Concerns\Makeable;
6+
57
final class Stringify
68
{
9+
use Makeable;
10+
711
public function get($value): string
812
{
913
switch (true) {

src/Services/Syncer.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Helldar\EnvSync\Services;
44

5+
use Helldar\EnvSync\Concerns\Makeable;
56
use Helldar\Support\Facades\Helpers\Filesystem\Directory;
67
use Helldar\Support\Facades\Helpers\Filesystem\File;
78

89
final class Syncer
910
{
11+
use Makeable;
12+
1013
protected $compiler;
1114

1215
protected $parser;

src/Support/Config.php

+4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Helldar\EnvSync\Support;
44

5+
use Helldar\Support\Concerns\Makeable;
6+
57
final class Config
68
{
9+
use Makeable;
10+
711
/** @var array */
812
protected $config;
913

tests/Cases/SymfonyTestCase.php

+71-12
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,101 @@
22

33
namespace Tests\Cases;
44

5+
use Composer\Config;
6+
use Helldar\EnvSync\Frameworks\Symfony\Console\Sync;
7+
use Helldar\EnvSync\Services\Syncer;
8+
use PHPUnit\Framework\TestCase;
59
use Symfony\Bundle\FrameworkBundle\Console\Application;
6-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
710
use Symfony\Component\Console\Command\Command;
811
use Symfony\Component\Console\Tester\CommandTester;
9-
use Symfony\Component\HttpKernel\KernelInterface;
12+
use Symfony\Component\DependencyInjection\ContainerInterface;
13+
use Symfony\Component\HttpKernel\Kernel;
1014
use Tests\Concerns\Configurable;
1115
use Tests\Concerns\Files;
1216

13-
abstract class SymfonyTestCase extends KernelTestCase
17+
abstract class SymfonyTestCase extends TestCase
1418
{
1519
use Configurable;
1620
use Files;
1721

18-
protected function call(string $name, array $options = []): string
22+
/** @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit\Framework\MockObject\MockObject */
23+
protected $container;
24+
25+
/** @var \Symfony\Bundle\FrameworkBundle\Console\Application */
26+
protected $application;
27+
28+
protected function setUp(): void
1929
{
20-
$command = $this->application()->find($name);
30+
$this->mockContainer();
31+
$this->mockApplication();
32+
$this->mockCommand();
33+
}
2134

22-
$tester = $this->tester($command);
35+
protected function mockContainer(): void
36+
{
37+
$this->container = $this->getMockBuilder(ContainerInterface::class)->getMock();
38+
}
39+
40+
/**
41+
* @return \Symfony\Component\HttpKernel\Kernel|\PHPUnit\Framework\MockObject\MockBuilder
42+
*/
43+
protected function mockKernel()
44+
{
45+
$kernel = $this->getMockBuilder(Kernel::class)
46+
->disableOriginalConstructor()
47+
->getMock();
2348

24-
$tester->execute($options);
49+
$kernel->expects($this->once())
50+
->method('getBundles')
51+
->will($this->returnValue([]));
2552

26-
return $tester->getDisplay();
53+
$kernel->expects($this->any())
54+
->method('getContainer')
55+
->will($this->returnValue($this->container));
56+
57+
return $kernel;
58+
}
59+
60+
protected function mockApplication(): void
61+
{
62+
$this->application = new Application($this->mockKernel());
63+
}
64+
65+
protected function mockCommand(): void
66+
{
67+
$command = $this->getCommand();
68+
69+
$this->application->add($command);
2770
}
2871

29-
protected function application(): Application
72+
protected function composerConfig(): Config
3073
{
31-
return new Application($this->kernel());
74+
return new Config();
3275
}
3376

34-
protected function kernel(): KernelInterface
77+
protected function getSyncer(): Syncer
3578
{
36-
return static::createKernel();
79+
return Syncer::make();
80+
}
81+
82+
protected function getCommand(): Sync
83+
{
84+
return new Sync($this->composerConfig(), $this->getSyncer());
3785
}
3886

3987
protected function tester(Command $command): CommandTester
4088
{
4189
return new CommandTester($command);
4290
}
91+
92+
protected function call(string $name, array $options = [])
93+
{
94+
$command = $this->application->find($name);
95+
96+
$tester = $this->tester($command);
97+
98+
$tester->execute(array_merge(['command' => $name], $options));
99+
100+
return $tester->getDisplay();
101+
}
43102
}

tests/Symfony/MainTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function testCustomPathFailed()
2424
{
2525
$this->expectException(DirectoryNotFoundException::class);
2626

27-
$this->call('env:sync', ['--path' => __DIR__ . '/foo']);
27+
$this->call('env:sync', ['--path' => '/foo']);
2828
}
2929
}

0 commit comments

Comments
 (0)