|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DotTest\Maker; |
| 6 | + |
| 7 | +use Dot\Maker\Context; |
| 8 | +use org\bovigo\vfs\vfsStream; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +use function sprintf; |
| 12 | + |
| 13 | +class ContextTest extends TestCase |
| 14 | +{ |
| 15 | + public function testWillNotInstantiateWithoutComposerJson(): void |
| 16 | + { |
| 17 | + $projectPath = '/invalid/path'; |
| 18 | + $this->expectExceptionMessage(sprintf('%s/composer.json: not found', $projectPath)); |
| 19 | + |
| 20 | + new Context($projectPath); |
| 21 | + } |
| 22 | + |
| 23 | + public function testWillNotInstantiateWithInvalidComposerJson(): void |
| 24 | + { |
| 25 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 26 | + 'composer.json' => '', |
| 27 | + ]); |
| 28 | + $this->expectExceptionMessage(sprintf('%s/composer.json: invalid JSON', $fileSystem->url())); |
| 29 | + |
| 30 | + new Context($fileSystem->url()); |
| 31 | + } |
| 32 | + |
| 33 | + public function testWillNotInstantiateWithoutComposerJsonAutoload(): void |
| 34 | + { |
| 35 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 36 | + 'composer.json' => '{}', |
| 37 | + ]); |
| 38 | + $this->expectExceptionMessage(sprintf('%s/composer.json: key "autoload" not found', $fileSystem->url())); |
| 39 | + |
| 40 | + new Context($fileSystem->url()); |
| 41 | + } |
| 42 | + |
| 43 | + public function testWillNotInstantiateWithoutComposerJsonAutoloadPSR4(): void |
| 44 | + { |
| 45 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 46 | + 'composer.json' => '{"autoload":[]}', |
| 47 | + ]); |
| 48 | + $this->expectExceptionMessage(sprintf('%s/composer.json: key "autoload"."psr-4" not found', $fileSystem->url())); |
| 49 | + |
| 50 | + new Context($fileSystem->url()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testWillDetectCoreArchitectureWhenUsed(): void |
| 54 | + { |
| 55 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 56 | + 'composer.json' => '{ |
| 57 | + "autoload": { |
| 58 | + "psr-4": { |
| 59 | + "Api\\\\App\\\\": "src/App/src/", |
| 60 | + "Core\\\\App\\\\": "src/Core/src/App/src/" |
| 61 | + } |
| 62 | + } |
| 63 | + }', |
| 64 | + ]); |
| 65 | + |
| 66 | + $context = new Context($fileSystem->url()); |
| 67 | + $this->assertTrue($context->hasCore()); |
| 68 | + } |
| 69 | + |
| 70 | + public function testWillDetectCoreArchitectureWhenNotUsed(): void |
| 71 | + { |
| 72 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 73 | + 'composer.json' => '{ |
| 74 | + "autoload": { |
| 75 | + "psr-4": { |
| 76 | + "Api\\\\App\\\\": "src/App/src/", |
| 77 | + "Other\\\\App\\\\": "src/Other/src/" |
| 78 | + } |
| 79 | + } |
| 80 | + }', |
| 81 | + ]); |
| 82 | + |
| 83 | + $context = new Context($fileSystem->url()); |
| 84 | + $this->assertFalse($context->hasCore()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testWillDetectProjectTypeAdmin(): void |
| 88 | + { |
| 89 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 90 | + 'composer.json' => '{ |
| 91 | + "autoload": { |
| 92 | + "psr-4": { |
| 93 | + "Admin\\\\App\\\\": "src/Admin/src/", |
| 94 | + "Other\\\\App\\\\": "src/Other/src/" |
| 95 | + } |
| 96 | + } |
| 97 | + }', |
| 98 | + ]); |
| 99 | + |
| 100 | + $context = new Context($fileSystem->url()); |
| 101 | + $this->assertTrue($context->isAdmin()); |
| 102 | + $this->assertFalse($context->isApi()); |
| 103 | + $this->assertFalse($context->isFrontend()); |
| 104 | + $this->assertFalse($context->isLight()); |
| 105 | + $this->assertFalse($context->isQueue()); |
| 106 | + $this->assertSame(Context::NAMESPACE_ADMIN, $context->getProjectType()); |
| 107 | + } |
| 108 | + |
| 109 | + public function testWillDetectProjectTypeApi(): void |
| 110 | + { |
| 111 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 112 | + 'composer.json' => '{ |
| 113 | + "autoload": { |
| 114 | + "psr-4": { |
| 115 | + "Api\\\\App\\\\": "src/App/src/", |
| 116 | + "Other\\\\App\\\\": "src/Other/src/" |
| 117 | + } |
| 118 | + } |
| 119 | + }', |
| 120 | + ]); |
| 121 | + |
| 122 | + $context = new Context($fileSystem->url()); |
| 123 | + $this->assertFalse($context->isAdmin()); |
| 124 | + $this->assertTrue($context->isApi()); |
| 125 | + $this->assertFalse($context->isFrontend()); |
| 126 | + $this->assertFalse($context->isLight()); |
| 127 | + $this->assertFalse($context->isQueue()); |
| 128 | + $this->assertSame(Context::NAMESPACE_API, $context->getProjectType()); |
| 129 | + } |
| 130 | + |
| 131 | + public function testWillDetectProjectTypeFrontend(): void |
| 132 | + { |
| 133 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 134 | + 'composer.json' => '{ |
| 135 | + "autoload": { |
| 136 | + "psr-4": { |
| 137 | + "Frontend\\\\App\\\\": "src/Frontend/src/", |
| 138 | + "Other\\\\App\\\\": "src/Other/src/" |
| 139 | + } |
| 140 | + } |
| 141 | + }', |
| 142 | + ]); |
| 143 | + |
| 144 | + $context = new Context($fileSystem->url()); |
| 145 | + $this->assertFalse($context->isAdmin()); |
| 146 | + $this->assertFalse($context->isApi()); |
| 147 | + $this->assertTrue($context->isFrontend()); |
| 148 | + $this->assertFalse($context->isLight()); |
| 149 | + $this->assertFalse($context->isQueue()); |
| 150 | + $this->assertSame(Context::NAMESPACE_FRONTEND, $context->getProjectType()); |
| 151 | + } |
| 152 | + |
| 153 | + public function testWillDetectProjectTypeLight(): void |
| 154 | + { |
| 155 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 156 | + 'composer.json' => '{ |
| 157 | + "autoload": { |
| 158 | + "psr-4": { |
| 159 | + "Light\\\\App\\\\": "src/Light/src/", |
| 160 | + "Other\\\\App\\\\": "src/Other/src/" |
| 161 | + } |
| 162 | + } |
| 163 | + }', |
| 164 | + ]); |
| 165 | + |
| 166 | + $context = new Context($fileSystem->url()); |
| 167 | + $this->assertFalse($context->isAdmin()); |
| 168 | + $this->assertFalse($context->isApi()); |
| 169 | + $this->assertFalse($context->isFrontend()); |
| 170 | + $this->assertTrue($context->isLight()); |
| 171 | + $this->assertFalse($context->isQueue()); |
| 172 | + $this->assertSame(Context::NAMESPACE_LIGHT, $context->getProjectType()); |
| 173 | + } |
| 174 | + |
| 175 | + public function testWillDetectProjectTypeQueue(): void |
| 176 | + { |
| 177 | + $fileSystem = vfsStream::setup('root', 0644, [ |
| 178 | + 'composer.json' => '{ |
| 179 | + "autoload": { |
| 180 | + "psr-4": { |
| 181 | + "Queue\\\\App\\\\": "src/Queue/src/", |
| 182 | + "Other\\\\App\\\\": "src/Other/src/" |
| 183 | + } |
| 184 | + } |
| 185 | + }', |
| 186 | + ]); |
| 187 | + |
| 188 | + $context = new Context($fileSystem->url()); |
| 189 | + $this->assertFalse($context->isAdmin()); |
| 190 | + $this->assertFalse($context->isApi()); |
| 191 | + $this->assertFalse($context->isFrontend()); |
| 192 | + $this->assertFalse($context->isLight()); |
| 193 | + $this->assertTrue($context->isQueue()); |
| 194 | + $this->assertSame(Context::NAMESPACE_QUEUE, $context->getProjectType()); |
| 195 | + } |
| 196 | +} |
0 commit comments