Skip to content

Commit a4a8969

Browse files
Moved tests to properly directories
1 parent 3b425e3 commit a4a8969

File tree

8 files changed

+151
-31
lines changed

8 files changed

+151
-31
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ jobs:
6363
if: matrix.symfony == '^7.0'
6464
run: composer remove --dev willdurand/hateoas-bundle --no-update --no-scripts
6565

66+
-
67+
name: Install MongoDB ODM package
68+
run: |
69+
composer require --dev doctrine/mongodb-odm-bundle "^5.0" --no-update --no-scripts
70+
(cd src/Component && composer require --dev doctrine/mongodb-odm "^2.8" --no-update --no-scripts)
71+
6672
-
6773
name: Install dependencies
6874
run: |

src/Bundle/spec/Event/ResourceControllerEventSpec.php renamed to tests/Bundle/Event/ResourceControllerEventTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use PHPUnit\Framework\TestCase;
1717
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
18+
use Sylius\Resource\Symfony\EventDispatcher\GenericEvent;
1819
use Symfony\Component\HttpFoundation\Response;
1920

2021
final class ResourceControllerEventTest extends TestCase
@@ -23,7 +24,8 @@ final class ResourceControllerEventTest extends TestCase
2324

2425
protected function setUp(): void
2526
{
26-
$this->event = new ResourceControllerEvent('message');
27+
$this->event = new ResourceControllerEvent();
28+
$this->event->setMessage('message');
2729
}
2830

2931
public function testItHasEmptyMessageByDefault(): void
@@ -33,11 +35,6 @@ public function testItHasEmptyMessageByDefault(): void
3335
$this->assertSame('', $event->getMessage());
3436
}
3537

