propel /test/testsuite/generator/builder/om/GeneratedQueryArrayColumnTypeTest.php

Language PHP Lines 227
MD5 Hash 6b71e9d5fb9ab68d448e6d2a612fc418
Repository https://github.com/mattleff/propel.git View Raw File View Project SPDX
  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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php

/**
 * This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__) . '/../../../../../generator/lib/util/PropelQuickBuilder.php';
require_once dirname(__FILE__) . '/../../../../../runtime/lib/Propel.php';

/**
 * Tests the generated queries for array column types filters
 *
 * @author     Francois Zaninotto
 * @package    generator.builder.om
 */
class GeneratedQueryArrayColumnTypeTest extends PHPUnit_Framework_TestCase
{
	public function setUp()
	{
		if (!class_exists('ComplexColumnTypeEntity11')) {
			$schema = <<<EOF
<database name="generated_object_complex_type_test_11">
	<table name="complex_column_type_entity_11">
		<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
		<column name="tags" type="ARRAY" />
		<column name="value_set" type="ARRAY" />
	</table>
</database>
EOF;
			PropelQuickBuilder::buildSchema($schema);
			$e0 = new ComplexColumnTypeEntity11();
			$e0->save();
			$e1 = new ComplexColumnTypeEntity11();
			$e1->setTags(array('foo', 'bar', 'baz'));
			$e1->save();
			$e2 = new ComplexColumnTypeEntity11();
			$e2->setTags(array('bar'));
			$e2->save();
			$e3 = new ComplexColumnTypeEntity11();
			$e3->setTags(array('bar23'));
			$e3->save();
		}
	}
	
	public function testActiveQueryMethods()
	{
		$this->assertTrue(method_exists('ComplexColumnTypeEntity11Query', 'filterByTags'));
		$this->assertTrue(method_exists('ComplexColumnTypeEntity11Query', 'filterByTag'));
		// only plural column names get a singular filter
		$this->assertTrue(method_exists('ComplexColumnTypeEntity11Query', 'filterByValueSet'));
	}

	public function testColumnHydration()
	{
		$e = ComplexColumnTypeEntity11Query::create()->orderById()->offset(1)->findOne();
		$this->assertEquals(array('foo', 'bar', 'baz'), $e->getTags(), 'array columns are correctly hydrated');
	}

	public function testWhere()
	{
		$e = ComplexColumnTypeEntity11Query::create()
			->where('ComplexColumnTypeEntity11.Tags LIKE ?', '%| bar23 |%')
			->find();
		$this->assertEquals(1, $e->count());
		$this->assertEquals(array('bar23'), $e[0]->getTags(), 'array columns are searchable by serialized object using where()');
		$e = ComplexColumnTypeEntity11Query::create()
			->where('ComplexColumnTypeEntity11.Tags = ?', array('bar23'))
			->find();
		$this->assertEquals(1, $e->count());
		$this->assertEquals(array('bar23'), $e[0]->getTags(), 'array columns are searchable by object using where()');
	}

