Skip to content

Commit fc706e3

Browse files
author
Bizley
committed
MSI
1 parent 69511cd commit fc706e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+635
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
composer.lock
44
.php_cs.cache
55
.phpunit.result.cache
6+
infection.log

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ There are some rules:
1616

1717
TODOs:
1818
- [ ] Infection
19-
- [ ] Clean messages
2019

2120
When API is ready, I'll start preparing the client.

infection.json renamed to infection.json.dist

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
]
66
},
77
"logs": {
8-
"text": "tests\/infection.log"
8+
"text": "infection.log"
99
},
1010
"mutators": {
1111
"@default": true
1212
},
13-
"minMsi": 56
13+
"minMsi": 95,
14+
"minCoveredMsi": 100
1415
}

src/Module.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ public function __construct(array $config = [])
8888
*/
8989
public function init(): void
9090
{
91-
parent::init();
92-
9391
$this->prepareTranslations();
9492
$this->completeComponents();
9593
}
@@ -236,7 +234,7 @@ public function getThread()
236234
return $this->get('thread');
237235
}
238236

239-
public function prepareTranslations(): void
237+
private function prepareTranslations(): void
240238
{
241239
Yii::$app->getI18n()->translations['podium.error'] = [
242240
'class' => PhpMessageSource::class,
@@ -251,7 +249,7 @@ public function prepareTranslations(): void
251249
*
252250
* @throws InvalidConfigException
253251
*/
254-
public function completeComponents(): void
252+
private function completeComponents(): void
255253
{
256254
$components = $this->getComponents();
257255

src/Services/Post/PostBuilder.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ public function edit(RepositoryInterface $post, array $data = []): PodiumRespons
122122
throw new ServiceException($post->getErrors());
123123
}
124124

125-
$this->afterEdit($post);
126-
127-
return PodiumResponse::success();
125+
$transaction->commit();
128126
} catch (ServiceException $exc) {
129127
$transaction->rollBack();
130128

@@ -135,6 +133,10 @@ public function edit(RepositoryInterface $post, array $data = []): PodiumRespons
135133

136134
return PodiumResponse::error(['exception' => $exc]);
137135
}
136+
137+
$this->afterEdit($post);
138+
139+
return PodiumResponse::success();
138140
}
139141