36-
public function testItCanBeInstantiatedWithMessage(): void
37-
{
38-
$this->assertSame('message', $this->event->getMessage());
39-
}
40-
4138
public function testItCanSetAndGetMessage(): void
4239
{
4340
$this->event->setMessage('custom_message');
@@ -52,9 +49,9 @@ public function testItHasEmptyMessageTypeByDefault(): void
5249

5350
public function testItCanSetAndGetMessageType(): void
5451
{
55-
$this->event->setMessageType(ResourceControllerEvent::TYPE_SUCCESS);
52+
$this->event->setMessageType(GenericEvent::TYPE_SUCCESS);
5653

57-
$this->assertSame(ResourceControllerEvent::TYPE_SUCCESS, $this->event->getMessageType());
54+
$this->assertSame(GenericEvent::TYPE_SUCCESS, $this->event->getMessageType());
5855
}
5956

6057
public function testItHasEmptyMessageParametersByDefault(): void
@@ -83,11 +80,11 @@ public function testItCanBeStopped(): void
8380

8481
public function testItStopsPropagationWhenStopped(): void
8582
{
86-
$this->event->stop('error_message', ResourceControllerEvent::TYPE_SUCCESS, ['parameter']);
83+
$this->event->stop('error_message', GenericEvent::TYPE_SUCCESS, ['parameter']);
8784

8885
$this->assertTrue($this->event->isPropagationStopped());
8986
$this->assertSame('error_message', $this->event->getMessage());
90-
$this->assertSame(ResourceControllerEvent::TYPE_SUCCESS, $this->event->getMessageType());
87+
$this->assertSame(GenericEvent::TYPE_SUCCESS, $this->event->getMessageType());
9188
$this->assertSame(['parameter'], $this->event->getMessageParameters());
9289
}
9390

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Bundle\EventListener;
15+
16+
use App\Entity\Book;
17+
use App\Entity\User;
18+
use Doctrine\ORM\EntityManagerInterface;
19+
use Doctrine\Persistence\Mapping\ClassMetadata;
20+
use Doctrine\Persistence\Mapping\ReflectionService;
21+
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
22+
use Sylius\Bundle\ResourceBundle\EventListener\AbstractDoctrineListener;
23+
use Sylius\Resource\Metadata\RegistryInterface;
24+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
25+
26+
final class AbstractDoctrineListenerTest extends KernelTestCase
27+
{
28+
private EntityManagerInterface $entityManager;
29+
30+
private AbstractDoctrineListener $listener;
31+
32+
protected function setUp(): void
33+
{
34+
self::bootKernel();
35+
$this->entityManager = $this->getContainer()->get(EntityManagerInterface::class);
36+
37+
$resourceRegistry = $this->getContainer()->get(RegistryInterface::class);
38+
$this->listener = new class($resourceRegistry) extends AbstractDoctrineListener {
39+
public function publicIsResource(ClassMetadata $metadata): bool
40+
{
41+
return $this->isResource($metadata);
42+
}
43+
44+
public function publicGetReflectionService(): \Doctrine\Persistence\Mapping\ReflectionService
45+
{
46+
return $this->getReflectionService();
47+
}
48+
};
49+
}
50+
51+
public function testIsResourceReturnsTrueForResourceInterface(): void
52+
{
53+
$metadata = $this->getClassMetadata(Book::class);
54+
55+
$result = $this->listener->publicIsResource($metadata);
56+
57+
$this->assertTrue($result);
58+
}
59+
60+
public function testGetReflectionServiceReturnsRuntimeReflectionService(): void
61+
{
62+
$reflectionService = $this->listener->publicGetReflectionService();
63+
64+
$this->assertInstanceOf(RuntimeReflectionService::class, $reflectionService);
65+
}
66+
67+
public function testGetReflectionServiceReturnsSameInstanceOnMultipleCalls(): void
68+
{
69+
$firstCall = $this->listener->publicGetReflectionService();
70+
$secondCall = $this->listener->publicGetReflectionService();
71+
72+
$this->assertSame($firstCall, $secondCall);
73+
}
74+
75+
public function testConstructorAcceptsResourceRegistry(): void
76+
{
77+
$resourceRegistry = $this->getContainer()->get(RegistryInterface::class);
78+
$listener = new class($resourceRegistry) extends AbstractDoctrineListener {
79+
};
80+
81+
$this->assertInstanceOf(AbstractDoctrineListener::class, $listener);
82+
}
83+
84+
public function testIsResourceWorksWithDifferentEntities(): void
85+
{
86+
$bookMetadata = $this->getClassMetadata(Book::class);
87+
$userMetadata = $this->getClassMetadata(User::class);
88+
89+
$this->assertTrue($this->listener->publicIsResource($bookMetadata));
90+
$this->assertTrue($this->listener->publicIsResource($userMetadata));
91+
}
92+
93+
public function testReflectionServiceImplementsCorrectInterface(): void
94+
{
95+
$reflectionService = $this->listener->publicGetReflectionService();
96+
97+
$this->assertInstanceOf(ReflectionService::class, $reflectionService);
98+
}
99+
100+
public function testReflectionServiceIsLazilyInitialized(): void
101+
{
102+
$resourceRegistry = $this->getContainer()->get(RegistryInterface::class);
103+
$newListener = new class($resourceRegistry) extends AbstractDoctrineListener {
104+
public function hasReflectionService(): bool
105+
{
106+
return $this->getReflectionService() instanceof RuntimeReflectionService;
107+
}
108+
};
109+
110+
$this->assertTrue($newListener->hasReflectionService());
111+
}
112+
113+
private function getClassMetadata(string $className): ClassMetadata
114+
{
115+
return $this->entityManager->getClassMetadata($className);
116+
}
117+
}

src/Bundle/spec/EventListener/ODMRepositoryClassSubscriberSpec.php renamed to tests/Bundle/EventListener/ODMRepositoryClassSubscriberTest.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,29 @@
1414
namespace Sylius\Bundle\ResourceBundle\Tests\Bundle\EventListener;
1515

1616
use Doctrine\Common\EventSubscriber;
17+
use Doctrine\ODM\MongoDB\DocumentManager;
1718
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
1819
use Doctrine\ODM\MongoDB\Events;
1920
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
21+
use PHPUnit\Framework\MockObject\MockObject;
2022
use PHPUnit\Framework\TestCase;
2123
use Sylius\Bundle\ResourceBundle\EventListener\ODMRepositoryClassSubscriber;
2224
use Sylius\Resource\Metadata\MetadataInterface;
2325
use Sylius\Resource\Metadata\RegistryInterface;
2426

25-
/**
26-
* @requires extension mongodb
27-
*/
2827
final class ODMRepositoryClassSubscriberTest extends TestCase
2928
{
29+
/** @var RegistryInterface&MockObject */
3030
private RegistryInterface $registry;
3131

3232
private ODMRepositoryClassSubscriber $subscriber;
3333

3434
protected function setUp(): void
3535
{
36+
if (!class_exists(Events::class)) {
37+
$this->markTestSkipped('Doctrine MongoDB ODM is not installed.');
38+
}
39+
3640
$this->registry = $this->createMock(RegistryInterface::class);
3741
$this->subscriber = new ODMRepositoryClassSubscriber($this->registry);
3842
}
@@ -77,11 +81,8 @@ public function testItSetsCustomRepositoryClassWhenConfigured(): void
7781
->method('setCustomRepositoryClass')
7882
->with('FooRepository');
7983

80-
$event = $this->createMock(LoadClassMetadataEventArgs::class);
81-
$event
82-
->expects($this->once())
83-
->method('getClassMetadata')
84-
->willReturn($classMetadata);
84+
$documentManager = $this->createMock(DocumentManager::class);
85+
$event = new LoadClassMetadataEventArgs($classMetadata, $documentManager);
8586

8687
$this->subscriber->loadClassMetadata($event);
8788
}
@@ -110,11 +111,8 @@ public function testItDoesNotSetCustomRepositoryClassWhenNotConfigured(): void
110111
->expects($this->never())
111112
->method('setCustomRepositoryClass');
112113

113-
$event = $this->createMock(LoadClassMetadataEventArgs::class);
114-
$event
115-
->expects($this->once())
116-
->method('getClassMetadata')
117-
->willReturn($classMetadata);
114+
$documentManager = $this->createMock(DocumentManager::class);
115+
$event = new LoadClassMetadataEventArgs($classMetadata, $documentManager);
118116

119117
$this->subscriber->loadClassMetadata($event);
120118
}
@@ -136,11 +134,8 @@ public function testItDoesNotSetCustomRepositoryClassWhenClassNotInRegistry(): v
136134
->expects($this->never())
137135
->method('setCustomRepositoryClass');
138136

