|
2 | 2 |
|
3 | 3 | use Cleantalk\Antispam\Cleantalk; |
4 | 4 | use Cleantalk\Antispam\CleantalkRequest; |
| 5 | +use Cleantalk\Antispam\CleantalkResponse; |
5 | 6 | use Cleantalk\ApbctWP\State; |
6 | 7 |
|
| 8 | + |
7 | 9 | class CleantalkTest extends \PHPUnit\Framework\TestCase |
8 | 10 | { |
9 | | - protected $ct; |
10 | | - |
11 | | - protected $ct_request; |
| 11 | + protected $ct; |
| 12 | + protected $ct_request; |
12 | 13 |
|
13 | | - public function setUp() |
14 | | - { |
| 14 | + public function setUp() |
| 15 | + { |
15 | 16 | global $apbct; |
16 | | - $apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') ); |
17 | | - |
18 | | - $this->ct = new Cleantalk(); |
19 | | - $this->ct->server_url = APBCT_MODERATE_URL; |
20 | | - $this->ct_request = new CleantalkRequest(); |
21 | | - $this->ct_request->auth_key = getenv("CLEANTALK_TEST_API_KEY"); |
22 | | - } |
23 | | - |
24 | | - public function testIsAllowMessage() |
25 | | - { |
26 | | - $this->ct_request->sender_email = 's@cleantalk.org'; |
27 | | - $this->ct_request->message = 'stop_word bad message'; |
28 | | - $result = $this->ct->isAllowMessage($this->ct_request); |
29 | | - $this->assertEquals(0, $result->allow); |
30 | | - |
31 | | - $this->ct_request->message = ''; |
32 | | - $this->ct_request->sender_email = ''; |
33 | | - } |
34 | | - |
35 | | - public function testIsAllowUser() |
36 | | - { |
37 | | - $this->ct_request->sender_email = 's@cleantalk.org'; |
38 | | - $result = $this->ct->isAllowUser($this->ct_request); |
39 | | - $this->assertEquals(0, $result->allow); |
40 | | - |
41 | | - $this->ct_request->sender_email = ''; |
42 | | - } |
| 17 | + $apbct = new State('cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats')); |
| 18 | + |
| 19 | + $this->ct_request = new CleantalkRequest(); |
| 20 | + $this->ct_request->auth_key = 'mockKeyAny'; |
| 21 | + } |
| 22 | + |
| 23 | + public function testIsAllowMessageWithMock() |
| 24 | + { |
| 25 | + // Создаем мок объекта Cleantalk |
| 26 | + $ctMock = $this->getMockBuilder(Cleantalk::class) |
| 27 | + ->setMethods(['httpRequest']) // Мокаем только метод httpRequest |
| 28 | + ->getMock(); |
| 29 | + |
| 30 | + // Настраиваем ожидаемый ответ |
| 31 | + $expectedResponse = new \stdClass(); |
| 32 | + $expectedResponse->allow = 0; |
| 33 | + $expectedResponse->comment = "Test comment"; |
| 34 | + $expectedResponse->errno = 0; |
| 35 | + $expectedResponse->errstr = ""; |
| 36 | + |
| 37 | + // Ожидаем, что httpRequest будет вызван 1 раз с любыми параметрами |
| 38 | + // и вернет наш фиктивный ответ |
| 39 | + $ctMock->expects($this->once()) |
| 40 | + ->method('httpRequest') |
| 41 | + ->willReturn(new CleantalkResponse($expectedResponse, null)); |
| 42 | + |
| 43 | + // Устанавливаем свойства для теста |
| 44 | + $ctMock->server_url = 'https://example.com'; |
| 45 | + |
| 46 | + // Выполняем тест |
| 47 | + $this->ct_request->sender_email = 's@cleantalk.org'; |
| 48 | + $this->ct_request->message = 'stop_word bad message'; |
| 49 | + $result = $ctMock->isAllowMessage($this->ct_request); |
| 50 | + |
| 51 | + // Проверяем результат |
| 52 | + $this->assertEquals(0, $result->allow); |
| 53 | + } |
| 54 | + |
| 55 | + // Альтернативный вариант с более полным контролем |
| 56 | + public function testIsAllowMessageWithFullControl() |
| 57 | + { |
| 58 | + // Создаем мок с полным контролем над процессом |
| 59 | + $ctMock = $this->getMockBuilder(Cleantalk::class) |
| 60 | + ->setMethods(['createMsg', 'httpRequest']) |
| 61 | + ->getMock(); |
| 62 | + |
| 63 | + // Мокаем createMsg чтобы он возвращал ожидаемый запрос |
| 64 | + $expectedRequest = new CleantalkRequest(); |
| 65 | + $expectedRequest->auth_key = getenv("CLEANTALK_TEST_API_KEY"); |
| 66 | + $expectedRequest->sender_email = 's@cleantalk.org'; |
| 67 | + $expectedRequest->message = 'stop_word bad message'; |
| 68 | + $expectedRequest->method_name = 'check_message'; |
| 69 | + // ... установите другие необходимые свойства ... |
| 70 | + |
| 71 | + $ctMock->expects($this->once()) |
| 72 | + ->method('createMsg') |
| 73 | + ->with('check_message', $this->isInstanceOf(CleantalkRequest::class)) |
| 74 | + ->willReturn($expectedRequest); |
| 75 | + |
| 76 | + // Мокаем httpRequest для возврата фиктивного ответа |
| 77 | + $mockResponse = new \stdClass(); |
| 78 | + $mockResponse->allow = 0; |
| 79 | + $mockResponse->comment = "Forbidden"; |
| 80 | + $mockResponse->errno = 0; |
| 81 | + $mockResponse->errstr = ""; |
| 82 | + |
| 83 | + $ctMock->expects($this->once()) |
| 84 | + ->method('httpRequest') |
| 85 | + ->with($expectedRequest) |
| 86 | + ->willReturn(new CleantalkResponse($mockResponse, null)); |
| 87 | + |
| 88 | + // Выполняем тест |
| 89 | + $this->ct_request->sender_email = 's@cleantalk.org'; |
| 90 | + $this->ct_request->message = 'stop_word bad message'; |
| 91 | + $result = $ctMock->isAllowMessage($this->ct_request); |
| 92 | + |
| 93 | + $this->assertEquals(0, $result->allow); |
| 94 | + $this->assertEquals("Forbidden", $result->comment); |
| 95 | + } |
| 96 | + |
| 97 | + public function testIsAllowMessageAllow() |
| 98 | + { |
| 99 | + // Тест для разрешенного сообщения |
| 100 | + $ctMock = $this->getMockBuilder(Cleantalk::class) |
| 101 | + ->setMethods(['httpRequest']) |
| 102 | + ->getMock(); |
| 103 | + |
| 104 | + $expectedResponse = new \stdClass(); |
| 105 | + $expectedResponse->allow = 1; // Сообщение разрешено |
| 106 | + $expectedResponse->comment = ""; |
| 107 | + $expectedResponse->errno = 0; |
| 108 | + $expectedResponse->errstr = ""; |
| 109 | + |
| 110 | + $ctMock->expects($this->once()) |
| 111 | + ->method('httpRequest') |
| 112 | + ->willReturn(new CleantalkResponse($expectedResponse, null)); |
| 113 | + |
| 114 | + $ctMock->server_url = 'https://example.com'; |
| 115 | + |
| 116 | + $this->ct_request->sender_email = 'good@example.com'; |
| 117 | + $this->ct_request->message = 'Normal message'; |
| 118 | + $result = $ctMock->isAllowMessage($this->ct_request); |
| 119 | + |
| 120 | + $this->assertEquals(1, $result->allow); |
| 121 | + $this->assertEquals("", $result->comment); |
| 122 | + } |
43 | 123 | } |
0 commit comments