我正在尝试测试注册方法,但是我在日志中得到了它。
[2018-08-30 00:13:50] request.INFO:匹配的路线“注册”。 {“ route”:“注册”,“ route_parameters”:{“ _ route”:“注册”,“ _ controller”:“ App \ Controller \ UserController :: signup”},“ request_uri”:“ http://localhost/signup”, “ method”:“ POST”} [] [2018-08-30 00:13:50] security.INFO:填充了TokenStorage 使用匿名令牌。 [] [] [2018-08-30 00:13:50] doctrine.DEBUG: “开始交易” [] [] [2018-08-30 00:13:50] doctrine.DEBUG: “ ROLLBACK” [] [] [2018-08-30 00:13:50]请求。严重:未捕获的PHP 异常学说\ DBAL \ Exception \ TableNotFoundException:“ 执行'INSERT INTO用户(电子邮件,用户名, is_active,密码,角色)VALUES(?,?,?,?,?)':SQLSTATE [HY000]: 一般错误:1没有这样的表:user“ at /Applications/MAMP/htdocs/my_project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php 第63行{“ exception”:“ [object] (Doctrine \ DBAL \ Exception \ TableNotFoundException(代码:0):一个 执行'INSERT INTO用户(电子邮件,用户名, is_active,密码,角色)VALUES(?,?,?,?, ?)':\ n \ nSQLSTATE [HY000]:常规错误:1无此类表:用户位于 /Applications/MAMP/htdocs/my_project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php:63, 原则\ DBAL \ Driver \ PDOException(代码:HY000):SQLSTATE [HY000]: 一般错误:1没有这样的表:用户位于 /Applications/MAMP/htdocs/my_project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:82, PDOException(代码:HY000):SQLSTATE [HY000]:常规错误:1否这样 表:用户位于 /Applications/MAMP/htdocs/my_project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:80)“} []
这是一个错误
Failed asserting that 500 is identical to 200.
SignUpControllerTest.php
<?php
namespace App\Tests\Controller;
use App\Entity\Product;
use App\Entity\Category;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Repository\CategoryRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\ORM\Tools\SchemaTool;
class SignUpControllerTest extends WebTestCase
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
/**
* {@inheritDoc}
*/
protected function setUp()
{
self::bootKernel();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager();
parent::setUp();
}
public function testUserCreate()
{
$client = static::createClient();
$client->request('POST', '/signup', [], [], [], json_encode([
'user' => [
'username' => 'chuck_norris',
'password' => 'foobar',
'email' => 'chuck@norris.com',
'roles' => 'ROLE_USER'
],
]));
$response = $client->getResponse();
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('user', $data);
$this->assertSame('chuck@norris.com', $data['user']['email']);
$this->assertSame('chuck_norris', $data['user']['username']);
}
UserController.php
/**
* @Route("/signup", name="signup")
*/
public function signup(Request $request, UserPasswordEncoderInterface $passwordEncoder )
{
$user = new User();
$entityManager = $this->getDoctrine()->getManager();
$user->setEmail($request->get('email'));
$user->setPlainPassword($request->get('password'));
$user->setUsername($request->get('username'));
$password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('login');
}
User.php(实体)
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity
* @UniqueEntity(fields="email", message="Email already taken")
* @UniqueEntity(fields="username", message="Username already taken")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=190, unique=true)
* @Assert\NotBlank()
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="string", length=190, unique=true)
* @Assert\NotBlank()
*/
private $username;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* @Assert\NotBlank()
* @Assert\Length(max=190)
*/
private $plainPassword;
/**
* The below length depends on the "algorithm" you use for encoding
* the password, but this works well with bcrypt.
*
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="array")
*/
private $roles;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="user")
*/
private $products;
public function __construct()
{
$this->roles = array('ROLE_USER');
$this->isActive = true;
$this->products = new ArrayCollection();
}
// other properties and methods
public function getEmail()
{
return $this->email;
}
public function isEnabled()
{
return $this->isActive;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
}
public function getPlainPassword()
{
return $this->plainPassword;
}
public function setPlainPassword($password)
{
$this->plainPassword = $password;
}
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getSalt()
{
// The bcrypt and argon2i algorithms don't require a separate salt.
// You *may* need a real salt if you choose a different encoder.
return null;
}
public function getRoles()
{
return $this->roles;
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized, array('allowed_classes' => false));
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->setUser($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->contains($product)) {
$this->products->removeElement($product);
// set the owning side to null (unless already changed)
if ($product->getUser() === $this) {
$product->setUser(null);
}
}
return $this;
}
}