1: <?php
2:
3: /**
4: *
5: * Test cases for the AlphaView class.
6: *
7: * @package alpha::tests
8: * @since 1.0
9: * @author John Collins <dev@alphaframework.org>
10: * @version $Id: AlphaView_Test.php 1762 2014-04-10 21:31:32Z alphadevx $
11: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12: * @copyright Copyright (c) 2014, John Collins (founder of Alpha Framework).
13: * All rights reserved.
14: *
15: * <pre>
16: * Redistribution and use in source and binary forms, with or
17: * without modification, are permitted provided that the
18: * following conditions are met:
19: *
20: * * Redistributions of source code must retain the above
21: * copyright notice, this list of conditions and the
22: * following disclaimer.
23: * * Redistributions in binary form must reproduce the above
24: * copyright notice, this list of conditions and the
25: * following disclaimer in the documentation and/or other
26: * materials provided with the distribution.
27: * * Neither the name of the Alpha Framework nor the names
28: * of its contributors may be used to endorse or promote
29: * products derived from this software without specific
30: * prior written permission.
31: *
32: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
35: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
37: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45: * </pre>
46: *
47: */
48: class AlphaView_Test extends PHPUnit_Framework_TestCase {
49: /**
50: * View class for testing
51: *
52: * @var AlphaView
53: * @since 1.0
54: */
55: private $view;
56:
57: /**
58: * (non-PHPdoc)
59: * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::setUp()
60: *
61: * @since 1.0
62: */
63: protected function setUp() {
64: $denum = new DEnum();
65: $denum->rebuildTable();
66:
67: $item = new DEnumItem();
68: $item->rebuildTable();
69:
70: $this->view = AlphaView::getInstance(new ArticleObject());
71: }
72:
73: /**
74: * (non-PHPdoc)
75: * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::tearDown()
76: *
77: * @since 1.0
78: */
79: protected function tearDown() {
80: unset($this->view);
81:
82: $denum = new DEnum();
83: $denum->dropTable();
84:
85: $item = new DEnumItem();
86: $item->dropTable();
87: }
88:
89: /**
90: * Testing that passing a bad object to the getInstance method will throw an IllegalArguementException
91: *
92: * @since 1.0
93: */
94: public function testGetInstanceBad() {
95: try {
96: $bad = AlphaView::getInstance(new AlphaView_Test());
97: $this->fail('testing that passing a bad object to the getInstance method will throw an IllegalArguementException');
98: }catch (IllegalArguementException $e) {
99: $this->assertEquals('The BO provided [AlphaView_Test] is not defined anywhere!', $e->getMessage(), 'testing that passing a bad object to the getInstance method will throw an IllegalArguementException');
100: }
101: }
102:
103: /**
104: * Testing that passing a good object to the getInstance method will return the child view object
105: *
106: * @since 1.0
107: */
108: public function testGetInstanceGood() {
109: try{
110: $good = AlphaView::getInstance(new ArticleObject());
111: $this->assertTrue($good instanceof ArticleView, 'testing that passing a good object to the getInstance method will return the child view object');
112: }catch (IllegalArguementException $e) {
113: $this->fail($e->getMessage());
114: }
115: }
116:
117: /**
118: * Testing that we can force the return of an AlphaView object even when a child definition for the provided BO exists
119: *
120: * @since 1.0
121: */
122: public function testGetInstanceForceParent() {
123: try{
124: $good = AlphaView::getInstance(new ArticleObject(), true);
125: $this->assertTrue($good instanceof AlphaView, 'testing that we can force the return of an AlphaView object even when a child definition for the provided BO exists');
126: }catch (IllegalArguementException $e) {
127: $this->fail($e->getMessage());
128: }
129: }
130:
131: /**
132: * Testing that we can attach a good BO to an existing view object
133: *
134: * @since 1.0
135: */
136: public function testSetBOGood() {
137: try{
138: $this->view->setBO(new ArticleObject());
139: $this->assertTrue(true);
140: }catch (IllegalArguementException $e) {
141: $this->fail($e->getMessage());
142: }
143: }
144:
145: /**
146: * Testing that attempting to attach a bad BO object to an existing view object will cause an exception
147: *
148: * @since 1.0
149: */
150: public function testSetBOBad() {
151: try{
152: $this->view->setBO(new AlphaView_Test());
153: $this->fail('testing that attempting to attach a bad BO object to an existing view object will cause an exception');
154: }catch (IllegalArguementException $e) {
155: $this->assertTrue(true);
156: }
157: }
158:
159: /**
160: * Testing that a bad mode param provided to the loadTemplate method will throw an exception
161: *
162: * @since 1.0
163: */
164: public function testLoadTemplateBad() {
165: try {
166: $this->view->loadTemplate($this->view->getBO(), 'BadMode', array());
167: $this->fail('testing that a bad mode param provided to the loadTemplate method will throw an exception');
168: }catch (IllegalArguementException $e) {
169: $this->assertEquals('No [BadMode] HTML template found for class [ArticleObject]', $e->getMessage(), 'testing that a bad mode param provided to the loadTemplate method will throw an exception');
170: }
171: }
172:
173: /**
174: * Testing accessing the attached BO via getBO()
175: *
176: * @since 1.0
177: */
178: public function testGetBO() {
179: $article = new ArticleObject();
180: $article->set('title', 'Test Article');
181: $this->view->setBO($article);
182:
183: $this->assertEquals('Test Article', $this->view->getBO()->get('title'), 'testing accessing the attached BO via getBO()');
184: }
185:
186: /**
187: * Testing that a generated HTML fragment can load from a file
188: *
189: * @since 1.2.3
190: */
191: public function testLoadTemplateFragment() {
192:
193: $generatedHTML = AlphaView::loadTemplateFragment('html', 'footer.phtml', array());
194:
195: $pos = strpos($generatedHTML, '<footer>');
196:
197: $this->assertTrue(strpos($generatedHTML, '<footer>') > 0, 'Testing that a generated HTML fragment can load from a file');
198: }
199: }
200:
201: ?>