This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
InsertTest.php
160 lines (136 loc) · 5.33 KB
/
InsertTest.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Db
*/
namespace ZendTest\Db\Sql;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Expression;
class InsertTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Insert
*/
protected $insert;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->insert = new Insert;
}
/**
* @covers Zend\Db\Sql\Insert::into
*/
public function testInto()
{
$this->insert->into('table', 'schema');
$this->assertEquals('table', $this->readAttribute($this->insert, 'table'));
}
/**
* @covers Zend\Db\Sql\Insert::columns
*/
public function testColumns()
{
$this->insert->columns(array('foo', 'bar'));
$this->assertEquals(array('foo', 'bar'), $this->readAttribute($this->insert, 'columns'));
}
/**
* @covers Zend\Db\Sql\Insert::values
*/
public function testValues()
{
$this->insert->values(array('foo' => 'bar'));
$this->assertEquals(array('foo'), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array('bar'), $this->readAttribute($this->insert, 'values'));
// test will merge cols and values of previously set stuff
$this->insert->values(array('foo' => 'bax'), Insert::VALUES_MERGE);
$this->insert->values(array('boom' => 'bam'), Insert::VALUES_MERGE);
$this->assertEquals(array('foo', 'boom'), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array('bax', 'bam'), $this->readAttribute($this->insert, 'values'));
$this->insert->values(array('foo' => 'bax'));
$this->assertEquals(array('foo'), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array('bax'), $this->readAttribute($this->insert, 'values'));
}
/**
* @covers Zend\Db\Sql\Insert::prepareStatement
*/
public function testPrepareStatement()
{
$mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockDriver->expects($this->any())->method('getPrepareType')->will($this->returnValue('positional'));
$mockDriver->expects($this->any())->method('formatParameterName')->will($this->returnValue('?'));
$mockAdapter = $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockDriver));
$mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$pContainer = new \Zend\Db\Adapter\ParameterContainer(array());
$mockStatement->expects($this->any())->method('getParameterContainer')->will($this->returnValue($pContainer));
$mockStatement->expects($this->at(1))
->method('setSql')
->with($this->equalTo('INSERT INTO "foo" ("bar", "boo") VALUES (?, NOW())'));
$this->insert->into('foo')
->values(array('bar' => 'baz', 'boo' => new Expression('NOW()')));
$this->insert->prepareStatement($mockAdapter, $mockStatement);
}
/**
* @covers Zend\Db\Sql\Insert::getSqlString
*/
public function testGetSqlString()
{
$this->insert->into('foo')
->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null));
$this->assertEquals('INSERT INTO "foo" ("bar", "boo", "bam") VALUES (\'baz\', NOW(), NULL)', $this->insert->getSqlString());
}
/**
* @covers Zend\Db\Sql\Insert::__set
*/
public function test__set()
{
$this->insert->foo = 'bar';
$this->assertEquals(array('foo'), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array('bar'), $this->readAttribute($this->insert, 'values'));
}
/**
* @covers Zend\Db\Sql\Insert::__unset
*/
public function test__unset()
{
$this->insert->foo = 'bar';
$this->assertEquals(array('foo'), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array('bar'), $this->readAttribute($this->insert, 'values'));
unset($this->insert->foo);
$this->assertEquals(array(), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array(), $this->readAttribute($this->insert, 'values'));
}
/**
* @covers Zend\Db\Sql\Insert::__isset
*/
public function test__isset()
{
$this->insert->foo = 'bar';
$this->assertTrue(isset($this->insert->foo));
}
/**
* @covers Zend\Db\Sql\Insert::__get
*/
public function test__get()
{
$this->insert->foo = 'bar';
$this->assertEquals('bar', $this->insert->foo);
}
/**
* @group ZF2-536
*/
public function testValuesMerge()
{
$this->insert->into('foo')
->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null));
$this->insert->into('foo')
->values(array('qux' => 100), Insert::VALUES_MERGE);
$this->assertEquals('INSERT INTO "foo" ("bar", "boo", "bam", "qux") VALUES (\'baz\', NOW(), NULL, \'100\')', $this->insert->getSqlString());
}
}