	public function testFilterByColumn()
	{
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('bar'))
			->orderById()
			->find();
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[0]->getTags(), 'array columns are searchable by element');
		$this->assertEquals(array('bar'), $e[1]->getTags(), 'array columns are searchable by element');
		$this->assertEquals(2, $e->count(), 'array columns do not return false positives');
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('bar23'))
			->findOne();
		$this->assertEquals(array('bar23'), $e->getTags(), 'array columns are searchable by element');
	}

	public function testFilterByColumnUsingContainsAll()
	{
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array(), Criteria::CONTAINS_ALL)
			->find();
		$this->assertEquals(4, $e->count());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('bar'), Criteria::CONTAINS_ALL)
			->orderById()
			->find();
		$this->assertEquals(2, $e->count());
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[0]->getTags());
		$this->assertEquals(array('bar'), $e[1]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('bar23'), Criteria::CONTAINS_ALL)
			->find();
		$this->assertEquals(1, $e->count());
		$this->assertEquals(array('bar23'), $e[0]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('foo', 'bar'), Criteria::CONTAINS_ALL)
			->find();
		$this->assertEquals(1, $e->count());
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[0]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('foo', 'bar23'), Criteria::CONTAINS_ALL)
			->find();
		$this->assertEquals(0, $e->count());
	}

	public function testFilterByColumnUsingContainsSome()
	{
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array(), Criteria::CONTAINS_SOME)
			->find();
		$this->assertEquals(4, $e->count());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('bar'), Criteria::CONTAINS_SOME)
			->orderById()
			->find();
		$this->assertEquals(2, $e->count());
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[0]->getTags());
		$this->assertEquals(array('bar'), $e[1]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('bar23'), Criteria::CONTAINS_SOME)
			->find();
		$this->assertEquals(1, $e->count());
		$this->assertEquals(array('bar23'), $e[0]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('foo', 'bar'), Criteria::CONTAINS_SOME)
			->orderById()
			->find();
		$this->assertEquals(2, $e->count());
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[0]->getTags());
		$this->assertEquals(array('bar'), $e[1]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('foo', 'bar23'), Criteria::CONTAINS_SOME)
			->find();
		$this->assertEquals(2, $e->count());
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[0]->getTags());
		$this->assertEquals(array('bar23'), $e[1]->getTags());
	}

	public function testFilterByColumnUsingContainsNone()
	{
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array(), Criteria::CONTAINS_NONE)
			->find();
		$this->assertEquals(1, $e->count());
		$this->assertEquals(array(), $e[0]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('bar'), Criteria::CONTAINS_NONE)
			->orderById()
			->find();
		$this->assertEquals(2, $e->count());
		$this->assertEquals(array(), $e[0]->getTags());
		$this->assertEquals(array('bar23'), $e[1]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('bar23'), Criteria::CONTAINS_NONE)
			->find();
		$this->assertEquals(3, $e->count());
		$this->assertEquals(array(), $e[0]->getTags());
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[1]->getTags());
		$this->assertEquals(array('bar'), $e[2]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('foo', 'bar'), Criteria::CONTAINS_NONE)
			->orderById()
			->find();
		$this->assertEquals(2, $e->count());
		$this->assertEquals(array(), $e[0]->getTags());
		$this->assertEquals(array('bar23'), $e[1]->getTags());
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTags(array('foo', 'bar23'), Criteria::CONTAINS_NONE)
			->find();
		$this->assertEquals(2, $e->count());
		$this->assertEquals(array(), $e[0]->getTags());
		$this->assertEquals(array('bar'), $e[1]->getTags());
	}
	
	public function testFilterBySingularColumn()
	{
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTag('bar')
			->orderById()
			->find();
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[0]->getTags(), 'array columns are searchable by element');
		$this->assertEquals(array('bar'), $e[1]->getTags(), 'array columns are searchable by element');
		$this->assertEquals(2, $e->count(), 'array columns do not return false positives');
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTag('bar23')
			->findOne();
		$this->assertEquals(array('bar23'), $e->getTags(), 'array columns are searchable by element');
	}
	
	public function testFilterBySingularColumnUsingContainsAll()
	{
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTag('bar', Criteria::CONTAINS_ALL)
			->orderById()
			->find();
		$this->assertEquals(2, $e->count(), 'array columns are searchable by element using Criteria::CONTAINS_ALL');
		$this->assertEquals(array('foo', 'bar', 'baz'), $e[0]->getTags(), 'array columns are searchable by element using Criteria::CONTAINS_ALL');
		$this->assertEquals(array('bar'), $e[1]->getTags(), 'array columns are searchable by element using Criteria::CONTAINS_ALL');
	}
	
	public function testFilterBySingularColumnUsingContainsNone()
	{
		$e = ComplexColumnTypeEntity11Query::create()
			->filterByTag('bar', Criteria::CONTAINS_NONE)
			->orderById()
			->find();
		$this->assertEquals(2, $e->count(), 'array columns are searchable by element using Criteria::CONTAINS_NONE');
		$this->assertEquals(array(), $e[0]->getTags(), 'array columns are searchable by element using Criteria::CONTAINS_NONE');
		$this->assertEquals(array('bar23'), $e[1]->getTags(), 'array columns are searchable by element using Criteria::CONTAINS_NONE');
	}
}
Back to Top