Add base\Struct from phalanx
[hoplite.git] / testing / tests / base / struct_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use \hoplite\base as base;
19
20 require_once TEST_ROOT . '/tests/base.php';
21
22 class TestStruct extends base\Struct
23 {
24 protected $fields = array(
25 'first',
26 'second',
27 'third'
28 );
29 }
30
31 class StructTest extends \PHPUnit_Framework_TestCase
32 {
33 public $struct;
34
35 public function setUp()
36 {
37 $this->struct = new TestStruct();
38 }
39
40 public function testCount()
41 {
42 $this->assertEquals(3, $this->struct->Count());
43 $this->struct->first = 'foo';
44 $this->assertEquals(3, $this->struct->Count());
45 }
46
47 public function testSet()
48 {
49 $this->struct->first = 1;
50 $this->struct->second = 2;
51 $this->struct->third = 3;
52 }
53
54 public function testSetInvalid()
55 {
56 $this->setExpectedException('hoplite\base\StructException');
57 $this->struct->fourth = 4;
58 }
59
60 public function testGetNull()
61 {
62 $this->assertNull($this->struct->first);
63 $this->assertNull($this->struct->second);
64 $this->assertNull($this->struct->third);
65 }
66
67 public function testGet()
68 {
69 $this->struct->first = 1;
70 $this->assertEquals(1, $this->struct->first);
71 }
72
73 public function testGetInvalid()
74 {
75 $this->setExpectedException('hoplite\base\StructException');
76 $foo = $this->struct->fourth;
77 }
78
79 public function testSetFromArray()
80 {
81 $array = array(
82 'first' => 1,
83 'second' => 2,
84 'fourth' => 4
85 );
86 $this->struct->SetFrom($array);
87 $this->assertEquals(1, $this->struct->first);
88 $this->assertEquals(2, $this->struct->second);
89 $this->assertNull($this->struct->third);
90 $this->assertEquals(3, $this->struct->Count());
91 }
92
93 public function testSetFromObject()
94 {
95 $obj = new \StdClass();
96 $obj->first = 1;
97 $obj->second = 2;
98 $obj->fourth = 4;
99 $this->struct->SetFrom($obj);
100 $this->assertEquals(1, $this->struct->first);
101 $this->assertEquals(2, $this->struct->second);
102 $this->assertNull($this->struct->third);
103 $this->assertEquals(3, $this->struct->Count());
104 }
105
106 public function testToArray()
107 {
108 $this->struct->first = 'alpha';
109 $this->struct->third = 'bravo';
110 $array = array(
111 'first' => 'alpha',
112 'third' => 'bravo'
113 );
114 $this->assertEquals($array, $this->struct->ToArray());
115 }
116 }