Completed
Push — master ( d508c6...ead087 )
by smiley
02:37
created

QueryTestAbstract::testCreateDatabaseNoName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class QueryTestAbstract
4
 *
5
 * @filesource   QueryTest.php
6
 * @created      12.05.2017
7
 * @package      chillerlan\DatabaseTest\Query
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\DatabaseTest\Query;
14
15
use chillerlan\Database\{DBQuery, DBResult};
16
use chillerlan\Database\Drivers\DBDriverException;
17
use chillerlan\Database\Query\{
18
	CreateDatabaseInterface, CreateTableInterface, DeleteInterface,
19
	InsertInterface, SelectInterface, StatementInterface, UpdateInterface
20
};
21
use chillerlan\DatabaseTest\TestAbstract;
22
23
abstract class QueryTestAbstract extends TestAbstract{
24
25
	const TEST_DBNAME = 'vagrant';
26
	const TEST_TABLENAME = 'querytest';
27
	/**
28
	 * @var \chillerlan\Database\DBQuery
29
	 */
30
	protected $statement;
31
32
	public function setUp(){
33
		parent::setUp();
34
35
		$this->statement = new DBQuery($this->DBDriver);
36
	}
37
38
	public function testInstance(){
39
40
		$update = $this->statement->update;
41
		$this->assertInstanceOf(StatementInterface::class, $update);
42
		$this->assertInstanceOf(UpdateInterface::class, $update);
43
44
		$delete = $this->statement->delete;
45
		$this->assertInstanceOf(StatementInterface::class, $delete);
46
		$this->assertInstanceOf(DeleteInterface::class, $delete);
47
48
	}
49
50
	protected function createDatabase(){
51
		$createdb = $this->statement->create->database(self::TEST_DBNAME);
52
		$this->assertInstanceOf(StatementInterface::class, $createdb);
53
		$this->assertInstanceOf(CreateDatabaseInterface::class, $createdb);
54
55
		return $createdb->name(self::TEST_DBNAME)->charset('utf8');
56
	}
57
58
	/**
59
	 * @expectedException \chillerlan\Database\Query\QueryException
60
	 * @expectedExceptionMessage no name specified
61
	 */
62
	public function testCreateDatabaseNoName(){
63
		$this->statement->create->database()->sql();
64
	}
65
66
	public function testCreateTable(){
67
68
		try{
69
			$this->DBDriver->raw('DROP TABLE '.self::TEST_TABLENAME);
70
		}
71
		catch(DBDriverException $e){
72
			var_dump('cannot drop "'.self::TEST_TABLENAME.'", table does not exist');
0 ignored issues
show
Security Debugging Code introduced by
var_dump('cannot drop "'...table does not exist'); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
73
		}
74
75
		$createTable = $this->statement->create->table(self::TEST_DBNAME.'.'.self::TEST_TABLENAME);
76
77
		$this->assertInstanceOf(StatementInterface::class, $createTable);
78
		$this->assertInstanceOf(CreateTableInterface::class, $createTable);
79
80
		$createTable->name(self::TEST_TABLENAME)
81
			->ifNotExists()
82
#			->temp()
83
			->charset('utf8')
84
			->field('id', 'int', 10)
85
			->field('hash', 'varchar', 32)
86
			->field('data', 'text', null, null, 'utf8')
87
			->field('value', 'decimal', '9,6')
88
			->field('active', 'boolean', null, null, null, null, null, 'false')
89
			->field('created', 'timestamp', null, null, null, null, 'CURRENT_TIMESTAMP')
90
			->primaryKey('id')
91
		;
92
93
#		print_r(PHP_EOL.$createTable->sql().PHP_EOL);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
95
		$this->assertTrue($createTable->execute());
96
	}
97
98
	/**
99
	 * @expectedException \chillerlan\Database\Query\QueryException
100
	 * @expectedExceptionMessage no name specified
101
	 */
102
	public function testCreateTableNoName(){
103
		$this->statement->create->table()->sql();
104
	}
105
106
	public function testInsert(){
107
		$insert = $this->statement->insert;
108
109
		$this->assertInstanceOf(StatementInterface::class, $insert);
110
		$this->assertInstanceOf(InsertInterface::class, $insert);
111
112
		$insert
113
			->into(self::TEST_TABLENAME)
114
			->values(['id' => 0, 'hash' => md5(0), 'data' => 'foo', 'value' => 123.456, 'active' => 1])
115
		;
116
117
#		print_r(PHP_EOL.$insert->sql().PHP_EOL);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
119
		$this->assertTrue($insert->execute());
120
	}
121
122
	public function testInsertMulti(){
123
		$insert = $this->statement->insert;
124
125
		$insert
126
			->into(self::TEST_TABLENAME)
127
			->values([
128
				['id' => 1, 'hash' => md5(1), 'data' => 'foo', 'value' => 123.456,    'active' => 0],
129
				['id' => 2, 'hash' => md5(2), 'data' => 'foo', 'value' => 123.456789, 'active' => 1],
130
				['id' => 3, 'hash' => md5(3), 'data' => 'foo', 'value' => 123.456,    'active' => 0],
131
			])
132
		;
133
134
		$this->assertTrue($insert->execute());
135
	}
136
137
	/**
138
	 * @expectedException \chillerlan\Database\Query\QueryException
139
	 * @expectedExceptionMessage no values given
140
	 */
141
	public function testInsertInvalidData(){
142
		$this->statement->insert->into(self::TEST_TABLENAME)->values([])->sql();
143
	}
144
145
	public function testSelect(){
146
		$select = $this->statement->select;
147
		$this->assertInstanceOf(StatementInterface::class, $select);
148
		$this->assertInstanceOf(SelectInterface::class, $select);
149
150
		$select
151
			->cols([
152
				'id'   => 't1.id',
153
				'hash' => ['t1.hash', 'upper'],
154
			])
155
			->from(['t1' => self::TEST_TABLENAME])
156
			->offset(1)
157
			->limit(2);
158
159
#		print_r(PHP_EOL.$select->sql().PHP_EOL);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
160
		$result = $select->execute();
161
		$this->assertInstanceOf(DBResult::class, $result);
162
		$this->assertCount(2, $result);
163
#		print_r($result);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
164
165
		$select = $this->statement->select;
166
		$select
167
			->cols(['id', 'hash', 'value'])
168
			->from([self::TEST_TABLENAME])
169
			->where('active', 1)
170
		;
171
172
#		print_r(PHP_EOL.$select->sql().PHP_EOL);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
173
		$result = $select->execute();
174
		$this->assertInstanceOf(DBResult::class, $result);
175
		$this->assertCount(2, $result);
176
#		print_r($result);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
177
178
		$select = $this->statement->select;
179
		$select
180
			->from([self::TEST_TABLENAME])
181
			->where('id', [1,2,3], 'in')
182
			->orderby(['id' => 'desc'])
183
		;
184
		$result = $select->execute();
185
#		print_r($result);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186
187
		if((bool)$result[0]->active){
188
			// postgres t/f
189
			$this->markTestSkipped('['.$this->DBDriver->dialect.'] invalid boolean value');
0 ignored issues
show
Bug introduced by
Accessing dialect on the interface chillerlan\Database\Drivers\DBDriverInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
190
		}
191
192
		$this->assertCount(3, $result);
193
		$this->assertFalse((bool)$result[0]->active);
194
		$this->assertSame(3, (int)$result[0]->id);
195
	}
196
197
	/**
198
	 * @expectedException \chillerlan\Database\Query\QueryException
199
	 * @expectedExceptionMessage no FROM expression specified
200
	 */
201
	public function testSelectEmptyFrom(){
202
		$this->statement->select->from([])->sql();
203
	}
204
205
	#public function testDelete(){}
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
206
207
	#public function testUpdate(){}
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
208
}
209