我一直在花时间来吸收编写可测试代码背后的方法,并且我 偶然发现了 Misko Hevery 的一篇精彩文章,他在其中清楚地解释了如何处理应用程序构建中的依赖关系,例如使用工厂来加载所有对象,从而减少使测试复杂化的依赖关系。
在他的文章中,他给出了一个最小但富有洞察力的示例,说明了他如何在 java 中设置应用程序,下面无耻地引用了这个家伙,并表示极大的敬意:
// Your main should look like this:
class Main {
public static void main(String…args) {
AppFactory factory = new AppFactory(args);
MyApp app = factory.create();
app.run();
}
}
然后他指出:
请注意代码如何分为三个阶段。创建工厂,创建应用程序,运行应用程序。这使得它可以测试。无论您的应用程序是什么,您都应该遵循这种模式。为了将单例放在正确的位置,Factory 只创建一个实例,然后在调用 new 时将该实例传递给所有类的构造函数。请参阅:http://misko.hevery.com/2009/ 03/30/协作者与工厂/
我不精通 Java,但假设这可以在 php 中模仿,当然减去 main() 方法,但是 $args 来自哪里网络应用程序的上下文?和初始化?
我非常有兴趣看到 PHP 中的最小可测试应用程序示例,甚至是人们认为测试高效的应用程序的链接。初始化是我最感兴趣的。我的目的不是复制粘贴,而是学习经验丰富的 OOP 编码人员的成果。
我翻遍了一些流行的代码库的代码:Zend、Symphony,但这些框架不是可运行的应用程序,而且对我来说“太大太快”,无法清晰地了解情况。此外,还指出了这些框架中有关测试实践的一些缺陷。只是一个小例子,如果可能的话(即使不能运行)可以让我在从头开始启动一个小型应用程序时更好地掌握正确的 OOP 代码布局实践。
I've been spending time to assimilate the methodologies behind writing testable code, and I stumbled on a great post by Misko Hevery where he explains clearly how to approach dependencies in application building, by using factories for example to load all objects and thus reduce lines of dependencies that complicate testing.
In his post he gives a minimal albeit insightful example of how he sets up application in java, shamelessly quoted below with great respect to the dude:
// Your main should look like this:
class Main {
public static void main(String…args) {
AppFactory factory = new AppFactory(args);
MyApp app = factory.create();
app.run();
}
}
He then states:
Notice how the code is broken to three phases. Create factory, create app, run app. This makes it testable. No matter what your app, you should fallow this pattern. To got singletons to right places the Factory only creates a single instance and then passes that instance to the constructors of all classes as it calls new. See: http://misko.hevery.com/2009/03/30/collaborator-vs-the-factory/
I am not proficient in Java, but assume this can be mimicked in php, minus the main() method of course, but where would $args come from in the context of a web-app? And initialisation?
I would be very interested in seeing a minimal testable-app example in PHP, or even links to apps that one would considered test-efficient. Initialisation is what I am curious about, mostly. My purpose is not to copy-paste, but learn from what experienced OOP coders have put out.
I did rummage through the code of a few popular code libraries: Zend, Symphony but these frameworks are not runnable applications and seem "too huge too fast" for me to grasp a clear picture. Besides, some deficiencies have been pointed out in those framework regarding testing practices. Just a small example, if possible (even if not runnable) would give me a better grip on proper OOP code-layout practices when starting a small app from scratch.
发布评论
评论(1)
PHPUnit 的作者 Sebastian Bergmann 有一个示例应用程序来说明您在 GitHub 上的要求:
Sebastian Bergmann, author of PHPUnit, has a sample app to illustrate what you are asking for at GitHub: