Passed
Push — master ( 7db1e7...88fb1f )
by Aimeos
02:53
created

StandardTest::testGetObjectKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2026
6
 */
7
8
9
namespace Aimeos\Base\View;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $translate;
16
17
18
	protected function setUp() : void
19
	{
20
		$engines = array( '.phtml' => new \Aimeos\Base\View\Engine\TestEngine() );
21
22
		$this->object = new \Aimeos\Base\View\Standard( array( __DIR__ => array( '_testfiles' ) ), $engines );
23
		$this->translate = new \Aimeos\Base\View\Helper\Translate\Standard( $this->object, new \Aimeos\Base\Translation\None( 'en_GB' ) );
24
	}
25
26
27
	protected function tearDown() : void
28
	{
29
		unset( $this->object, $this->translate );
30
	}
31
32
33
	public function testMagicMethods()
34
	{
35
		$this->assertEquals( false, isset( $this->object->test ) );
36
37
		$this->object->test = 10;
38
		$this->assertEquals( 10, $this->object->test );
39
		$this->assertEquals( true, isset( $this->object->test ) );
40
41
		unset( $this->object->test );
42
		$this->assertEquals( false, isset( $this->object->test ) );
43
44
		$this->expectException( \Aimeos\Base\View\Exception::class );
45
		$this->object->test;
46
	}
47
48
49
	public function testAdd()
50
	{
51
		$this->object->set( 'test', 'oldvalue' );
52
		$this->object->set( 'test2', 'existing' );
53
		$result = $this->object->add( ['test' => 'value', 'key' => 'val'] );
54
55
		$this->assertInstanceOf( \Aimeos\Base\View\Iface::class, $result );
56
		$this->assertEquals( 'existing', $this->object->get( 'test2' ) );
57
		$this->assertEquals( 'value', $this->object->get( 'test' ) );
58
		$this->assertEquals( 'val', $this->object->get( 'key' ) );
59
	}
60
61
62
	public function testAssign()
63
	{
64
		$this->object->set( 'test', 'oldvalue' );
65
		$this->object->set( 'test2', 'existing' );
66
		$result = $this->object->assign( ['test' => 'value', 'key' => 'val'] );
67
68
		$this->assertInstanceOf( \Aimeos\Base\View\Iface::class, $result );
69
		$this->assertEquals( null, $this->object->get( 'test2' ) );
70
		$this->assertEquals( 'value', $this->object->get( 'test' ) );
71
		$this->assertEquals( 'val', $this->object->get( 'key' ) );
72
	}
73
74
75
	public function testGet()
76
	{
77
		$this->object->test = 'val';
78
		$this->assertEquals( 'val', $this->object->get( 'test' ) );
79
	}
80
81
82
	public function testGetDefault()
83
	{
84
		$this->assertEquals( null, $this->object->get( 'test' ) );
85
		$this->assertEquals( 1, $this->object->get( 'test', 1 ) );
86
	}
87
88
89
	public function testGetPath()
90
	{
91
		$this->object->test = array( 'key' => 'val' );
92
		$this->assertEquals( 'val', $this->object->get( 'test/key' ) );
93
	}
94
95
96
	public function testGetObjectKey()
97
	{
98
		$this->object->test = new \stdClass();
99
		$this->assertEquals( null, $this->object->get( 'test/key' ) );
100
	}
101
102
103
	public function testSet()
104
	{
105
		$result = $this->object->set( 'test', 'value' );
106
		$this->assertEquals( 'value', $this->object->get( 'test' ) );
107
		$this->assertInstanceOf( \Aimeos\Base\View\Iface::class, $result );
108
	}
109
110
111
	public function testSetPath()
112
	{
113
		$result = $this->object->set( 'test/key', 'value' );
114
		$this->assertEquals( 'value', $this->object->get( 'test/key' ) );
115
		$this->assertInstanceOf( \Aimeos\Base\View\Iface::class, $result );
116
	}
117
118
119
	public function testCallCreateHelper()
120
	{
121
		$enc = $this->object->encoder();
122
		$this->assertInstanceOf( \Aimeos\Base\View\Helper\Iface::class, $enc );
123
	}
124
125
126
	public function testCallInvalidName()
127
	{
128
		$this->expectException( \Aimeos\Base\View\Exception::class );
129
		$this->object->invalid();
130
	}
131
132
133
	public function testCallUnknown()
134
	{
135
		$this->expectException( \Aimeos\Base\View\Exception::class );
136
		$this->object->unknown();
137
	}
138
139
140
	public function testCallAddHelper()
141
	{
142
		$this->object->addHelper( 'translate', $this->translate );
143
		$this->assertEquals( 'File', $this->object->translate( 'test', 'File', 'Files', 1 ) );
144
	}
145
146
147
	public function testAssignRender()
148
	{
149
		$this->object->addHelper( 'translate', $this->translate );
150
151
		$ds = DIRECTORY_SEPARATOR;
152
		$filenames = array( 'notexisting', __DIR__ . $ds . '_testfiles' . $ds . 'template1' );
153
154
		$output = $this->object->assign( array( 'quantity' => 1 ) )->render( $filenames );
155
		$this->assertEquals( "Number of files: 1 File", $output );
156
157
		$output = $this->object->assign( array( 'quantity' => 0 ) )->render( $filenames );
158
		$this->assertEquals( "Number of files: 0 Files", $output );
159
	}
160
161
162
	public function testAssignRenderRelativePath()
163
	{
164
		$this->object->addHelper( 'translate', $this->translate );
165
166
		$output = $this->object->assign( ['quantity' => 1] )->render( ['notexisting', 'template1'] );
167
		$this->assertEquals( "Number of files: 1 File", $output );
168
169
		$output = $this->object->assign( ['quantity' => 2] )->render( ['notexisting', 'template2'] );
170
		$this->assertEquals( "Number of directories: 2", $output );
171
	}
172
}
173