Tôi có một loạt các thử nghiệm cho các bộ điều khiển dựa trên các API của bên thứ ba cần được mô phỏng trong môi trường thử nghiệm. Chúng tôi ghi đè trình thu thập thông tin khách hàng thử nghiệm chức năng và loại bỏ các phụ thuộc mà chúng tôi cần kiểm tra. Chìa khóa ở đây là các mocks thay đổi trên mọi thử nghiệm.Ghi đè Symfony 3.3 dịch vụ vùng chứa trong các kiểm tra điều khiển
Trong Symfony 3.2 này hoạt động tốt nhưng trong Symfony 3.3 Tôi có một số cảnh báo không dùng nữa do cách các dịch vụ hiện đang tiêm:
Một trường hợp ví dụ thử nghiệm điều khiển:
class MyControllerTest extends WebTestCase
{
private static $kernelModifier = null;
public function setKernelModifier(\Closure $kernelModifier)
{
self::$kernelModifier = $kernelModifier;
$this->ensureKernelShutdown();
}
protected static function createClient(array $options = [], array $server = [])
{
static::bootKernel($options);
if ($kernelModifier = self::$kernelModifier) {
$kernelModifier->__invoke();
self::$kernelModifier = null;
};
$client = static::$kernel->getContainer()->get('test.client');
$client->setServerParameters($server);
return $client;
}
protected function getPropertyClient()
{
$mockService = (new PropertyMock())->getPropertyMock();
$this->setKernelModifier(function() use ($mockService) {
static::$kernel->getContainer()->set('app.property_service', $mockService);
});
return static::createClient();
}
protected function getPropertyFailureClient()
{
$mockService = (new PropertyMock())->getPropertyFailureMock();
$this->setKernelModifier(function() use ($mockService) {
static::$kernel->getContainer()->set('app.property_service', $mockService);
});
return static::createClient();
}
}
Các thực tế kiểm tra:
public function testInvalidPropertyRequest()
{
$client = $this->getPropertyClient();
$client->request(
'POST',
'/webhook/property',
[],
[],
[],
''
);
$this->assertEquals(400, $client->getResponse()->getStatusCode());
}
Các lỗi không chấp nhận:
Setting the "app.property_service" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0: 3x
Tôi đã trải qua tài liệu phát hành 3.3, BC bị gián đoạn và ngừng sử dụng và không thể tìm ra cách tôi sẽ thay thế các dịch vụ container bằng mocks sau khi chúng được định cấu hình.
Mọi trợ giúp đều được đánh giá cao.
Có thể điều này có thể giúp bạn: https://stackoverflow.com/a/44948780/1652031 – TiMESPLiNTER
@TiMESPLiNTER Cảm ơn vì điều đó. Chúng tôi đã giải quyết nó cuối cùng bằng cách tái cấu trúc các thử nghiệm của chúng tôi để chúng không còn cần phải sửa đổi vùng chứa. –