1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sleepness\UberTranslationAdminBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Testing TranslationController actions |
9
|
|
|
* |
10
|
|
|
* @author Viktor Novikov <[email protected]> |
11
|
|
|
* @author Alexandr Zhulev <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class TranslationControllerTest extends WebTestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Sleepness\UberTranslationBundle\Storage\UberMemcached; |
17
|
|
|
*/ |
18
|
|
|
private $uberMemcached; |
19
|
|
|
|
20
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Test indexAction() of TranslationController |
24
|
|
|
*/ |
25
|
|
|
public function testIndexAction() |
26
|
|
|
{ |
27
|
|
|
$crawler = $this->client->request('GET', '/translations'); |
28
|
|
|
$this->assertEquals(5, $crawler->filter('th')->count()); |
29
|
|
|
$this->responseAsserts($this->client->getResponse()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Test editAction() of TranslationController |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function testEditAction() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$crawler = $this->client->request('POST', '/translation/edit/en_US/messages/test.key'); |
38
|
|
|
$this->assertEquals(1, $crawler->filter('form')->count()); |
39
|
|
|
$this->assertEquals(1, $crawler->filter('div.modal')->count()); |
40
|
|
|
$this->responseAsserts($this->client->getResponse()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test deleteAction() of TranslationController |
45
|
|
|
*/ |
46
|
|
|
public function testDeleteAction() |
47
|
|
|
{ |
48
|
|
|
$this->client->request('GET', '/translation/delete/en_US/messages/test.key'); |
49
|
|
|
$this->assertTrue($this->client->getResponse()->isRedirect()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Test createAction() of TranslationController |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function testCreateAction() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$crawler = $this->client->request('GET', '/translation/create'); |
58
|
|
|
$this->assertEquals(1, $crawler->filter('form')->count()); |
59
|
|
|
$this->assertEquals(4, $crawler->filter('input')->count()); |
60
|
|
|
$this->responseAsserts($this->client->getResponse()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test checkAction() of TranslationController |
65
|
|
|
*/ |
66
|
|
|
public function testCheckAction() |
67
|
|
|
{ |
68
|
|
|
$this->client->request('GET', 'translation/check?key=test.key&locale=en_US&domain=messages'); |
69
|
|
|
$response = $this->client->getResponse(); |
70
|
|
|
$arrayResponse = json_decode($response->getContent(), true); |
71
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
72
|
|
|
$this->assertTrue($response->isSuccessful()); |
73
|
|
|
$this->assertTrue( |
74
|
|
|
$response->headers->contains( |
75
|
|
|
'Content-Type', |
76
|
|
|
'application/json' |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
$this->assertTrue($arrayResponse['isExists']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Common asserts for response |
84
|
|
|
* |
85
|
|
|
* @param $response |
86
|
|
|
*/ |
87
|
|
|
private function responseAsserts($response) |
88
|
|
|
{ |
89
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
90
|
|
|
$this->assertTrue($response->isSuccessful()); |
91
|
|
|
$this->assertTrue( |
92
|
|
|
$response->headers->contains( |
93
|
|
|
'Content-Type', |
94
|
|
|
'text/html; charset=UTF-8' |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set up fixtures for testing |
101
|
|
|
*/ |
102
|
|
|
public function setUp() |
103
|
|
|
{ |
104
|
|
|
static::bootKernel(array()); |
105
|
|
|
$container = static::$kernel->getContainer(); |
106
|
|
|
$this->client = static::createClient(); |
107
|
|
|
$this->uberMemcached = $container->get('uber.memcached'); |
108
|
|
|
$this->uberMemcached->addItem('en_US', array('messages' => array('test.key' => 'test value'))); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Tear down fixtures after testing |
113
|
|
|
*/ |
114
|
|
|
public function tearDown() |
115
|
|
|
{ |
116
|
|
|
$this->uberMemcached->deleteItem('en_US'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.