- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想在 Symfony2 中测试持久性。我想知道它是更好的模拟实体并提供给实体管理器还是更好的模拟实体管理器并将实体传递给管理器?
我是第一个选项,但实体管理器抛出异常而不是对象不是实体学说。如何在 PHPUNIT 中测试持久性 symfony?
最佳答案
与其编写单元测试,不如为持久层编写集成测试。
单元测试中有一条规则“不要 mock 你不拥有的东西”。
你不拥有 Doctrine 类或接口(interface),你永远无法确定你对你模拟的接口(interface)所做的假设是真实的。即使它们在您编写测试时为真,您也无法确定 Doctrine 的行为是否随时间而改变。
每当您使用第 3 方代码时,您都应该为使用它的任何内容编写集成测试。集成测试实际上会调用数据库并确保该原则按照您认为的方式工作。
这就是为什么最好从第 3 方的东西解耦。
在学说的情况下,这很容易。您可以做的一件事是为每个存储库引入一个接口(interface)。
interface ArticleRepository
{
/**
* @param int $id
*
* @return Article
*
* @throws ArticleNotFoundException
*/
public function findById($id);
}
您可以轻松模拟或 stub 这样的接口(interface):
class MyServiceTest extends \PHPUnit_Framework_Test_Case
{
public function test_it_does_something()
{
$article = new Article();
$repository = $this->prophesize(ArticleRepository::class);
$repository->findById(13)->willReturn($article);
$myService = new MyService($repository->reveal());
$myService->doSomething();
// ...
}
}
该接口(interface)将与学说一起实现:
use Doctrine\ORM\EntityRepository;
class DoctrineArticleRepository extends EntityRepository implement ArticleRepository
{
public function findById($id)
{
// call doctrine here
}
}
实现是您要为其编写集成测试的对象。在存储库的集成测试中,您实际上将调用数据库:
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class ArticleTest extends KernelTestCase
{
private $em;
private $repository;
protected function setUp()
{
self::bootKernel();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager();
$this->repository = $this->em->getRepository(Article::class);
}
public function test_it_finds_an_article()
{
$this->createArticle(13);
$article = $this->repository->findById(13);
$this->assertInstanceOf(Article::class, $article);
$this->assertSame(13, $article->getId());
}
/**
* @expectedException ArticleNotFoundException
*/
public function test_it_throws_an_exception_if_article_is_not_found()
{
$this->repository->findById(42);
}
protected function tearDown()
{
parent::tearDown();
$this->em->close();
}
private function createArticle($id)
{
$article = new Article($id);
$this->em->persist($article);
$this->em->flush();
// clear is important, otherwise you might get objects stored by doctrine in memory
$this->em->clear();
}
}
在其他任何地方,您都将使用将被 stub (或模拟)的接口(interface)。在其他任何地方你都不会访问数据库。这使您的测试速度更快。
stub 中的实体可以简单地创建或 stub 。大多数时候我创建实体对象而不是模拟它们。
关于php - 单元测试持久层 - Symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35030745/
我开始从事一个用 Symfony 2.8 编写的大型项目。将整个项目升级到 SF 3 需要数百小时,现在还不可能。我想到了一个想法,将 symfony/symfony 包解压到它替换的单个包中(com
我在提交表单后使用 FOSUserEvents,但订阅者调用了两次。 这样我的验证码第一次有效第二次无效 这是我的代码 router = $router; $this->request
我有以下路线: blog_show: path: /test/123 defaults: { _controller: TotalcanBravofillBundle:Te
我是测试新手。我想测试我的功能。我已经成功安装了 phpUnit。我在互联网上查看了许多教程。但我无法获得有关测试的正确信息。这是我的功能代码: public function loginAction
我正在尝试重现 facebook batch requests 的行为在他们的图形 api 上运行。 所以我认为最简单的解决方案是在 Controller 上向我的应用程序发出几个请求,例如: pub
在 Symfony Progress Bar documentation有一个超酷酒吧的示例图像。不幸的是,看起来文档的其余部分没有解释如何获得这样的结果。 这是图片,以防您错过: 我怎么才能得到它?
我使用Finder发送假脱机电子邮件,但是自动名称生成器将点放在文件名中,有时它们出现在文件的开头。 查找程序似乎无法获取具有该名称的文件-那些文件被隐藏了……有人经历过这种行为吗?有什么建议如何使用
我正在尝试进行 LDAP 身份验证,我目前遇到此类错误: ServiceNotFoundException: The service "security.firewall.map.context.ma
有没有办法验证和检查集合数组是否为空。我已经尝试过: /** * @Assert\NotBlank() * @Assert\Length( min = 1) */ protected $work
使用Smyfony2和Doctrin2,可以使用以下示例创建数据固定装置:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/i
我看到在大多数Symfony 2示例中,例如,不存在记录时,Symfony 2会引发异常。我认为这种方法对最终用户不友好。为什么有人更喜欢引发异常而不在Flashbag上添加一些错误消息? 最佳答案
我对项目中的以下服务有疑问: app.security.guardAuthenticatorLoginPassword: class: AppBundle\Security\LoginPa
symfony缓存和登录Docker容器存在问题。 Web服务器从www-data用户和组执行,当我使用docker上安装的php从docker容器中清除symfony缓存时,它从root执行。 因此
我想了解 symfony 中的服务 我已阅读http://symfony.com/doc/2.3/book/service_container.html#creating-configuring-se
因为我对 Symfony 和 Doctrine 还很陌生,所以我有一个可能很愚蠢的问题;-) 有人可以用简单的词语向我解释集合(尤其是实体中的ArrayCollections)吗?它是什么以及何时以及
我收到了这个表格: {{ form_start(form) }} {{ form_end(form) }} 我想检查用户是否登录,我这样做了: {% if is_g
我的网站已准备好部署,我正在尝试将其设置为在线。 一些信息: 主持人是 OVH。 它不允许 SSH,我必须使用 FTP 发送文件。也没有命令行。 我现在希望能够在子目录中设置网站:/www/test(
过去几个月以来,我一直在尝试与symfony合作。昨晚我自动删除了不需要的存储库。之后,我无法使用symfony命令创建新的symfony项目。当我在终端中运行Symfony new Security
In the environnement variable, then in system variable, I edited the path and added在环境变量中,然后在系统变量
我们有一个 Symfony 1.4 应用程序,想升级到 Symfony 4。是否有可能或者我们必须重新编程该应用程序? 我们询问了我们附近的一家软件公司,他们告诉我们必须重新编写应用程序。 最佳答案
我是一名优秀的程序员,十分优秀!