139-
$event = $this->createMock(LoadClassMetadataEventArgs::class);
140-
$event
141-
->expects($this->once())
142-
->method('getClassMetadata')
143-
->willReturn($classMetadata);
137+
$documentManager = $this->createMock(DocumentManager::class);
138+
$event = new LoadClassMetadataEventArgs($classMetadata, $documentManager);
144139

145140
$this->subscriber->loadClassMetadata($event);
146141
}

src/Bundle/spec/EventListener/ORMRepositoryClassSubscriberSpec.php renamed to tests/Bundle/EventListener/ORMRepositoryClassSubscriberTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
final class ORMRepositoryClassSubscriberTest extends TestCase
2626
{
27+
/** @var RegistryInterface&\PHPUnit\Framework\MockObject\MockObject */
2728
private RegistryInterface $registry;
2829

2930
private ORMRepositoryClassSubscriber $subscriber;

src/Bundle/spec/Storage/CookieStorageSpec.php renamed to tests/Bundle/Storage/CookieStorageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public function testItReturnsAllStoredValues(): void
6666
$this->storage->set('buzz', 'lightyear');
6767

6868
$this->assertSame([
69-
'buzz' => 'lightyear',
7069
'foo' => 'bar',
70+
'buzz' => 'lightyear',
7171
], $this->storage->all());
7272
}
7373
}

src/Bundle/spec/Storage/SessionStorageSpec.php renamed to tests/Bundle/Storage/SessionStorageTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public function testItThrowsStorageUnavailableExceptionWhenSessionNotAvailable(c
6767
$method($storage);
6868
}
6969

70+
/**
71+
* @return iterable<string, array{callable(SessionStorage): mixed}>
72+
*/
7073
public static function storageMethodsDataProvider(): iterable
7174
{
7275
yield 'has' => [fn (SessionStorage $storage) => $storage->has('name')];
@@ -111,8 +114,8 @@ public function testItReturnsAllStoredValues(): void
111114
$this->storage->set('buzz', 'lightyear');
112115

113116
$this->assertSame([
114-
'buzz' => 'lightyear',
115117
'foo' => 'bar',
118+
'buzz' => 'lightyear',
116119
], $this->storage->all());
117120
}
118121
}

src/Bundle/spec/Twig/Context/LegacyContextFactorySpec.php renamed to tests/Bundle/Twig/Context/LegacyContextFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
final class LegacyContextFactoryTest extends TestCase
2727
{
28+
/** @var ContextFactoryInterface&\PHPUnit\Framework\MockObject\MockObject */
2829
private ContextFactoryInterface $decorated;
2930

3031
private LegacyContextFactory $factory;
@@ -61,9 +62,9 @@ public function testItAddsLegacyTwigVariablesWhenContextContainsOptions(): void
6162
$result = $this->factory->create($data, $operation, $context);
6263

6364
$this->assertSame([
65+
'resource' => $data,
6466
'configuration' => $requestConfiguration,
6567
'metadata' => $metadata,
66-
'resource' => $data,
6768
], $result);
6869
}
6970

0 commit comments

Comments
 (0)