Skip to content

Commit 9f2cd21

Browse files
committed
phpunit tests initial commit
1 parent 2e86855 commit 9f2cd21

18 files changed

+1204
-0
lines changed

Tests/Config/ConfigValidatorTest.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Tests\Config;
4+
5+
use Mdiyakov\DoctrineSolrBundle\Config\ConfigValidator;
6+
use Mdiyakov\DoctrineSolrBundle\Finder\ClassFinder;
7+
use Mdiyakov\DoctrineSolrBundle\Tests\Config\Entity\MyEntity;
8+
use Mdiyakov\DoctrineSolrBundle\Tests\Config\Entity\MyEntity2;
9+
use Symfony\Component\Yaml\Yaml;
10+
11+
class MyEntityFinder extends ClassFinder {}
12+
13+
14+
class ConfigValidatorTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var ConfigValidator
18+
*/
19+
private $validator;
20+
21+
/**
22+
* @var array[]][]
23+
*/
24+
private $configFixtures;
25+
26+
public function setUp()
27+
{
28+
$this->validator = new ConfigValidator();
29+
$this->configFixtures = Yaml::parse(file_get_contents(__DIR__ . '/config_fixtures.yml'));
30+
}
31+
32+
public function testSuccessValidation()
33+
{
34+
$this->runConfigTest('success_config');
35+
}
36+
37+
/**
38+
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\ConfigFieldException
39+
*/
40+
public function testConfigFieldMissed()
41+
{
42+
$this->runConfigTest('config_field_missed');
43+
}
44+
45+
/**
46+
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\SchemaConfigException
47+
* @expectedExceptionMessage You have more than one fields with the same document field name "d_id"
48+
*/
49+
public function testDocumentNamesAreUnique()
50+
{
51+
$this->runConfigTest('document_field_names_unique');
52+
}
53+
54+
/**
55+
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\RequiredFieldException
56+
* @expectedExceptionMessage Either getter method "getExtraField" or isser method "isExtraField" is not found in Mdiyakov\DoctrineSolrBundle\Tests\Config\Entity\MyEntity.
57+
*/
58+
public function testEntityFieldNotExist()
59+
{
60+
$this->runConfigTest('entity_field_not_exist');
61+
}
62+
63+
/**
64+
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\FilterConfigException
65+
* @expectedExceptionMessage Filter "filter_1" is not defined in "filters" section
66+
*/
67+
public function testFilterNotDefined()
68+
{
69+
$this->runConfigTest('filter_not_defined');
70+
}
71+
72+
/**
73+
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\ClientConfigException
74+
* @expectedExceptionMessage Solarium client "my_client" is not defined in "solarium_clients" section
75+
*/
76+
public function testClientNotDefined()
77+
{
78+
$this->runConfigTest('client_not_defined');
79+
}
80+
81+
/**
82+
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\SchemaNotFoundException
83+
* @expectedExceptionMessage Schema "my_schema" is not found in "schema" config section. Check config.yml
84+
*/
85+
public function testSchemaNotDefined()
86+
{
87+
$this->runConfigTest('schema_not_defined');
88+
}
89+
90+
/**
91+
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\ConfigFieldException
92+
* @expectedExceptionMessage "my_enitity_type" discriminator value has already been used.
93+
It seems there are two entity classes inside "indexed_entities" with identical discriminator config field value
94+
*/
95+
public function testDiscriminatorConfigNotUnique()
96+
{
97+
$config = $this->configFixtures['discriminator_config_not_unique'];
98+
99+
$config['indexed_entities']['my_entity_1']['class'] = MyEntity::class;
100+
$config['indexed_entities']['my_entity_2']['class'] = MyEntity2::class;
101+
102+
foreach ($config['indexed_entities'] as $entityConfig) {
103+
$this->validator->validate($entityConfig, $config['schemes'], $config['filters'], $config['solarium_clients']);
104+
}
105+
}
106+
107+
/**
108+
* @param string $configName
109+
* @return array
110+
*/
111+
private function getConfig($configName)
112+
{
113+
$config = $this->configFixtures[$configName];
114+
115+
$config['indexed_entities']['my_entity']['class'] = MyEntity::class;
116+
$config['indexed_entities']['my_entity']['finder_class'] = MyEntityFinder::class;
117+
118+
return $config;
119+
}
120+
121+
/**
122+
* @param string $configName
123+
*/
124+
private function runConfigTest($configName)
125+
{
126+
$config = $this->getConfig($configName);
127+
foreach ($config['indexed_entities'] as $entityConfig) {
128+
$this->validator->validate($entityConfig, $config['schemes'], $config['filters'], $config['solarium_clients']);
129+
}
130+
}
131+
}

Tests/Config/Entity/MyEntity.php

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Tests\Config\Entity;
4+
5+
6+
class MyEntity
7+
{
8+
/**
9+
* @var int
10+
*/
11+
private $id;
12+
13+
/**
14+
* @var \DateTime
15+
*/
16+
private $createdAt;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $title;
22+
23+
/**
24+
* @var float
25+
*/
26+
private $price;
27+
28+
/**
29+
* @var bool
30+
*/
31+
private $enabled = false;
32+
33+
/**
34+
* @var array
35+
*/
36+
private $tags = [];
37+
38+
/**
39+
* @return int
40+
*/
41+
public function getId()
42+
{
43+
return $this->id;
44+
}
45+
46+
/**
47+
* @param int $id
48+
*/
49+
public function setId($id)
50+
{
51+
$this->id = $id;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getTitle()
58+
{
59+
return $this->title;
60+
}
61+
62+
/**
63+
* @param string $title
64+
*/
65+
public function setTitle($title)
66+
{
67+
$this->title = $title;
68+
}
69+
70+
/**
71+
* @return float
72+
*/
73+
public function getPrice()
74+
{
75+
return $this->price;
76+
}
77+
78+
/**
79+
* @param float $price
80+
*/
81+
public function setPrice($price)
82+
{
83+
$this->price = $price;
84+
}
85+
86+
/**
87+
* @return boolean
88+
*/
89+
public function isEnabled()
90+
{
91+
return $this->enabled;
92+
}
93+
94+
/**
95+
* @param boolean $enabled
96+
*/
97+
public function setEnabled($enabled)
98+
{
99+
$this->enabled = $enabled;
100+
}
101+
102+
/**
103+
* @return \DateTime
104+
*/
105+
public function getCreatedAt()
106+
{
107+
return $this->createdAt;
108+
}
109+
110+
/**
111+
* @param \DateTime $createdAt
112+
*/
113+
public function setCreatedAt($createdAt)
114+
{
115+
$this->createdAt = $createdAt;
116+
}
117+
118+
/**
119+
* @return array
120+
*/
121+
public function getTags()
122+
{
123+
return $this->tags;
124+
}
125+
126+
/**
127+
* @param array $tags
128+
*/
129+
public function setTags($tags)
130+
{
131+
$this->tags = $tags;
132+
}
133+
134+
}
135+
136+

Tests/Config/Entity/MyEntity2.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Tests\Config\Entity;
4+
5+
class MyEntity2 extends MyEntity {}

0 commit comments

Comments
 (0)