Make WordPress Core

Ticket #3875: TestPlugin.php

File TestPlugin.php, 3.7 KB (added by santosj, 16 years ago)

Plugin Unit Test

Line 
1<?php
2
3set_include_path(get_include_path()
4                .PATH_SEPARATOR.realpath(dirname(__FILE__).'/../wp-includes/')
5);
6
7require_once 'PHPUnit/Framework.php';
8
9require_once 'plugin.php';
10
11class MockObject_ConstructorNoReference
12{
13        function MockObject_ConstructorNoReference()
14        {
15                add_filter('testFilter', array($this, 'hook'));
16        }
17       
18        function hook()
19        {
20                echo 'Passed!';
21        }
22}
23
24class MockObject_ConstructorReference
25{
26        function MockObject_ConstructorReference()
27        {
28                add_filter('testFilter', array(&$this, 'hook'));
29        }
30       
31        function hook()
32        {
33                echo 'Passed!';
34        }
35}
36
37class MockObject
38{
39        function MockObject()
40        {
41               
42        }
43       
44        function hook()
45        {
46                echo 'Passed!';
47        }
48}
49
50function MockFunction1()
51{
52        echo 'Passed!';
53}
54
55function MockFunction2()
56{
57        echo 'Passed!';
58}
59
60function add_filter_test($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
61        global $wp_filter;
62
63        $wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
64        return true;
65}
66 
67class TestPlugin extends PHPUnit_Framework_TestCase
68{
69    public function testUseCase1()
70    {
71                // Add first object
72                $idx = _wp_filter_build_unique_id('testFilter', array($this, __FUNCTION__));
73               
74                $this->assertEquals('TestPlugintestUseCase10', $idx);
75               
76                // Add same object
77                $idx = _wp_filter_build_unique_id('testFilter', array($this, __FUNCTION__));
78               
79                $this->assertEquals('TestPlugintestUseCase10', $idx);
80    }
81       
82    public function testUseCase2()
83    {
84                // Add first object
85                $idx = _wp_filter_build_unique_id('testFilter', array(&$this, __FUNCTION__));
86                $this->assertEquals('TestPlugintestUseCase20', $idx);
87               
88                // Add same object
89                $idx = _wp_filter_build_unique_id('testFilter', array(&$this, __FUNCTION__));
90                $this->assertEquals('TestPlugintestUseCase20', $idx);
91    }
92 
93    public function testUseCase5()
94    {
95                $idx = _wp_filter_build_unique_id('testFilter', array('MockObject', 'hook'));
96                $this->assertEquals('MockObjecthook', $idx);
97    }
98 
99    public function testUseCase6()
100    {
101                $idx = _wp_filter_build_unique_id('testFilter', 'MockFunction1');
102                $this->assertEquals('MockFunction1', $idx);
103               
104        $idx = _wp_filter_build_unique_id('testFilter', 'MockFunction2');
105                $this->assertEquals('MockFunction2', $idx);
106    }
107       
108        public function testOldAddFilterfunction()
109        {
110                for($i=0; $i<500; ++$i)
111                {
112                        add_filter_test('testFilter', 'MockFunction1');
113                }
114        }
115       
116        public function testNewAddFilterFunction()
117        {
118                for($i=0; $i<500; ++$i)
119                {
120                        add_filter('testFilter', 'MockFunction1');
121                }
122        }
123       
124        public function testOldAddFilterFunctionStatic()
125        {
126                for($i=0; $i<500; ++$i)
127                {
128                        add_filter_test('testFilter', array('MockObject', 'hook'));
129                }
130        }
131       
132        public function testNewAddFilterFunctionStatic()
133        {
134                for($i=0; $i<500; ++$i)
135                {
136                        add_filter('testFilter', array('MockObject', 'hook'));
137                }
138        }
139       
140        public function testOldAddFilterFunctionClasses()
141        {
142                for($i=0; $i<500; ++$i)
143                {
144                        add_filter_test('testFilter', array(&$this, __FUNCTION__));
145                }
146        }
147       
148        public function testNewAddFilterFunctionClasses()
149        {
150                for($i=0; $i<500; ++$i)
151                {
152                        add_filter('testFilter', array(&$this, __FUNCTION__));
153                }
154        }
155       
156        public function testOldRemoveFilterFunctionClasses()
157        {
158                global $wp_filter;
159               
160                for($i=0; $i<500; $i++)
161                {
162                        $this->testing = $i;
163                        add_filter_test('testFilter1', array(&$this, __FUNCTION__));
164                }
165               
166                remove_filter('testFilter1', array(&$this, __FUNCTION__));
167        }
168       
169        public function testNewRemoveFilterFunctionClasses()
170        {
171                for($i=0; $i<500; $i++)
172                {
173                        add_filter('filter2', array(&$this, __FUNCTION__));
174                }
175               
176                $this->assertEquals(1, count($GLOBALS['wp_filter']['filter2'][10]));
177               
178                remove_filter('filter2', array(&$this, __FUNCTION__));
179               
180                $this->assertEquals(0, count($GLOBALS['wp_filter']['filter2'][10]));
181        }
182}