|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Smartling\Tests\Services; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Smartling\Services\BlogRemovalHandler; |
| 7 | +use Smartling\Settings\ConfigurationProfileEntity; |
| 8 | +use Smartling\Settings\TargetLocale; |
| 9 | +use Smartling\Submissions\SubmissionEntity; |
| 10 | +use Smartling\Vendor\Smartling\AuditLog\Params\CreateRecordParameters; |
| 11 | +use Smartling\ApiWrapperInterface; |
| 12 | +use Smartling\Settings\SettingsManager; |
| 13 | +use Smartling\Submissions\SubmissionManager; |
| 14 | + |
| 15 | +class BlogRemovalHandlerTest extends TestCase |
| 16 | +{ |
| 17 | + public function testBlogRemovalHandler() |
| 18 | + { |
| 19 | + $locales = []; |
| 20 | + for ($blogId = 1; $blogId < 4; $blogId++) { |
| 21 | + $locale = $this->createMock(TargetLocale::class); |
| 22 | + $locale->method('getBlogId')->willReturn($blogId); |
| 23 | + $locales[] = $locale; |
| 24 | + } |
| 25 | + |
| 26 | + $profiles = []; |
| 27 | + foreach ([1, 3] as $blogId) { |
| 28 | + $profile = $this->createMock(ConfigurationProfileEntity::class); |
| 29 | + $profile->method('getSourceLocale')->willReturn(array_values(array_filter($locales, static function (TargetLocale $locale) use ($blogId) { |
| 30 | + return $locale->getBlogId() === $blogId; |
| 31 | + }))[0]); |
| 32 | + $profile->method('getTargetLocales')->willReturn(array_values(array_filter($locales, static function (TargetLocale $locale) use ($blogId) { |
| 33 | + return $locale->getBlogId() !== $blogId; |
| 34 | + }))); |
| 35 | + $profiles[] = $profile; |
| 36 | + } |
| 37 | + |
| 38 | + $settingsManager = $this->createMock(SettingsManager::class); |
| 39 | + $settingsManager->method('getEntities')->willReturn($profiles); |
| 40 | + |
| 41 | + $submissions = []; |
| 42 | + foreach (['12', '13', '31', '32'] as $blogs) { |
| 43 | + $submission = $this->createMock(SubmissionEntity::class); |
| 44 | + $submission->method('getFileUri')->willReturn($blogs); |
| 45 | + $submission->method('getId')->willReturn((int)$blogs); |
| 46 | + $submission->method('getSourceBlogId')->willReturn((int)$blogs[0]); |
| 47 | + $submission->method('getTargetBlogId')->willReturn((int)$blogs[1]); |
| 48 | + $submissions[] = $submission; |
| 49 | + } |
| 50 | + |
| 51 | + $submissionManager = $this->createMock(SubmissionManager::class); |
| 52 | + $submissionManager->method('find')->willReturnCallback(function (array $arguments) use ($submissions) { |
| 53 | + if (array_key_exists(SubmissionEntity::FIELD_TARGET_BLOG_ID, $arguments)) { |
| 54 | + return array_filter($submissions, static function (SubmissionEntity $submission) use ($arguments) { |
| 55 | + return $submission->getTargetBlogId() === $arguments[SubmissionEntity::FIELD_TARGET_BLOG_ID]; |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return []; |
| 60 | + }); |
| 61 | + $submissionManager->expects($this->exactly(2))->method('delete'); |
| 62 | + |
| 63 | + $apiWrapper = $this->createMock(ApiWrapperInterface::class); |
| 64 | + $matcher = $this->exactly(2); |
| 65 | + $apiWrapper->expects($matcher) |
| 66 | + ->method('createAuditLogRecord') |
| 67 | + ->willReturnCallback(function (ConfigurationProfileEntity $profile, string $actionType, string $description) use ($matcher, $profiles) { |
| 68 | + $this->assertEquals($profiles[$matcher->getInvocationCount() - 1], $profile); |
| 69 | + $this->assertEquals(CreateRecordParameters::ACTION_TYPE_DELETE, $actionType); |
| 70 | + switch ($matcher->getInvocationCount()) { |
| 71 | + case 1: |
| 72 | + $this->assertEquals('Blog deletion handler, submissionId=12, fileUri=12', $description); |
| 73 | + break; |
| 74 | + case 2: |
| 75 | + $this->assertEquals('Blog deletion handler, submissionId=32, fileUri=32', $description); |
| 76 | + break; |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + (new BlogRemovalHandler($apiWrapper, $settingsManager, $submissionManager))->blogRemovalHandler(2); |
| 81 | + } |
| 82 | +} |
0 commit comments