PHPUnit模拟类常量(PHPUnit Mocking class constant)

编程入门 行业动态 更新时间:2024-10-22 08:31:52
PHPUnit模拟类常量(PHPUnit Mocking class constant)

我正在测试一个注入了依赖项的类。 在我的无限聪明中,我在整个代码中分散了对依赖项类常量的引用。

(抱歉这个愚蠢的例子,想要偏离foobar。)

<?php class Book_PhoneBook extends Book { private $author; public function __construct(Author $author) { $this->author = $author; } public function getCover() { return $this->author->getTitle(Author::NAME_UNKNOWN) . ' - YELLOW PAGES'; } } class Author { const NAME_UNKNOWN = 'anonymous'; public function getTitle($nameStyle) { //do complicated calculation of author name if ($nameStyle == $this::NAME_UNKNOWN) { $name = "Anonymous"; } //do further complicated calculations, modifying $name return $name; } }

我的问题是单元测试! 当我嘲笑Author时,我得到“致命错误:未定义的类常量'NAME_UNKNOWN'”。 模拟的整个目的是避免包含Author类文件,那么处理该常量的正确方法是什么?

我可以将Author分成几个对象或拆分依赖常量的方法,但这会产生很多重复。

I am testing a class that has a dependency injected. In my infinite cleverness, I scattered references to the dependency's class constant throughout my code.

(Sorry for the stupid example, wanted to deviate from foobar.)

<?php class Book_PhoneBook extends Book { private $author; public function __construct(Author $author) { $this->author = $author; } public function getCover() { return $this->author->getTitle(Author::NAME_UNKNOWN) . ' - YELLOW PAGES'; } } class Author { const NAME_UNKNOWN = 'anonymous'; public function getTitle($nameStyle) { //do complicated calculation of author name if ($nameStyle == $this::NAME_UNKNOWN) { $name = "Anonymous"; } //do further complicated calculations, modifying $name return $name; } }

My problem is with unit tests! I'm getting "Fatal error: Undefined class constant 'NAME_UNKNOWN'" when I mock Author. The whole purpose of mocking is to avoid including the Author class file, so what is the correct way to handle that constant?

I could split Author into several objects or split the method that relies on the constant, but that would incur much repetition.

最满意答案

您使用来自Author的类常量的事实意味着您依赖于在您的代码中提供该类,因此也在您的测试中。 因此,为了测试Book_PhoneBook您需要拥有一个带有常量的Author类。 如果类常量将是您需要传递给类的一系列标志,则需要使该类可用,并且在测试中确保已加载原始类。 您依赖于Book_PhoneBook::getCover() 。

您也可以在Book_PhoneBook中使用类常量的字符串值,尽管这样会使其成为常量的目的失败,这样您就可以在一个地方更改值。

通过你的例子,了解你想要实现的内容有点棘手但是因为你想要删除Book_PhoneBook对Author的依赖,我会把它改成这样的东西:

<?php class Book_PhoneBook extends Book { private $author; public function __construct(Author $author) { $this->author = $author; } public function getCover() { return $this->author->getTitle() . ' - YELLOW PAGES'; } } class Author { const NAME_UNKNOWN = 'anonymous'; public function __construct($nameStyle) { $this->nameStyle = $nameStyle; } public function getTitle() { //do complicated calculation of author name if ($this->nameStyle == $this::NAME_UNKNOWN) { $name = "Anonymous"; } //do further complicated calculations, modifying $name return $name; } }

此解决方案确实存在为getTitle添加其他选项很困难的问题。 如果没有更好地了解您尝试使用getTitle实现的目标,我无法提供更好的解决方案。

The fact that you are using the class constant from Author means that you are dependent on having that class available in your code and thus also in your test. So in order to test Book_PhoneBook you need to have an Author class with the constant. If the class constant is going to be a series of flags that you will need to pass to your class, you will need to have the class available and in your test make sure that the original class is loaded. You are dependent on that class for Book_PhoneBook::getCover().

You can also just use the string value of the class constant in your Book_PhoneBook though that sort of defeats the purpose of making it a constant so that you are able to change the value in one place.

With your example, it is a little tricky to get an idea of what you are trying to achieve but since you want to remove the dependency on Author in Book_PhoneBook, I would change it to something like this:

<?php class Book_PhoneBook extends Book { private $author; public function __construct(Author $author) { $this->author = $author; } public function getCover() { return $this->author->getTitle() . ' - YELLOW PAGES'; } } class Author { const NAME_UNKNOWN = 'anonymous'; public function __construct($nameStyle) { $this->nameStyle = $nameStyle; } public function getTitle() { //do complicated calculation of author name if ($this->nameStyle == $this::NAME_UNKNOWN) { $name = "Anonymous"; } //do further complicated calculations, modifying $name return $name; } }

This solution does have the problem that adding other options is difficult for getTitle. And without having a better idea of what you are trying to achieve with getTitle, I am not able to offer a better solution.

更多推荐

本文发布于:2023-08-07 04:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   PHPUnit   Mocking   constant   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!