/
ComponentTest.php
124 lines (106 loc) · 3.61 KB
/
ComponentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @since 1.2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Controller;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use Cake\Utility\ClassRegistry;
use TestApp\Controller\ComponentTestController;
use TestApp\Controller\Component\AppleComponent;
use TestApp\Controller\Component\OrangeComponent;
/**
* ComponentTest class
*
*/
class ComponentTest extends TestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('App.namespace', 'TestApp');
$this->_pluginPaths = App::path('Plugin');
}
/**
* test accessing inner components.
*
* @return void
*/
public function testInnerComponentConstruction() {
$Collection = new ComponentRegistry();
$Component = new AppleComponent($Collection);
$this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Component->Orange, 'class is wrong');
}
/**
* test component loading
*
* @return void
*/
public function testNestedComponentLoading() {
$Collection = new ComponentRegistry();
$Apple = new AppleComponent($Collection);
$this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
$this->assertInstanceOf('TestApp\Controller\Component\BananaComponent', $Apple->Orange->Banana, 'class is wrong');
$this->assertTrue(empty($Apple->Session));
$this->assertTrue(empty($Apple->Orange->Session));
}
/**
* test that component components are not enabled in the collection.
*
* @return void
*/
public function testInnerComponentsAreNotEnabled() {
$mock = $this->getMock('Cake\Event\EventManager');
$controller = new Controller();
$controller->setEventManager($mock);
$mock->expects($this->once())
->method('attach')
->with($this->isInstanceOf('TestApp\Controller\Component\AppleComponent'));
$Collection = new ComponentRegistry($controller);
$Apple = $Collection->load('Apple');
$this->assertInstanceOf('TestApp\Controller\Component\OrangeComponent', $Apple->Orange, 'class is wrong');
}
/**
* test a component being used more than once.
*
* @return void
*/
public function testMultipleComponentInitialize() {
$Collection = new ComponentRegistry();
$Banana = $Collection->load('Banana');
$Orange = $Collection->load('Orange');
$this->assertSame($Banana, $Orange->Banana, 'Should be references');
$Banana->testField = 'OrangeField';
$this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
}
/**
* Test mutually referencing components.
*
* @return void
*/
public function testSomethingReferencingCookieComponent() {
$Controller = new ComponentTestController();
$Controller->components = array('SomethingWithCookie');
$Controller->uses = false;
$Controller->constructClasses();
$Controller->startupProcess();
$this->assertInstanceOf('TestApp\Controller\Component\SomethingWithCookieComponent', $Controller->SomethingWithCookie);
$this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $Controller->SomethingWithCookie->Cookie);
}
}