140142
/**

tests/AppTestCase.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class AppTestCase extends TestCase
1515
{
16+
public Transaction $transaction;
17+
1618
public static function setUpBeforeClass(): void
1719
{
1820
new Application(
@@ -40,8 +42,9 @@ public static function tearDownAfterClass(): void
4042

4143
protected function setUp(): void
4244
{
45+
$this->transaction = $this->createMock(Transaction::class);
4346
$connection = $this->createMock(Connection::class);
44-
$connection->method('beginTransaction')->willReturn($this->createMock(Transaction::class));
47+
$connection->method('beginTransaction')->willReturn($this->transaction);
4548
Yii::$app->set('db', $connection);
4649
}
4750
}

tests/Unit/Account/AccountComponentTest.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,25 @@ public function testGetMembershipShouldReturnLoadedMemberRepository(): void
6161
$this->component->userConfig = $user;
6262

6363
$member = $this->createMock(MemberRepositoryInterface::class);
64-
$member->expects(self::once())->method('fetchOne')->willReturn(true);
64+
$member->expects(self::once())->method('fetchOne')->with(['user_id' => '1'])->willReturn(true);
6565
$this->component->repositoryConfig = $member;
6666

6767
$this->component->getMembership();
68+
$this->component->getMembership(); // second one to test internal cache
69+
}
70+
71+
public function testGetMembershipShouldReloadMemberIfRequested(): void
72+
{
73+
$user = $this->createMock(User::class);
74+
$user->expects(self::exactly(2))->method('getId')->willReturn(1);
75+
$this->component->userConfig = $user;
76+
77+
$member = $this->createMock(MemberRepositoryInterface::class);
78+
$member->expects(self::exactly(2))->method('fetchOne')->willReturn(true);
79+
$this->component->repositoryConfig = $member;
80+
81+
$this->component->getMembership();
82+
$this->component->getMembership(true);
6883
}
6984

7085
public function testGetMembershipShouldThrowExceptionWhenUserComponentIsMisconfigured(): void

tests/Unit/Category/CategoryArchiverTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function testArchiveShouldReturnErrorWhenRepositoryIsWrong(): void
3535

3636
public function testArchiveShouldReturnErrorWhenArchivingErrored(): void
3737
{
38+
$this->transaction->expects(self::once())->method('rollBack');
39+
3840
$category = $this->createMock(CategoryRepositoryInterface::class);
3941
$category->method('isArchived')->willReturn(false);
4042
$category->method('archive')->willReturn(false);
@@ -47,6 +49,8 @@ public function testArchiveShouldReturnErrorWhenArchivingErrored(): void
4749

4850
public function testArchiveShouldReturnErrorWhenCategoryIsAlreadyArchived(): void
4951
{
52+
$this->transaction->expects(self::once())->method('rollBack');
53+
5054
$category = $this->createMock(CategoryRepositoryInterface::class);
5155
$category->method('isArchived')->willReturn(true);
5256
$result = $this->service->archive($category);
@@ -57,6 +61,8 @@ public function testArchiveShouldReturnErrorWhenCategoryIsAlreadyArchived(): voi
5761

5862
public function testArchiveShouldReturnSuccessWhenArchivingIsDone(): void
5963
{
64+
$this->transaction->expects(self::once())->method('commit');
65+
6066
$category = $this->createMock(CategoryRepositoryInterface::class);
6167
$category->method('isArchived')->willReturn(false);
6268
$category->method('archive')->willReturn(true);
@@ -67,6 +73,8 @@ public function testArchiveShouldReturnSuccessWhenArchivingIsDone(): void
6773

6874
public function testArchiveShouldReturnErrorWhenArchivingThrowsException(): void
6975
{
76+
$this->transaction->expects(self::once())->method('rollBack');
77+
7078
$category = $this->createMock(CategoryRepositoryInterface::class);
7179
$category->method('isArchived')->willReturn(false);
7280
$category->method('archive')->willThrowException(new Exception('exc'));
@@ -91,6 +99,8 @@ public function testReviveShouldReturnErrorWhenRepositoryIsWrong(): void
9199

92100
public function testReviveShouldReturnErrorWhenRevivingErrored(): void
93101
{
102+
$this->transaction->expects(self::once())->method('rollBack');
103+
94104
$category = $this->createMock(CategoryRepositoryInterface::class);
95105
$category->method('isArchived')->willReturn(true);
96106
$category->method('revive')->willReturn(false);
@@ -103,6 +113,8 @@ public function testReviveShouldReturnErrorWhenRevivingErrored(): void
103113

104114
public function testReviveShouldReturnErrorWhenCategoryIsNotArchived(): void
105115
{
116+
$this->transaction->expects(self::once())->method('rollBack');
117+
106118
$category = $this->createMock(CategoryRepositoryInterface::class);
107119
$category->method('isArchived')->willReturn(false);
108120
$result = $this->service->revive($category);
@@ -113,6 +125,8 @@ public function testReviveShouldReturnErrorWhenCategoryIsNotArchived(): void
113125

114126
public function testReviveShouldReturnSuccessWhenRevivingIsDone(): void
115127
{
128+
$this->transaction->expects(self::once())->method('commit');
129+
116130
$category = $this->createMock(CategoryRepositoryInterface::class);
117131
$category->method('isArchived')->willReturn(true);
118132
$category->method('revive')->willReturn(true);
@@ -123,6 +137,8 @@ public function testReviveShouldReturnSuccessWhenRevivingIsDone(): void
123137

124138
public function testReviveShouldReturnErrorWhenRevivingThrowsException(): void
125139
{
140+
$this->transaction->expects(self::once())->method('rollBack');
141+
126142
$category = $this->createMock(CategoryRepositoryInterface::class);
127143
$category->method('isArchived')->willReturn(true);
128144
$category->method('revive')->willThrowException(new Exception('exc'));

tests/Unit/Category/CategoryBuilderTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public function testBeforeCreateShouldReturnTrue(): void
2727

2828
public function testCreateShouldReturnErrorWhenCreatingErrored(): void
2929
{
30+
$this->transaction->expects(self::once())->method('rollBack');
31+
3032
$category = $this->createMock(CategoryRepositoryInterface::class);
3133
$category->method('create')->willReturn(false);
3234
$category->method('getErrors')->willReturn([1]);
@@ -38,6 +40,8 @@ public function testCreateShouldReturnErrorWhenCreatingErrored(): void
3840

3941
public function testCreateShouldReturnSuccessWhenCreatingIsDone(): void
4042
{
43+
$this->transaction->expects(self::once())->method('commit');
44+
4145
$category = $this->createMock(CategoryRepositoryInterface::class);
4246
$category->method('create')->willReturn(true);
4347
$result = $this->service->create($category, $this->createMock(MemberRepositoryInterface::class));
@@ -47,6 +51,8 @@ public function testCreateShouldReturnSuccessWhenCreatingIsDone(): void
4751

4852
public function testCreateShouldReturnErrorWhenCreatingThrowsException(): void
4953
{
54+
$this->transaction->expects(self::once())->method('rollBack');
55+
5056
$category = $this->createMock(CategoryRepositoryInterface::class);
5157
$category->method('create')->willThrowException(new Exception('exc'));
5258
$result = $this->service->create($category, $this->createMock(MemberRepositoryInterface::class));
@@ -62,6 +68,8 @@ public function testBeforeEditShouldReturnTrue(): void
6268

6369
public function testEditShouldReturnErrorWhenEditingErrored(): void
6470
{
71+
$this->transaction->expects(self::once())->method('rollBack');
72+
6573
$category = $this->createMock(CategoryRepositoryInterface::class);
6674
$category->method('edit')->willReturn(false);
6775
$category->method('getErrors')->willReturn([1]);
@@ -73,6 +81,8 @@ public function testEditShouldReturnErrorWhenEditingErrored(): void
7381

7482
public function testEditShouldReturnSuccessWhenEditingIsDone(): void
7583
{
84+
$this->transaction->expects(self::once())->method('commit');
85+
7686
$category = $this->createMock(CategoryRepositoryInterface::class);
7787
$category->method('edit')->willReturn(true);
7888
$result = $this->service->edit($category);
@@ -82,6 +92,8 @@ public function testEditShouldReturnSuccessWhenEditingIsDone(): void
8292

8393
public function testEditShouldReturnErrorWhenEditingThrowsException(): void
8494
{
95+
$this->transaction->expects(self::once())->method('rollBack');
96+
8597
$category = $this->createMock(CategoryRepositoryInterface::class);
8698
$category->method('edit')->willThrowException(new Exception('exc'));
8799
$result = $this->service->edit($category);

tests/Unit/Category/CategoryRemoverTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void
3535

3636
public function testRemoveShouldReturnErrorWhenRemovingErrored(): void
3737
{
38+
$this->transaction->expects(self::once())->method('rollBack');
39+
3840
$category = $this->createMock(CategoryRepositoryInterface::class);
3941
$category->method('isArchived')->willReturn(true);
4042
$category->method('delete')->willReturn(false);
@@ -46,6 +48,8 @@ public function testRemoveShouldReturnErrorWhenRemovingErrored(): void
4648

4749
public function testRemoveShouldReturnErrorWhenCategoryIsNotArchived(): void
4850
{
51+
$this->transaction->expects(self::once())->method('rollBack');
52+
4953
$category = $this->createMock(CategoryRepositoryInterface::class);
5054
$category->method('isArchived')->willReturn(false);
5155
$category->method('delete')->willReturn(true);
@@ -57,6 +61,8 @@ public function testRemoveShouldReturnErrorWhenCategoryIsNotArchived(): void
5761

5862
public function testRemoveShouldReturnSuccessWhenRemovingIsDone(): void
5963
{
64+
$this->transaction->expects(self::once())->method('commit');
65+
6066
$category = $this->createMock(CategoryRepositoryInterface::class);
6167
$category->method('isArchived')->willReturn(true);
6268
$category->method('delete')->willReturn(true);
@@ -67,6 +73,8 @@ public function testRemoveShouldReturnSuccessWhenRemovingIsDone(): void
6773

6874
public function testRemoveShouldReturnErrorWhenRemovingThrowsException(): void
6975
{
76+
$this->transaction->expects(self::once())->method('rollBack');
77+
7078
$category = $this->createMock(CategoryRepositoryInterface::class);
7179
$category->method('isArchived')->willReturn(true);
7280
$category->method('delete')->willThrowException(new Exception('exc'));
@@ -78,6 +86,8 @@ public function testRemoveShouldReturnErrorWhenRemovingThrowsException(): void
7886

7987
public function testRemoveShouldReturnErrorWhenIsArchivedThrowsException(): void
8088
{
89+
$this->transaction->expects(self::once())->method('rollBack');
90+
8191
$category = $this->createMock(CategoryRepositoryInterface::class);
8292
$category->method('isArchived')->willThrowException(new Exception('exc'));
8393
$result = $this->service->remove($category);

tests/Unit/Category/CategorySorterTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function testReplaceShouldReturnErrorWhenSecondRepositoryIsWrong(): void
4949

5050
public function testReplaceShouldReturnErrorWhenSettingFirstOrderErrored(): void
5151
{
52+
$this->transaction->expects(self::once())->method('rollBack');
53+
5254
$category = $this->createMock(CategoryRepositoryInterface::class);
5355
$category->method('getOrder')->willReturn(1);
5456
$category->method('setOrder')->willReturn(false);
@@ -59,6 +61,8 @@ public function testReplaceShouldReturnErrorWhenSettingFirstOrderErrored(): void
5961

6062
public function testReplaceShouldReturnErrorWhenSettingSecondOrderErrored(): void
6163
{
64+
$this->transaction->expects(self::once())->method('rollBack');
65+
6266
$category1 = $this->createMock(CategoryRepositoryInterface::class);
6367
$category1->method('getOrder')->willReturn(1);
6468
$category1->method('setOrder')->willReturn(true);
@@ -72,6 +76,8 @@ public function testReplaceShouldReturnErrorWhenSettingSecondOrderErrored(): voi
7276

7377
public function testReplaceShouldReturnSuccessWhenReplacingIsDone(): void
7478
{
79+
$this->transaction->expects(self::once())->method('commit');
80+
7581
$category = $this->createMock(CategoryRepositoryInterface::class);
7682
$category->method('getOrder')->willReturn(1);
7783
$category->method('setOrder')->willReturn(true);
@@ -82,6 +88,8 @@ public function testReplaceShouldReturnSuccessWhenReplacingIsDone(): void
8288

8389
public function testReplaceShouldReturnErrorWhenReplacingThrowsException(): void
8490
{
91+
$this->transaction->expects(self::once())->method('rollBack');
92+
8593
$category = $this->createMock(CategoryRepositoryInterface::class);
8694
$category->method('getOrder')->willReturn(1);
8795
$category->method('setOrder')->willThrowException(new Exception('exc'));
@@ -98,6 +106,8 @@ public function testBeforeSortShouldReturnTrue(): void
98106

99107
public function testSortShouldReturnErrorWhenSortingErrored(): void
100108
{
109+
$this->transaction->expects(self::once())->method('rollBack');
110+
101111
$category = $this->createMock(CategoryRepositoryInterface::class);
102112
$category->method('sort')->willReturn(false);
103113
$result = $this->service->sort($category);
@@ -107,6 +117,8 @@ public function testSortShouldReturnErrorWhenSortingErrored(): void
107117

108118
public function testSortShouldReturnSuccessWhenSortingIsDone(): void
109119
{
120+
$this->transaction->expects(self::once())->method('commit');
121+
110122
$category = $this->createMock(CategoryRepositoryInterface::class);
111123
$category->method('sort')->willReturn(true);
112124
$result = $this->service->sort($category);
@@ -116,6 +128,8 @@ public function testSortShouldReturnSuccessWhenSortingIsDone(): void
116128

117129
public function testSortShouldReturnErrorWhenSortingThrowsException(): void
118130
{
131+
$this->transaction->expects(self::once())->method('rollBack');
132+
119133
$category = $this->createMock(CategoryRepositoryInterface::class);
120134
$category->method('sort')->willThrowException(new Exception('exc'));
121135
$result = $this->service->sort($category);

0 commit comments

Comments
 (0)