Skip to content

Commit

Permalink
Merge pull request #181 from lilHermit/fixes/entity-class-issues
Browse files Browse the repository at this point in the history
Fixes to entityClass not working 100%
  • Loading branch information
markstory authored Aug 23, 2018
2 parents 00215eb + 49c4874 commit e8ee0bf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"autoload-dev": {
"psr-4": {
"Cake\\ElasticSearch\\Test\\": "tests",
"TestApp\\": "tests/testapp/TestApp/src/",
"TestPlugin\\": "tests/testapp/Plugin/TestPlugin/src",
"TestPluginTwo\\": "tests/testapp/Plugin/TestPluginTwo/src"
}
Expand Down
13 changes: 6 additions & 7 deletions src/Association/Embedded.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,18 @@ abstract class Embedded
public function __construct($alias, $options = [])
{
$this->alias = $alias;
$defaults = [
$properties = [
'entityClass',
'property'
];
foreach ($defaults as $prop) {
$options += [
'entityClass' => $alias
];
foreach ($properties as $prop) {
if (isset($options[$prop])) {
$this->{$prop} = $options[$prop];
$this->{$prop}($options[$prop]);
}
}

if (empty($this->entityClass) && strpos($alias, '.')) {
$this->entityClass = $alias;
}
}

/**
Expand Down
60 changes: 60 additions & 0 deletions tests/TestCase/EmbeddedDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\ElasticSearch\Test;

use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\ElasticSearch\Index;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -62,6 +63,45 @@ public function testGetWithEmbedOne()
$this->assertEquals('123 street', $result->address->street);
}

/**
* DataProvider for different embed types
*
* @return array
*/

public function embedTypeProvider()
{
return [
// Test to make sure entityClass is derived from Alias
[[], 'TestApp\Model\Document\Address'],

// Test to make sure simple classname entityClass works
[['entityClass' => 'Address'], 'TestApp\Model\Document\Address'],

// Test to make sure full namespace on entityClass works
[['entityClass' => 'TestApp\Model\Document\Address'], 'TestApp\Model\Document\Address'],
];
}

/**
* Test fetching with EmbedOne documents.
*
* @dataProvider embedTypeProvider
*
* @param array $options Options to pass to embed
* @param string $expected Expected type
*
* @return void
*/
public function testGetWithEmbedOneType($options, $expected)
{
Configure::write('App.namespace', 'TestApp');
$this->index->embedOne('Address', $options);
$result = $this->index->get(1);
$this->assertInstanceOf($expected, $result->address);
$this->assertEquals('123 street', $result->address->street);
}

/**
* Test fetching with embedded documents.
*
Expand Down Expand Up @@ -89,6 +129,26 @@ public function testGetWithEmbedMany()
$this->assertInstanceOf('Cake\ElasticSearch\Document', $result->address[1]);
}

/**
* Test fetching with EmbedMany documents.
*
* @dataProvider embedTypeProvider
*
* @param array $options Options to pass to embed
* @param string $expected Expected type
*
* @return void
*/
public function testGetWithEmbedManyType($options, $expected)
{
Configure::write('App.namespace', 'TestApp');
$this->index->embedMany('Address', $options);
$result = $this->index->get(3);
$this->assertInternalType('array', $result->address);
$this->assertInstanceOf($expected, $result->address[0]);
$this->assertInstanceOf($expected, $result->address[1]);
}

/**
* Test fetching with embedded documents.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/testapp/TestApp/src/Model/Document/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace TestApp\Model\Document;

use Cake\ElasticSearch\Document;

class Address extends Document
{
}

0 comments on commit e8ee0bf

Please sign in to comment.