Skip to content

Instantly share code, notes, and snippets.

@angelxmoreno
Last active May 25, 2023 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelxmoreno/c95a8f21fdadda5c9cd350cf2b216f63 to your computer and use it in GitHub Desktop.
Save angelxmoreno/c95a8f21fdadda5c9cd350cf2b216f63 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
/**
* Test suite bootstrap for SeoBakery.
*
* This function is used to find the location of CakePHP whether CakePHP
* has been installed as a dependency of the plugin, or the plugin is itself
* installed as a dependency of an application.
*/
use Migrations\TestSuite\Migrator;
$findRoot = function ($root) {
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . '/vendor/cakephp/cakephp')) {
return $root;
}
} while ($root !== $lastRoot);
throw new Exception('Cannot find the root of the application, unable to run tests');
};
$root = $findRoot(__FILE__);
unset($findRoot);
chdir($root);
require_once $root . '/vendor/autoload.php';
/**
* Define fallback values for required constants and configuration.
* To customize constants and configuration remove this require
* and define the data required by your plugin here.
*/
require_once $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';
require $root . '/config/bootstrap.php';
$migrator = new Migrator();
$migrator->run([
'connection' => 'test',
'plugin' => 'SeoBakery',
]);
<?php
declare(strict_types=1);
namespace SeoBakery\Test\TestCase\Core;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use SeoBakery\Test\SeoObjects\Product;
class SeoAwareEntityTest extends TestCase
{
/**
* Test subject
*/
protected Table $Products;
public function setUp(): void
{
parent::setUp();
$this->Products = $this->getTableInstance();
TableRegistry::getTableLocator()->set('Products', $this->Products);
}
protected function getTableInstance(): Table
{
$schema = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
'description' => ['type' => 'string'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
],
];
$table = new Table([
'alias' => 'Products',
'schema' => $schema,
'entityClass' => Product::class,
]);
$table->setPrimaryKey('id');
$table->setDisplayField('name');
return $table;
}
public function tearDown(): void
{
parent::tearDown();
unset($this->Products);
TableRegistry::getTableLocator()->remove('Products');
}
public function testSomething(): void
{
$data = [
'name' => 'Product 1',
'description' => 'description 1',
];
$entity = new Product($data, [
'source' => 'Products'
]);
$this->assertSame($data['name'], $entity->buildMetaTitleFallback('view'));
}
}
@angelxmoreno
Copy link
Author

when I run ./vendor/bin/phpunit --filter SeoAwareEntityTest I get:

amoreno@Angels-MacBook-Pro seo-bakery % ./vendor/bin/phpunit --filter SeoAwareEntityTest
PHPUnit 9.6.8 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 00:00.017, Memory: 12.00 MB

There was 1 error:

1) SeoBakery\Test\TestCase\Core\SeoAwareEntityTest::testSomething
Cake\Database\Exception\DatabaseException: Cannot describe slug_tags. It has 0 columns.

@angelxmoreno
Copy link
Author

The issue was that inside the entity I am doing TableRegistry::getTableLocator()->get($entity->getSource()); which looks for an alias that does no exist 🤷

The solution was to manually add the alias https://gist.github.com/angelxmoreno/c95a8f21fdadda5c9cd350cf2b216f63#file-seoawareentitytest-php-L22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment